You are here:  » How to generate master/detail page


How to generate master/detail page

Submitted by Stian on Thu, 2006-10-26 18:20 in

Hi, I have the following xml file:

<members>
<team1>
<member id="7">
<name>Hildegunn Lie</name>
<age>34</name>
</member>
<member id="4">
<name>Elin Vevle</name>
<age>28</name>
</member>
<member id="8">
<name>Sigrun Bruget</name>
<age>33</name>
</member>
</team1>
<team2>
<member id="2">
<name>Stian Totland</name>
<age>32</name>
</member>
<member id="9">
<name>Rune Vedvik</name>
<age>27</name>
</member>
</team2>
</members>

What I would like is one page with a UL list in this format:

  • team1
    • Hildegunn Lie
    • Elin Vevle
    • Sigrun Bruget
  • team2
    • Stian Totland
    • Rune Vedvik

The XML could be extended with more data on each member, such as adress and phone. And there could be added more teams and members. Is there an easier way to build the XML file? Any help is much appreciated!

Best Stian

Submitted by support on Fri, 2006-10-27 08:12

Hello Stian,

Firsly, the XML you posted is not valid - it has an error on each of the AGE elements, as they use the closing tag NAME, and so the Parser would not work on this file. The corrected version is as follows:

<members>
  <team1>
    <member id="7">
      <name>Hildegunn Lie</name>
      <age>34</age>
    </member>
    <member id="4">
      <name>Elin Vevle</name>
      <age>28</age>
    </member>
    <member id="8">
      <name>Sigrun Bruget</name>
      <age>33</age>
    </member>
  </team1>
  <team2>
    <member id="2">
      <name>Stian Totland</name>
      <age>32</age>
    </member>
    <member id="9">
      <name>Rune Vedvik</name>
      <age>27</age>
    </member>
  </team2>
</members>

Now, to process this in the way you require will involve 2 separate calls to MagicParser_parse(), because first you want to extract all the Team 1 Memebers, and secondly you want to extract the Team 2 members. The Magic Parser format strings required for these are as follows:

xml|MEMBERS/TEAM1/MEMBER/

and

xml|MEMBERS/TEAM2/MEMBER/

I've written a demo to process this XML file and generate the output you require. To run this, save your XML as teams.xml in the same directory as this script:

View Output

Here's the source:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    print 
"<li>".$record["NAME"]."</li>";
  }
  print 
"<ul>";
  print 
"<li>Team 1</li>";
  print 
"<ul>";
  
MagicParser_parse("teams.xml","myRecordHandler","xml|MEMBERS/TEAM1/MEMBER/");
  print 
"</ul>";
  print 
"<li>Team 2</li>";
  print 
"<ul>";
  
MagicParser_parse("teams.xml","myRecordHandler","xml|MEMBERS/TEAM2/MEMBER/");
  print 
"</ul>";
  print 
"</ul>";
?>

Hope this helps!
Cheers,
David.

Submitted by Stian on Fri, 2006-10-27 08:33

David, thanks for your reply. The problem is that I don't know how many teams there are. I guess I could start it of with a function called "getTeams", and in that function call "getMembers" for each team returned? Would that strain the server, with all these calls to the function?

Best, Stian

PS. And by the way, your HTML output is not correct since you close the LI after Team 1 before starting a new UL. The Team 1 LI should be closed AFTER the second UL.

Submitted by Stian on Fri, 2006-10-27 13:49

Hm, I've tried my own solution, to no avail. Any hints is much appreciated!

Best, Stian

Submitted by support on Fri, 2006-10-27 15:25

Hello Stian,

I think one solution might be to create a loop that constructs the format string for each team, and continues until there are no more teams. In this example; the team members are loaded into an array rather than printed out in your myRecordHandler function. This is because you don't want to print the Team heading (e.g. Team 2) until you know that there are members; and you don't know this until after you have tried to parse the feed for that team.

Have a try with this code - View Output

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    global 
$membersNames;
    
$membersNames[] = $record["NAME"];
  }
  print 
"<ul>";
  
$team 0;
  do {
    
$team++;
    
$membersNames = array();
    
$formatString "xml|MEMBERS/TEAM".$team."/MEMBER/";
    
MagicParser_parse("teams.xml","myRecordHandler",$formatString);
    if (
count($membersNames))
    {
      print 
"<li>";
      print 
"Team ".$team;
      print 
"<ul>";
      foreach(
$membersNames as $name) print "<li>".$name."</li>";
      print 
"</ul>";
      print 
"</li>";
    }
  } while(
count($membersNames));
  print 
"</ul>";
?>

Hope this helps,
Cheers,
David.