# My PHP won't play with my XML

**URL:** <https://forum.kirupa.com/t/my-php-wont-play-with-my-xml/236688>\
**Category:** programming\
**Created:** [August 22, 2007, 9:31pm UTC](https://forum.kirupa.com/t/my-php-wont-play-with-my-xml/236688 "2007-08-22T21:31:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![camargo](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/camargo/32/1363_2.png) [@camargo](https://forum.kirupa.com/u/camargo)\
**Post date:** [August 22, 2007, 9:31pm UTC](https://forum.kirupa.com/t/my-php-wont-play-with-my-xml/236688/1 "2007-08-22T21:31:07Z")

</div>

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:

```auto
<?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:

```auto
<?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_):

```auto
    <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!
