...

View Full Version : Getting an array into an array



itxtme
08-10-2012, 05:12 AM
I am trying to create a CSV export from my query using fputcsv. I have some code that works well in creating the CSV but I need to get my data format right so it succesfully creates it. That format is an array containing the result sets in their own arrays

I can get a single row into the CSV file, or I can get all of the result rows in by manually writing the amount of arrays to call, however live the code wont know how many. My thoughts are I will need to use a count and can get it to repeat. However I cant run a loop within the array. I basically need a push in the right direction as to what the best approach is.

Loop thats getting the mysql results

$result = mysql_query("SELECT HR.order_id, HR.account_id, HR.child_id, HR.order_date, HR.pay_type, HR.pay_ref, HR.items, HR.order_total, HA.full_name, HR.pay_status, HR.order_status, AC.first_name, AC.last_name, PA.f_name FROM {$sql_instance}register AS HR INNER JOIN all_child AS AC ON HR.child_id=AC.child_id INNER JOIN {$sql_instance}avaliable AS HA ON HA.hp_location=HR.hp_location LEFT JOIN {$sql_instance}pay_options AS PA ON PA.id=HR.order_status WHERE HR.archived = '0' AND HR.admin_actioned='0' ORDER BY HR.order_date DESC") or die(mysql_error());
$count = 0;
while($row = mysql_fetch_array( $result )) {
$loop[] = array( "{$row['order_id']}","{$row['account_id']}","{$row['child_id']}","{$row['order_date']}","{$row['pay_type']}","{$row['pay_ref']}","{$row['items']}","{$row['order_total']}","{$row['full_name']}","{$row['pay_status']}","{$row['order_status']}","{$row['first_name']}","{$row['last_name']}","{$row['f_name']}");
}


Code that creates the CSV

require('test2.php');
$data = array(
$loop[0],
$loop[1],
$loop[2]

);

foreach( $data as $row )
{
fputcsv($outstream, $row, ',', '"');
}

fclose($outstream);

This code is working, but I cant manually write how many $loop[]'s there might be as I dont know, so the question is how do I prepare the arrays prior to creating the 'master' $data array??? Any help is appreciated

Len Whistler
08-10-2012, 06:51 AM
This code is working, but I cant manually write how many $loop[]'s there might be as I dont know, so the question is how do I prepare the arrays prior to creating the 'master' $data array??? Any help is appreciated

How about something like the code below. It's just a quick untested example, syntax is probably wrong but it should point you in the right direction.


$COUNTER=0

foreach( $data as $row )
{
$loop['$COUNTER']
$COUNTER=$COUNTER+1

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum