mysql to associate arrays to csv file
Posted: Mon May 25, 2009 5:51 pm
Hi:
This is what I am trying to do..
I got a mysql query that give me the data in the following format:
name|qty|week
abc|60|1
abc|20|2
abc|80|3
abc|22|4
bfg|23|1
bfg|45|2
bfg|633|3
bfg|90|4
jhg|34|1
jhg|23|2
jhg|20|3
jhg|2|4
--- Given the above result from the query how can I generate a CSV file of the following format?
name|week_1|week_2|week_3|week_4
abc|60|20|80|22
bfg|23|45|633|90
jhg|34|23|20|2
I have tried to use the following code to generate an associative array but I am not sure how to print it to generate the CSV file:
while ($row=mysql_fetch_array($result))
{
$name=$row["name"];
$qty = $row['qty'];
$week = $row["week"];
$row=array($name => array($week=>$qty));
}
Can someone please help me a little bit on how to use the $row associative array to generate the CSV file?
Thanks
Alex B
This is what I am trying to do..
I got a mysql query that give me the data in the following format:
name|qty|week
abc|60|1
abc|20|2
abc|80|3
abc|22|4
bfg|23|1
bfg|45|2
bfg|633|3
bfg|90|4
jhg|34|1
jhg|23|2
jhg|20|3
jhg|2|4
--- Given the above result from the query how can I generate a CSV file of the following format?
name|week_1|week_2|week_3|week_4
abc|60|20|80|22
bfg|23|45|633|90
jhg|34|23|20|2
I have tried to use the following code to generate an associative array but I am not sure how to print it to generate the CSV file:
while ($row=mysql_fetch_array($result))
{
$name=$row["name"];
$qty = $row['qty'];
$week = $row["week"];
$row=array($name => array($week=>$qty));
}
Can someone please help me a little bit on how to use the $row associative array to generate the CSV file?
Thanks
Alex B