Jump to content

Nesting a foreach loop


kawaii1001

Recommended Posts

Starting off, I am really new to php.

 

My second foreach loop is messing it all up.  There are a bunch of additional images $AdditionalImage that I need to repeat within my loop.  Once I added in the second foreach loop, it's not working.  Any help would be appreciated:

 

 <?php
    $preowned = simplexml_load_file('file.xml');
     foreach ($preowned as $preownedinfo):
    $ADID=$preownedinfo->ADID;
    $CompanyID=$preownedinfo->CompanyID;
    $Category=$preownedinfo->Category;
    $StockNumber=$preownedinfo->StockNumber;
        $Year=$preownedinfo->Yrs;
        $Make=$preownedinfo->Make;
        $Model=$preownedinfo->Model;
        $Trim=$preownedinfo->Trim;
        $Odometer=$preownedinfo->ExtraField->ContentEN->Odometer;
        $ExteriorColor=$preownedinfo->ExtraField->ContentEN->ExteriorColor;
        $InteriorColor=$preownedinfo->ExtraField->ContentEN->InteriorColor;
        $Engine=$preownedinfo->ExtraField->ContentEN->Engine;
        $Transmission=$preownedinfo->ExtraField->ContentEN->Transmission;
        $Price=$preownedinfo->Price;
        $Options=$preownedinfo->ExtraField->ContentEN->Options;
        $AdDescription=$preownedinfo->AdDescription;
        $MainPhoto=$preownedinfo->MainPhoto;
        $AdditionalPhoto=$preownedinfo->AdditionalPhoto;
        $Doors=$preownedinfo->ExtraField->ContentEN->Doors;
        $price = '$' . money_format($Price, 2, '.', ',');
    
  
    echo "<div class=\"detailsTitle\"> <a href=\"#\">$Yrs $Make $Model $ExtiorColor $Doors Doors </a></div>    
    <div>
    <div style\"float:left; width:400px;\"><img src=\"$MainPhoto\"width=\"400\" border=\"0\" /></div>
    <div style=\"float:right;\">
    <ul>";
    foreach ($AdditionalPhoto as $addphoto);
    echo "<li>$addphoto</li>";
    endforeach;
    "</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\">$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\">$Engine</div></div>
<div class=\"specsDetailsRow\"><div class=\"leftDetailsSpec\">Transmission: </div><div class=\"rightSpec\">$Transmission</div></div>
        <div class=\"specsDetailsRow\"><div class=\"leftDetailsSpec\">Ext. Colour: </div><div class=\"rightSpec\">$ExteriorColor</div></div>
<div class=\"specsDetailsRow\"><div class=\"leftDetailsSpec\">Int. Colour: </div><div class=\"rightSpec\">$InteriorColor</div></div>
<div class=\"specsDetailsRow\"><div class=\"leftDetailsSpec\">Klometres: </div><div class=\"rightSpec\">$Odometer</div></div>
<div class=\"specsDetailsRow\"><div class=\"leftDetailsSpec\">Stock Number: </div><div class=\"rightSpec\">$StockNumber</div></div>
 
</div>
 
<div class=\"detailsOverview\">
$AdDescription
</div>
    <div class=\"detailsOverview requestInfo\">
<a href=\"#\">Request Info</a>
</div>
</div>";
endforeach;
?>  
Link to comment
Share on other sites

Sorry.  I'm getting an error that says "Parse error: syntax error, unexpected T_ENDFOREACH on line 139".  I am hoping to have all of the additional images appear within their own li tags within the other loop.  There is one main image which is pulling fine, but then to the right of that, I need all of the additional images, there's about 10 for each vehicle, to show as thumbnails.  Make sense?

Link to comment
Share on other sites

 foreach ($AdditionalPhoto as $addphoto);
    echo "<li>$addphoto</li>";
    endforeach;
Wrong syntax. Either use normal {}s

 foreach ($AdditionalPhoto as $addphoto) {
    echo "<li>$addphoto</li>";
    }
or the alternative syntax which uses a colon (not a semicolon)

 foreach ($AdditionalPhoto as $addphoto):
    echo "<li>$addphoto</li>";
    endforeach;
However given that you use the alternative syntax correctly earlier I'm going to guess the semicolon is a typo.
Link to comment
Share on other sites

I think it's getting closer.  So I have this now, but the loop is stopping after the main image and description and not pulling the additional images.  Does it look like I've added something in error.

 

 <?php
 
error_reporting(-1);
 
 
    $preowned = simplexml_load_file('file.xml');
     foreach ($preowned as $preownedinfo):
    $ADID=$preownedinfo->ADID;
    $CompanyID=$preownedinfo->CompanyID;
    $Category=$preownedinfo->Category;
    $StockNumber=$preownedinfo->StockNumber;
        $Year=$preownedinfo->Yrs;
        $Make=$preownedinfo->Make;
        $Model=$preownedinfo->Model;
        $Trim=$preownedinfo->Trim;
        $Odometer=$preownedinfo->ExtraField->ContentEN->Odometer;
        $ExteriorColor=$preownedinfo->ExtraField->ContentEN->ExteriorColor;
        $InteriorColor=$preownedinfo->ExtraField->ContentEN->InteriorColor;
        $Engine=$preownedinfo->ExtraField->ContentEN->Engine;
        $Transmission=$preownedinfo->ExtraField->ContentEN->Transmission;
        $Price = number_format((float)$preownedinfo->Price, 2);
        $Options=$preownedinfo->ExtraField->ContentEN->Options;
        $AdDescription=$preownedinfo->AdDescription;
        $MainPhoto=$preownedinfo->MainPhoto;
        $AdditionalPhoto=$preownedinfo->AdditionalPhoto;
        $Doors=$preownedinfo->ExtraField->ContentEN->Doors;
    
  
    echo "<div class=\"detailsTitle\"> <a href=\"#\">$Year $Make $Model $ExteriorColor $Doors Doors </a></div>    
    <div>
    <div style\"float:left; width:400px;\"><img src=\"$MainPhoto\"width=\"400\" border=\"0\" /></div>
    <div style=\"float:right;\">";
    foreach ($AdditionalPhoto as $addphoto):
    echo "<li>$addphoto</li>";
    endforeach;
    "</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\">$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\">$Engine</div></div>
<div class=\"specsDetailsRow\"><div class=\"leftDetailsSpec\">Transmission: </div><div class=\"rightSpec\">$Transmission</div></div>
        <div class=\"specsDetailsRow\"><div class=\"leftDetailsSpec\">Ext. Colour: </div><div class=\"rightSpec\">$ExteriorColor</div></div>
<div class=\"specsDetailsRow\"><div class=\"leftDetailsSpec\">Int. Colour: </div><div class=\"rightSpec\">$InteriorColor</div></div>
<div class=\"specsDetailsRow\"><div class=\"leftDetailsSpec\">Klometres: </div><div class=\"rightSpec\">$Odometer</div></div>
<div class=\"specsDetailsRow\"><div class=\"leftDetailsSpec\">Stock Number: </div><div class=\"rightSpec\">$StockNumber</div></div>
 
</div>
 
<div class=\"detailsOverview\">
$AdDescription
</div>
    <div class=\"detailsOverview requestInfo\">
<a href=\"#\">Request Info</a>
</div>
</div>";
endforeach;
?>    
Link to comment
Share on other sites

1. Please use [ code ] tags when posting code.

 

2. You can use the syntax for foreach() as requinix showed, but (and this is just my opinion) I think it is better to use the curly braces to deliniate the start/end of the code blocks associated with the foreach loop.

 

3. The inner foreach() loop is creating the same content each time. That is a waste of resources. You should instead, run that foreach() loop first and create the content as a variable. Then use that variable inside the main foreach loop.

 

4. It's also a waste to create variables from the $preownedinfo object since you are only using them once.

 

Give this a try

 

<?php
$preowned = simplexml_load_file('file.xml');

$photosHTML = '';
foreach ($AdditionalPhoto 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>";

}

?> 
Link to comment
Share on other sites

I appreciate all the time you spent on this.  The one issue that is still appearing is it is saying "Notice: Undefined variable: AdditionalPhoto in file.php on line 91

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

 

I tried changing this part to add in "preowned" and still get the error message.

$photosHTML = '';
foreach ($preowned->$AdditionalPhoto as $addphoto)
{
    $photosHTML .= "<li>$addphoto</li>";
}
Edited by kawaii1001
Link to comment
Share on other sites

Try with the loop for the photos inside the $preownedinfo loop, and then with it looping over $preownedinfo->AdditionalPhoto:

 

<?php
$preowned = simplexml_load_file('file.xml');

foreach ($preowned as $preownedinfo)
{
    $price = '$' . money_format($preownedinfo->Price, 2, '.', ',');

    $photosHTML = '';
    foreach ($preownedinfo->AdditionalPhoto as $addphoto)
    {
        $photosHTML .= "<li>$addphoto</li>";
    }
 
    echo...
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.