Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Nesting a foreach loop

    I am parsing an xml file and am attempting to have the "AditionalPhoto" show as thumbnails of the bunch of images given in the xml file. However, with my code below, I am getting the error "Notice: Undefined variable: AditionalPhoto in file.php on line 91

    Warning: Invalid argument supplied for foreach() in file.php on line 91.

    Code:
     <?php
    
    	error_reporting(-1);
    
    
    $preowned = simplexml_load_file('xmlfile.xml');
    
    $photosHTML = '';
    foreach ($AditionalPhoto as $addphoto)
    {
        $photosHTML .= "<li>$addphoto</li>";
    }
    
    foreach ($preowned as $preownedinfo)
    {
        $price = '$' . money_format($preownedinfo->Price, 2, '.', ',');
     	
        echo "
    <div class=\"detailsTitle\">
        <a href=\"#\">{$preownedinfo->Yrs} {$preownedinfo->Make} {$preownedinfo->Model} {$preownedinfo->ExtraField->ContentEN->ExteriorColor} {$preownedinfo->ExtraField->ContentEN->Doors} Doors</a>
    </div>    
    <div>
        <div style\"float:left; width:400px;\"><img src=\"{$preownedinfo->MainPhoto}\"width=\"400\" border=\"0\" /></div>
        <div style=\"float:right;\">
            <ul>{$photosHTML}</ul>
        </div>
        <div style=\"clear:both;\">
            <div class=\"specsDetails\">
                <div class=\"specsDetailsRow\">
                    <div class=\"leftDetailsSpec\">Our Price: </div>
                    <div class=\"rightSpec\">{$price}</div>
                </div>
                <div class=\"specsDetailsRow\">
                    <div class=\"leftDetailsSpec\">Body Style: </div>
                    <div class=\"rightSpec\">{$preownedinfo->Trim}</div>
                </div>
                <div class=\"specsDetailsRow\">
                    <div class=\"leftDetailsSpec\">Status: </div>
                    <div class=\"rightSpec\">Used</div>
                </div>
                <div class=\"specsDetailsRow\">
                    <div class=\"leftDetailsSpec\">Engine: </div>
                    <div class=\"rightSpec\">{$preownedinfo->ExtraField->ContentEN->Engine}</div>
                </div>
                <div class=\"specsDetailsRow\">
                    <div class=\"leftDetailsSpec\">Transmission: </div>
                    <div class=\"rightSpec\">{$preownedinfo->ExtraField->ContentEN->Transmission}</div>
                </div>
                <div class=\"specsDetailsRow\">
                    <div class=\"leftDetailsSpec\">Ext. Colour: </div>
                    <div class=\"rightSpec\">{$preownedinfo->ExtraField->ContentEN->ExteriorColor}</div>
                </div>
                <div class=\"specsDetailsRow\">
                    <div class=\"leftDetailsSpec\">Int. Colour: </div>
                    <div class=\"rightSpec\">{$preownedinfo->ExtraField->ContentEN->InteriorColor}</div>
                </div>
                <div class=\"specsDetailsRow\">
                    <div class=\"leftDetailsSpec\">Klometres: </div>
                    <div class=\"rightSpec\">{$preownedinfo->ExtraField->ContentEN->Odometer}</div>
                </div>
                <div class=\"specsDetailsRow\">
                    <div class=\"leftDetailsSpec\">Stock Number: </div>
                    <div class=\"rightSpec\">{$preownedinfo->StockNumber}</div>
                </div>
        </div>
        <div class=\"detailsOverview\">{$preownedinfo->AdDescription}</div>
        <div class=\"detailsOverview requestInfo\">
            <a href=\"#\">Request Info</a>
        </div>
    </div>";
    
    }
    
    ?>

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    where are you populating $AditionalPhoto??
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #3
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It should be under $preownedinfo. It's one of the fields in the xml just as the others. Can I put a foreach loop within the second one?

  • #4
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    Yes, something like:

    PHP Code:
    $preowned simplexml_load_file('xmlfile.xml');


    foreach (
    $preowned as $preownedinfo)
    {
        
    $photosHTML '';
        foreach (
    $preownedinfo->AditionalPhoto as $addphoto)
        {
            
    $photosHTML .= "<li>$addphoto</li>";
        }

        
    $price '$' money_format($preownedinfo->Price2'.'',');
         
        echo 

    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •