PHP Classes

column headings problem

Recommend this page to a friend!

      MS-Excel Stream Handler  >  All threads  >  column headings problem  >  (Un) Subscribe thread alerts  
Subject:column headings problem
Summary:I get sequential numbers for the first row of data.
Messages:3
Author:Ray
Date:2009-12-24 21:57:47
Update:2010-01-30 01:18:26
 

  1. column headings problem   Reply   Report abuse  
Picture of Ray Ray - 2009-12-24 21:57:47
Hello.

I am using your stream handler. It works well and I am satisfied to continue using it.

I have a small problem that I have tried to solve. But I am not a very competent php programmer. The top row of the generated spreadsheet is a list of sequential numbers beginning at 0 and continuing across the top row for the extent of the number of columns I have in my array.

I loaded the column headings into the 0th row of the array in an attempt to mitigate this, but the data shows up on row #1

Here's how I loaded the array from a sql query:

=====================================================
$rs = $conn->Execute($sql);
$hd=0;
$row = array();

while (!$rs->EOF) {
$col = array();
if ($hd==0)
{
$row[0][0] = "Log ID";
$row[0][1] = "Location";
$row[0][2] = "Facility";
$row[0][3] = "Short Name";
$row[0][4] = "Equip ID";
$row[0][5] = "Interval";
$row[0][6] = "Earliest";
$row[0][7] = "Scheduled";
$row[0][8] = "Latest";
$row[0][9] = "Order";
$row[0][10] = "Paragraph";
$row[0][11] = "Days Until Scheduled Date";
$row[0][12] = "Days Until Window Closes";
$hd++;
}
for ($i=0; $i<14; $i++) {
try {
$value = $rs->fields[$i]->value;

if (trim($value) != "")
{
// check if its a string
if (gettype($value) !== 'string')
{
if (settype($value,"string"))
{
array_push($col, $value);
}
}
else
{
array_push($col, $value);
}
}
else
{
array_push($col, "");
}
}
catch (exception $e) {
break;
}
}
array_push($row, $col);
$rs->MoveNext();
}
=====================================================

And here is the use of your 'excel.php' include:

=====================================================
require_once "excel.php";
$export_file = "xlsfile://xampplite/htdocs/tools/example.xls";
$fp = fopen($export_file, "wb");
if (!is_resource($fp))
{
die("Cannot open $export_file");
}
fwrite($fp, serialize($row));
fclose($fp);
=====================================================

Any help would be appreciated.

Thanks,
Ray

  2. Re: column headings problem   Reply   Report abuse  
Picture of Stephen Stephen - 2010-01-28 19:25:19 - In reply to message 1 from Ray
I just had the same problem. I noticed after printing the array back before xls-ing it that the keys for the header row are 0,1,2.... Just a guess, but I assumed that's where the numbers were coming from. I just did an array slice on the array to remove the top row before serialize and that worked for me.

$row=array_slice($row,1)

  3. Re: column headings problem   Reply   Report abuse  
Picture of Ray Ray - 2010-01-30 01:18:26 - In reply to message 2 from Stephen
Figured it out too. This class needs an associative array. I rewrote the code thus:
===========================================================

// Now let's dump the data to an excel file

$assoc = array();

for ($k = 0; $k <= $i; $k++) {

$assoc[$k] = array();

$assoc[$k]["Log ID"] = $row[$k][0];

$assoc[$k]["Location"] = $row[$k][1];

$assoc[$k]["Facility"] = $row[$k][2];

$assoc[$k]["Short Name"] = $row[$k][3];

$assoc[$k]["Equip ID"] = $row[$k][4];

$assoc[$k]["Interval"] = $row[$k][5];

$assoc[$k]["Earliest"] = $row[$k][6];

$assoc[$k]["Scheduled"] = $row[$k][7];

$assoc[$k]["Latest"] = $row[$k][8];

$assoc[$k]["Order"] = $row[$k][9];

$assoc[$k]["Paragraph"] = $row[$k][10];

$assoc[$k]["Days Until Scheduled Date"] = $row[$k][11];

$assoc[$k]["Days Until Window Closes"] = $row[$k][12];

}



require_once "excel.php";

$export_file = "xlsfile://xampplite/htdocs/tools/example.xls";

$fp = fopen($export_file, "wb");

if (!is_resource($fp))

{

die("Cannot open $export_file");

}

fwrite($fp, serialize($assoc));

fclose($fp);

===========================================================

Cheers,
Ray