Forum Moderators: coopster

Message Too Old, No Replies

Three csv files to one xml

output coming from new website - here are my thoughts

         

mattyb515

1:19 pm on Apr 10, 2007 (gmt 0)

10+ Year Member



Ok, so here's what we've got. A new website for our company in which customers can place orders and they eventually get output into three csv files. The common thread between the three is the order number. We're trying to use those three files and link it with our office system. We're just trying to eliminate hand entering each order into our office system. So I was thinking that I could write a script to read in all three files store them in an array and then output them to xml.

Our office system supports various types of files - csv, xml, and tab delimited. I know the xml tags that are used by the system so I was thinking xml would be the easiest. If my thinking is off on this please let me know or if anyone can point me in the right direction I would really appreciate it. If I'm on the right track and just need to watch out for pitfalls, a heads up would be greatly appreciated. Thanks in advance for all of the help. I love this site as everyone has helped out immensely in the past.

Oh yeah, if anyone needs any more info. just let me know!

eelixduppy

2:27 pm on Apr 10, 2007 (gmt 0)



You can use file() [php.net] to get the contents of each of the files as an array, with each line as a different element of the array. Then, you are going to have to loop through the array, explode() [php.net] each element, and write [php.net] it to the new XML file appropriately. Don't write it individually, however. Append the strings to a buffer variable (ie $buffer) and then write it all at once.

try to get some code and we can go from there :)

Good luck!

mattyb515

2:39 pm on Apr 10, 2007 (gmt 0)

10+ Year Member



Thanks for the advice. I'll grab/write some code and then post back. Instead of using file() would it be easier to use fgetcsv()?

eelixduppy

3:44 pm on Apr 10, 2007 (gmt 0)



If you want to use that, it will work just the same for the most part :)

mattyb515

2:08 pm on Apr 12, 2007 (gmt 0)

10+ Year Member



Here's some of the code I've come up with. Please let me know if I'm on the right track.

$DOCUMENT_ROOT = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];
$fp = fopen("$DOCUMENT_ROOT/ncorders/order.csv", "r");

while(!feof($fp))
{
$order = fgetcsv($fp, ",");
$exp_order = explode(",", $order);
}

It seems to be getting stuck in this loop. I'm not too sure why. Any thoughts?

mattyb515

3:51 pm on Apr 12, 2007 (gmt 0)

10+ Year Member



Ok so I've found this function and I just need to modify it:

$DOCUMENT_ROOT = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];
$file = "$DOCUMENT_ROOT/ncorders/order.csv";

function parse_csv_file($file, $columnheadings = true, $delimiter = ',', $enclosure = '\n') {
$row = 1;
$rows = array();
$fp = fopen("$file", "r");

while (($data = fgetcsv($fp, 0, $delimiter, $enclosure )))
{
if ($columnheadings == false && $row == 1) {
$headingTexts = $data;
}
elseif ($columnheadings == true) {
foreach ($data as $key => $value) {
unset($data[$key]);
$data[$headingTexts[$key]] = $value;
}
$rows[] = $data;
}
else {
$rows[] = $data;
}
$row++;
}
return $rows;
}

Does this look like a viable csv parser?

mattyb515

5:53 pm on Apr 12, 2007 (gmt 0)

10+ Year Member



Ok so I've been totally over-thinking this. Came up with a much simpler piece of code:

$file = "$DOCUMENT_ROOT/ncorders/order_variation.csv";
$row2 = 1;
$handle2 = fopen("$file", "r");
while (($data = fgetcsv($handle2, 1000, ","))!== FALSE)
{
$num = count($data);
$row2++;
for ($c = 0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle2);

Just need to throw an if statement to compensate for the header row and I should be golden.