0

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>";
    }
}
?>

2 Answers 2

1

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>";
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I got one other question for ya, I added <div id=$menu> to my print $menuhead ?? It doesnt create a div with id attribute in the xml. How do i go about adding this?
I'm not sure what is in the $menu variable, so i just only assume that its a DOMNode instance, and those don't convert to strings automatically so you cant just print them. I think you wanted to do $themenu->getAttribute('id') there.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.