Hello Kirupans!
I have an issue with some PHP that I’m developing. It’s job is to load an XML file with simplexml_load_file(), then parse the info apart into variables, and finally, display the data for every record in its own <div>.
Here’s the code for popping open the XML file and turning the important nodes into variables:
<?php
$invxml = simplexml_load_file('inventory.xml');
$make = $invxml->cpo->vehicle->make;
$year = $invxml->cpo->vehicle->year;
$model = $invxml->cpo->vehicle->model;
$type = $invxml->cpo->vehicle->type;
$trans = $invxml->cpo->vehicle->trans;
$price = $invxml->cpo->vehicle->price;
$mileage = $invxml->cpo->vehicle->mileage;
$extColor = $invxml->cpo->vehicle->extColor;
$intColor = $invxml->cpo->vehicle->intColor;
$image = $invxml->cpo->vehicle->image;
?>
And here’s a small sample of the XML file I’m using:
<?xml version="1.0" encoding="utf-8"?>
<cpo>
<vehicle vin="12345678" stockNumber="1009">
<make>Mercedes Benz</make>
<year>2003</year>
<model>C320W</model>
<type>4 Door Sedan</type>
<trans>Automatic</trans>
<price>25990</price>
<mileage>44724</mileage>
<extColor>Bordeaux Red</extColor>
<intColor>Ash Leather</intColor>
<image>C320.jpg</image>
</vehicle>
<vehicle vin="98765432" stockNumber="1056">
<make>Mercedes Benz</make>
<year>2003</year>
<model>CL500C</model>
<type>2 Door Coupe</type>
<trans>Manual</trans>
<price>61991</price>
<mileage>31015</mileage>
<extColor>Black</extColor>
<intColor>Charcoal Leather</intColor>
<image>CL500.jpg</image>
</vehicle>
...
And finally, here’s a sample of the code I’m using to display the output of the XML file (I’m not using all of the above variables yet):
<div id="cpoList">
<?php
// Loops the XML output for each vehicle
**foreach ($invxml->cpo->vehicle as $vehicle) {**
echo "<div class='toggler lineItem'>";
echo "<span class='basic'>" . $year . "</span>";
echo "<span class='price'>" . $price . "</span>";
echo "<span class='color'>" . $extColor . "</span>";
echo "<span class='thumb'><img src='" . $image . "'/></span>";
echo "</div>";
echo "<div class='element lineDetail'>";
echo "</div>";
}
?>
</div>
For some reason, every time start this page up, instead of getting my inventory all nice and pretty, I get the following error (I’ve bolded the allegedly problematic line in the code above):
Warning: Invalid argument supplied for foreach() in C:\wamp\www\roots\sandbox1\public_html\mbencino\cpo_css.php on line **44
**I’m just not sure why she ain’t workin’! If you all have any insight or if you find something I didn’t, let me know!
Cheers!