...

View Full Version : Outputting array within an array (Excel Export)



Mark91
12-01-2010, 12:05 AM
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:


$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?

DrDOS
12-01-2010, 12:37 AM
$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"]);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum