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
    New Coder
    Join Date
    Sep 2010
    Posts
    35
    Thanks
    3
    Thanked 1 Time in 1 Post

    Outputting array within an array (Excel Export)

    Hi guys. I am trying to setup a script which exports data into an Excel file. I am having troubles outputting arrays. It will only output the last value of an array.

    I am currently using this PHP Excel class: http://code.google.com/p/php-excel/

    This is the part of my code which does the exporting:

    Code:
    $query = "SELECT * FROM asset WHERE operational_state = 'Production' OR operational_state = 'Planned' OR operational_state = 'Expired' OR operational_state = 'Maintenance' OR operational_state = 'Repair'";
    $myresult = QueryIntoArray($query);
    for($i=0;$i<count($myresult);$i++){
       $hostname = $myresult[$i]["hostname"];
       $description = $myresult[$i]["description"];
    }
    
    $info = array (
       1 => array ("Hostname", "Description"),
       array ($hostname, $description)
    );
    
    // generate file (constructor parameters are optional)
    $xls = new Excel_XML('UTF-8', false, 'Current Assets');
    $xls->addArray($info);
    $xls->generateXML('current-assets-test20');
    It only outputs the last value of the arrays $hostname and $description. I was playing around with the array_push() function but could not get it working properly. Any ideas how can I output whole arrays with each value in the array on a new row?

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,022
    Thanks
    15
    Thanked 239 Times in 239 Posts
    PHP Code:
    $hostname = array();$description = array();
    for(
    $i=0;$i<count($myresult);$i++){
       
    $hostname[$i] = $myresult[$i]["hostname"];
       
    $description[$i] = $myresult[$i]["description"];
    }
    Similarly for push:
    $hostname = array();$description = array();
    for(
    $i=0;$i<count($myresult);$i++){
    array_push($hostname,$myresult[$i]["hostname"]);
    array_push($description,$myresult[$i]["description"]);

    Last edited by DrDOS; 12-01-2010 at 05:05 PM.


  •  

    Posting Permissions

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