Jump to content

PHP5 XML Help


chuy08

Recommended Posts

Given the following XML Doc

 

<Consumer>

      <Programs>

            <Program>

                  <Title>30 Year</Title>

                  <author>Juan Rodriguez</author>

                  <publisher>abc</publisher>

            </Program>

            <Program>

                  <Title>20 Year</Title>

                  <author>Albert Rodriguez</author>

                  <publisher>abc</publisher>

            </Program>

            <Program>

                  <Title>10 Year</Title>

                  <author>Jose Guzman</author>

                  <publisher>xyz</publisher>

            </Program>

 

How could I construct a table like the following:

 

Title    | Author              |

 

      ABC Publishing          |

30 Year | Juan Rodriguez  |

20 Year | Albert Rodriguez |

 

      XYZ Publishing

10 Year | Jose Guzman    |

 

 

Currently I can build a dynamic table using the following but I can't figure out how to only print out the publisher once as a heading for all the products that match it, and then when a new publisher is detected print a new header and output all matching published products beneath that.

 

$file = 'results.xml';

 

// Loading the results into a new DomDocument

        $dom1 = new DOMDocument('1.0', 'ISO-8859-1');

        $dom1->load($file, LIBXML_NOBLANKS);

 

<?php

        echo("<TR>");

        echo("<TH></TH>");

        echo("<TH>Title</TH>");

        echo("<TH>Author</TH>");

        echo("<TH>Publisher</TH>");

 

        $root = $dom1->documentElement;

 

        $title = $dom1->getElementsByTagName("Title");

        $author = $dom1->getElementsByTagName("Author");

        $publisher = $dom1->getElementsByTagName("Publisher");

 

        $length = $title->length;

        for ($x=0; $x < $length; $x++) {

 

 

        echo "<TR><TD>".$title->item($x)->nodeValue."</TD>";

        echo "<TD>".$author->item($x)->nodeValue."</TD>";

        echo "<TD>".$publisher->item($x)->nodeValue."</TD></TR>";

 

The above code actually outputs

 

Title    |  Author        |  Publisher

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.