kawaii1001 Posted August 28, 2013 Share Posted August 28, 2013 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; ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted August 28, 2013 Share Posted August 28, 2013 it's not working.Saying that doesn't tell us anything. You have to actually explain how it's not working. What is it doing? What did you expect it to do? What have you tried to do to fix it? Quote Link to comment Share on other sites More sharing options...
kawaii1001 Posted August 28, 2013 Author Share Posted August 28, 2013 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? Quote Link to comment Share on other sites More sharing options...
requinix Posted August 28, 2013 Share Posted August 28, 2013 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. Quote Link to comment Share on other sites More sharing options...
kawaii1001 Posted August 28, 2013 Author Share Posted August 28, 2013 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; ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 28, 2013 Share Posted August 28, 2013 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
kawaii1001 Posted August 29, 2013 Author Share Posted August 29, 2013 (edited) 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 August 29, 2013 by kawaii1001 Quote Link to comment Share on other sites More sharing options...
requinix Posted August 29, 2013 Share Posted August 29, 2013 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... Quote Link to comment Share on other sites More sharing options...
kawaii1001 Posted August 29, 2013 Author Share Posted August 29, 2013 No errors, but it's not showing anything inside the <ul>. Quote Link to comment Share on other sites More sharing options...
requinix Posted August 29, 2013 Share Posted August 29, 2013 By the way, what is the XML you're reading from? Quote Link to comment Share on other sites More sharing options...
kawaii1001 Posted August 29, 2013 Author Share Posted August 29, 2013 Thank you! I just figured it out. I was missing AditionalPhotos->AditionalPhoto. Thanks for all of your time! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.