As I have been going through the tutorials, I wanted to try and use what I'm learning.I came across the need to export values from an XML file to a CSV file. Looking at examples in the PHP manual and trying to use what I have been learning I have started the script.
The XML layout looks like this:
<?xml version="1.0" encoding="utf-8"?>
<form>
<title><![CDATA[submit Directory Profile Information]]></title>
<submissions>
<submission>
<name><![CDATA[Joe Mama]]></name>
<email><![CDATA[enter@here.com]]></email>
<address><![CDATA[1234 there]]></address>
<city><![CDATA[Thereville]]></city>
<state><![CDATA[iL]]></state>
<zip>55566</zip>
<phone><![CDATA[555-555-1234 x1234]]></phone>
<fax><![CDATA[555-555-5678]]></fax>
<website><![CDATA[http://]]></website>
</submission>
<submission>
<name><![CDATA[Test Item]]></name>
<email><![CDATA[enter@here.com]]></email>
<address><![CDATA[1234 there]]></address>
<city><![CDATA[Thereville]]></city>
<state><![CDATA[iL]]></state>
<zip>55566</zip>
<phone><![CDATA[555-555-1234 x1234]]></phone>
<fax><![CDATA[555-555-5678]]></fax>
<website><![CDATA[http://]]></website>
</submission>
</submissions>
</form>
The beginning of my code looks like this:
$file = "mystuff.xml";
$xml = simplexml_load_file($file);
//get data from XML tags
foreach($xml->submissions->submission as $item)
{
//store those in array
$basic = array(
"name" => (string)$item->name,
"email" => (string)$item->email,
"address" => (string)$item->address,
"city" => (string)$item->city,
"state" => (string)$item->state,
"zip" => (string)$item->zip,
"phone" => (string)$item->phone,
"fax" => (string)$item->fax,
"website" => (string)$item->website
);
Not very far and my first problem appears. I understand why it happens I just don't know how to fix it. For every foreach loop the result will just overwrite the the first one. So if I vardump outside the loop you get one submission, not both. I'm stuck because I can't figure out how to get the results from each loop outside the loop, if that makes sense. Either its not very easy to do, or I am just missing something simple, either way if I can get some help with that I can move one to the next part.
Thanks for the time,
benjmain