simzam Posted December 16, 2010 Share Posted December 16, 2010 Hi I'm trying to count XML elements and generate table row and cell as per result. XML entries generated by users what I'm trying to do is auto generate table rows and cols and paging them bec if XML file get heavier its hard to get results it slow down processing that's why i appreciate paging My XML: <note> <a1>Keep holding On - Avril Lavigne </a1> <a2>Miley Cyrus - Party In The U.S.A </a2> <a3>Swift Taylor - Breath </a3> <a4>Coldplay - The Hardest Part </a4> <a5>P!nk - Who Knew </a5> <a6>Eminem - Love The Way You Lie ft. Rihanna </a6> <a7>Nelly Furtado - Say It Right </a7> <a8>The Black Eyed Peas - Meet Me Halfway </a8> <a9>Justin Bieber - Baby ft. Ludacris </a9> <a10>N.E.R.D. - Hot-n-Fun (Official Version) ft. Nelly Furtado </a10> </note> Quote Link to comment https://forums.phpfreaks.com/topic/221889-count-xml-child-elements-and-generate-html-table/ Share on other sites More sharing options...
Maq Posted December 16, 2010 Share Posted December 16, 2010 You can use - http://us.php.net/simplexml - to grab the child nodes, and loop through them to formulate the table. Quote Link to comment https://forums.phpfreaks.com/topic/221889-count-xml-child-elements-and-generate-html-table/#findComment-1148219 Share on other sites More sharing options...
simzam Posted December 16, 2010 Author Share Posted December 16, 2010 Hi I tried my self when i hit http://myurl/test.php?xml=note result not separating all data shows in same cell my PHP: <?php error_reporting(-1); if (!empty($_GET['xml'])) { $xml = new DOMDocument(); $xml->load("link.xml"); $nodes = $xml->getElementsByTagName($_GET['xml']); echo <<<EOF <table border = 1> <tr> <th>title</th> </tr> EOF; foreach($nodes as $node) { echo <<<EOF <tr> <td>$node->textContent.</td> </tr> EOF; } echo '</table>'; } ?> my XML: <note> <a1>Keep holding On - Avril Lavigne </a1> <a2>Miley Cyrus - Party In The U.S.A </a2> <a3>Swift Taylor - Breath </a3> <a4>Coldplay - The Hardest Part </a4> <a5>P!nk - Who Knew </a5> <a6>Eminem - Love The Way You Lie ft. Rihanna </a6> <a7>Nelly Furtado - Say It Right </a7> <a8>The Black Eyed Peas - Meet Me Halfway </a8> <a9>Justin Bieber - Baby ft. Ludacris </a9> <a10>N.E.R.D. - Hot-n-Fun (Official Version) ft. Nelly Furtado </a10> </note> Quote Link to comment https://forums.phpfreaks.com/topic/221889-count-xml-child-elements-and-generate-html-table/#findComment-1148288 Share on other sites More sharing options...
Maq Posted December 16, 2010 Share Posted December 16, 2010 Close, I adjusted it a bit for you, try this: ini_set ("display_errors", "1"); error_reporting(E_ALL); if(!empty($_GET['xml']) && isset($_GET['xml'])) { $xml = $_GET['xml']; $dom = new DOMDocument; $dom->load('link.xml'); $xpath = new DOMXPath($dom); $xpath->registerNamespace("php", "http://php.net/xpath"); $nodes = $xpath->query("/{$xml}/*"); echo "</pre> <table border="'1'">title"; foreach ($nodes as $node) { echo ""; echo "" . $node->nodeValue . ""; echo ""; } echo "</table>";<br>}<br>else<br>{<br> echo "You did not provide a value";<br>}<br><br>?&g Quote Link to comment https://forums.phpfreaks.com/topic/221889-count-xml-child-elements-and-generate-html-table/#findComment-1148312 Share on other sites More sharing options...
simzam Posted December 16, 2010 Author Share Posted December 16, 2010 will this works like charm but i don't understand DOMXPath() and registerNamespace kindly help me to understand this explain Quote Link to comment https://forums.phpfreaks.com/topic/221889-count-xml-child-elements-and-generate-html-table/#findComment-1148323 Share on other sites More sharing options...
Maq Posted December 16, 2010 Share Posted December 16, 2010 will this works like charm but i don't understand DOMXPath() and registerNamespace kindly help me to understand this explain XPath is an XML path language that allows you to select nodes and includes some logic and conditions. registerNamespace is exactly what is sounds like, registering the xpath namespace. Quote Link to comment https://forums.phpfreaks.com/topic/221889-count-xml-child-elements-and-generate-html-table/#findComment-1148330 Share on other sites More sharing options...
simzam Posted December 16, 2010 Author Share Posted December 16, 2010 thanks ! how to load multi category on different rows like i tried but no luck need your help ! title1 | title2 | | | <note> <source category= "links"> <b1>song 1 </b1> <b2> song 2 </b2> <b3>song 3 </b3> <b4> song 4 </b4> <b5> song 5 </b5> <b6> song 6 </b6> <b7> song 7 </b7> <b8>song 8 </b8> <b9>song 9 </b9> <b10> song 10 </b10> </source> <source category= "title"> <a1>Keep holding On - Avril Lavigne </a1> <a2>Miley Cyrus - Party In The U.S.A </a2> <a3>Swift Taylor - Breath </a3> <a4>Coldplay - The Hardest Part </a4> <a5>P!nk - Who Knew </a5> <a6>Eminem - Love The Way You Lie ft. Rihanna </a6> <a7>Nelly Furtado - Say It Right </a7> <a8>The Black Eyed Peas - Meet Me Halfway </a8> <a9>Justin Bieber - Baby ft. Ludacris </a9> <a10>N.E.R.D. - Hot-n-Fun (Official Version) ft. Nelly Furtado </a10> </source> </note> Quote Link to comment https://forums.phpfreaks.com/topic/221889-count-xml-child-elements-and-generate-html-table/#findComment-1148340 Share on other sites More sharing options...
Maq Posted December 16, 2010 Share Posted December 16, 2010 I put them in 2 separate tables cause I hate dealing with them. $i = 1; foreach ($nodes as $node) { echo "</pre> <table border="'1'">title"; $nodes2 = $xpath->query("/note/source[$i]/*"); foreach($nodes2 as $node2) { echo ""; echo "" . $node2->nodeValue . ""; echo ""; } $i++; echo "</table>";<br> } Quote Link to comment https://forums.phpfreaks.com/topic/221889-count-xml-child-elements-and-generate-html-table/#findComment-1148383 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.