Cam
Cam

Reputation: 1902

creating a php file that displays xml values

I have my xml file which works perfectly and is displaying correctly, now I am creating a php program that will allow ease to update it quickly and efficiently, here is my php script right now. MY PROBLEM: IT shows only one record currently in each set, it doesnt show the entire record. Below my php is a small sample of xml file. Thanks! Here is a sample http://codepad.viper-7.com/7RznW7

<?php 
$doc = new DOMDocument(); 
$doc->load( 'menu-1a.xml' ); 


$allmenu = $doc->getElementsByTagName( "menu" ); 
foreach( $allmenu as $themenu ) 
    {
    $head = $themenu->getElementsByTagName( "menuhead" ); 
    $menuhead = $head->item(0)->nodeValue; 

            $menunames = $themenu->getElementsByTagName( "menuname" ); 
            $menuname = $menunames->item(0)->nodeValue; 

            $desc= $themenu->getElementsByTagName( "menudesc" ); 
            $menudesc= $desc->item(0)->nodeValue; 

            $price = $themenu->getElementsByTagName( "price" ); 
            $price = $price->item(0)->nodeValue; 

            $info2 = $themenu->getElementsByTagName( "price2des" ); 
            $price2des = $info2->item(0)->nodeValue; 

            $cost2 = $themenu->getElementsByTagName( "price2" ); 
            $price2 = $cost2->item(0)->nodeValue; 

            echo " <h3>$menuhead</h3><b>$menuname - $menudesc - $price -     $price2des - $price2 \n</b><br>";

    }
?>

--XML FILE--

<main>
    <menu id="appet">
    <menuhead>
    Appetizers
    </menuhead>
    <menuitem>
        <menuname>
        Cheese Stick
        </menuname>
        <menudesc>
        (6 Sticks)
        </menudesc>
        <price>
        $7
        </price>
        <price2des>
        </price2des>
        <price2>
        </price2>
    </menuitem>

--NEW CODE! WORKS -- load( 'menu-1a.xml' );

$allmenu = $doc->getElementsByTagName( "menu" ); 
foreach( $allmenu as $themenu ) {
$head = $themenu->getElementsByTagName( "menuhead" ); 
$menuhead = $head->item(0)->nodeValue; 
print "$menuhead";
foreach ($themenu->getElementsByTagName('menuitem') as $menuitem) {
    $menuname = $menuitem->getElementsByTagName( "menuname" )->item(0)->nodeValue;
    $menudesc = $menuitem->getElementsByTagName( "menudesc" )->item(0)->nodeValue;
    $price = $menuitem->getElementsByTagName( "price" )->item(0)->nodeValue;
    $price2des = $menuitem->getElementsByTagName( "price2des" )->item(0)->nodeValue;
    $price2 = $menuitem->getElementsByTagName( "price2" )->item(0)->nodeValue;
    print "<b>$menuname - $menudesc - $price -     $price2des - $price2 \n</b><br>";
    }
}
?>

Upvotes: 0

Views: 187

Answers (2)

complex857
complex857

Reputation: 20753

You want to loop over the <menuitem>s too inside the "menu" loop, and search for the individual <menuitem>'s childnodes for prizes and such. Something like this:

foreach( $allmenu as $themenu ) {
    $head = $themenu->getElementsByTagName( "menuhead" ); 
    $menuhead = $head->item(0)->nodeValue; 
    print $menuhead;
    foreach ($themenu->getElementsByTagName('menuitem') as $menuitem) {
        $menuname = $menuitem->getElementsByTagName( "menuname" )->item(0)->nodeValue;
        // ... the others
        print <b>$menuname - $menudesc - $price -     $price2des - $price2 \n</b><br>";
    }

Upvotes: 1

hakre
hakre

Reputation: 198219

It only shows one entry per each section because you have written the code that way.

You always access the first element only, like in $menunames->item(0).

If you instead access all child elements - not only the first one - and you output them, your problem is solved.

Please see DOMElement::getElementsByTagName, it contains an example how you can do that.

Upvotes: 0

Related Questions