Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2009
    Posts
    144
    Thanks
    3
    Thanked 20 Times in 20 Posts

    Getting an array into an array

    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
    Code:
    $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
    Code:
    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

  • #2
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by itxtme View Post

    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.

    PHP Code:
    $COUNTER=0

    foreach( $data as $row )  

    $loop['$COUNTER']
    $COUNTER=$COUNTER+1
        

    Leonard Whistler


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •