docpepper Posted February 9, 2008 Share Posted February 9, 2008 Hi, I am trying to achieve something here with XML but am a bit stuck, I hope it is just a bit of misplaced code! What I am wanting to do is have some projects in an xml file then echo them out using php ( Managed that successfully) But what I also want to do is get php to automatically count the projects and echo the ordered number next to each....for eg. 1. Project 1 2. Project 2 3. Project 3 ... and so on. Code so far XML: <?xml version="1.0" encoding="utf-8"?> <webprojects> <project> <name>Project 1</name> <date>Jan 2008</date> <url>work/image1.jpg</url> <id>P1</id> <images>image1.jpg</images> <description>Description Here </description> </project> <project> <name>Project 2</name> <date>Jan 2008</date> <url>work/image2.jpg</url> <id>P2</id> <images>image21.jpg</images> <description>Description Here </description> </project><project> <name>Project 3</name> <date>Jan 2008</date> <url>work/image3.jpg</url> <id>P3</id> <images>image3.jpg</images> <description>Description Here </description> </project> </webprojects> PHP so Far: <?php if (! ($xmlparser = xml_parser_create()) ) { die ("Cannot create parser"); } $current = ""; function start_tag($parser, $name, $attribs) { for($i=0; $i < count($name); $i++) global $current; $current = $name; if ($name == "WEBPROJECTS") { echo "<ul id=\"webprojects\">"; } if ($name == "PROJECT") { echo "<li> <ul class=\"webnav\"><li class=\"left\">" . $i . "</li>"; } if ($name == "NAME") { echo "<li class=\"name\"><h4>"; } if ($name == "DATE") { echo "<li class=\"date\">"; } if ($name == "URL") { echo "<p class=\"web_header\"> <a href=\""; } if ($name == "ID") { echo "<div class=\"web_imgs\" id=\""; } if ($name == "IMAGES") { echo "<img src=\"work/"; } if ($name == "DESCRIPTION") { echo "<p class=\"web_comment hide\">"; } if ($name == "STRONG") { echo "<b>"; } if ($name == "SPAN") { echo "<span>"; } } function end_tag($parser, $name) { if ($name == "DESCRIPTION") { echo "</p> <div class=\"web_footer\"><a class=\"btn_comment\" title=\" :: \"></a></div> </div>"; } if ($name == "IMAGES") { echo "\" alt=\"\" width=\"500px\" height=\"300px\" /><br /> </div>"; } if ($name == "ID") { echo "\">"; } if ($name == "URL") { echo "\" rel=\"lightbox\">View larger</a><img alt=\"\" src=\"images/spanner.gif\" /> </p>"; } if ($name == "DATE") { echo "</li> <li class=\"right\"><!--spanner--></li> </ul> <div class=\"web_content\">"; } if ($name == "NAME") { echo "</h4></li>"; } if ($name == "PROJECT") { echo "</li>"; } if ($name == "WEBPROJECTS") { echo "</ul>"; } if ($name == "STRONG") { echo "</b>"; } if ($name == "SPAN") { echo "</span>"; } } function tag_contents($parser, $data) { global $current; if ($current == "WEBPROJECTS") { echo $data; } if ($current == "PROJECT") { echo $data; } if ($current == "NAME") { echo $data; } if ($current == "DATE") { echo $data; } if ($current == "URL") { echo $data; } if ($current == "ID") { echo $data; } if ($current == "IMAGES") { echo $data; } if ($current == "DESCRIPTION") { echo $data; } if ($current == "STRONG") { echo $data; } if ($current == "SPAN") { echo $data; } } xml_set_element_handler($xmlparser, "start_tag", "end_tag"); ?> <?php xml_set_character_data_handler($xmlparser, "tag_contents"); ?> <?php $filename = "xml/data.xml"; if (!($fp = fopen($filename, "r"))) { die("cannot open ".$filename); } ?> <?php while ($data = fread($fp, 4096)){ $data = eregi_replace(">"."[[:space:]]+"."< ",">< ",$data); if (!xml_parse($xmlparser, $data, feof($fp))) { $reason = xml_error_string(xml_get_error_code($xmlparser)); $reason .= xml_get_current_line_number($xmlparser); die($reason); } } xml_parser_free($xmlparser); fclose($fp); ?> I am calling the count with $i but all its generating is the number 1 for each project rather than counting up. I have a feeling its because it is in the first function and only counting the 1 project each time the function is run rathern than counting the whole thing... Any ideas on moving or editing it would be awesome cheers Quote Link to comment https://forums.phpfreaks.com/topic/90208-counting-xml-nodes-with-php/ Share on other sites More sharing options...
Daniel0 Posted February 9, 2008 Share Posted February 9, 2008 Use SimpleXML instead. <?php $xml = new SimpleXMLElement('xml/data.xml', null, true); $projects = $xml->xpath('project'); $i = 0; foreach($projects as $project) { $i++; // do stuff } ?> Quote Link to comment https://forums.phpfreaks.com/topic/90208-counting-xml-nodes-with-php/#findComment-462798 Share on other sites More sharing options...
docpepper Posted February 10, 2008 Author Share Posted February 10, 2008 Thanks for your reply Daniel0, I think my main issue is actually where to place the code I have written rather than a full re-write as I am not to familiar with SimpleXML. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/90208-counting-xml-nodes-with-php/#findComment-463168 Share on other sites More sharing options...
Daniel0 Posted February 10, 2008 Share Posted February 10, 2008 I'd suggest to make it use SimpleXML. It's much easier and it'll be easier for you to maintain. The current code is quite a mess. Quote Link to comment https://forums.phpfreaks.com/topic/90208-counting-xml-nodes-with-php/#findComment-463174 Share on other sites More sharing options...
docpepper Posted February 10, 2008 Author Share Posted February 10, 2008 Ok I will give it a go thanks anyway. Quote Link to comment https://forums.phpfreaks.com/topic/90208-counting-xml-nodes-with-php/#findComment-463190 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.