PEAR Forum :: PHP Extension and Application Repository

PEAR Forum Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Problems in Transforming MySQL Query Results to Spreadsheet

 
Post new topic   Reply to topic    PEAR Forum Forum Index -> File Formats
View previous topic :: View next topic  
Author Message
Babe Ruth



Joined: 18 Apr 2008
Posts: 6

PostPosted: Fri Apr 18, 2008 9:43 am    Post subject: Problems in Transforming MySQL Query Results to Spreadsheet Reply with quote

I am trying to transform an SQL result set into an Excel spreadsheet. I want to turn all the records from the table into rows of an Excel spreadsheet. I am using PEAR's Spreadsheet_Excel_Writer so that I can make a format/template design of a spreadsheet. Take a look at the code I made:

<?php
// include class file
include 'Spreadsheet/Excel/Writer.php';

// initialize reader object
$excel = new Spreadsheet_Excel_Writer();

// send client headers
$excel->send('country.xls');

// add worksheet
$sheet =& $excel->addWorksheet('SQL_Output');

// attempt a connection
try {
$pdo = new PDO('mysql:dbname=cdms;host=localhost', 'root', 'admin');
} catch (PDOException $e) {
die("ERROR: Could not connect: " . $e->getMessage());
}

// read data from database
// convert into spreadsheet
$rowCount = 0;
$sql = "SELECT * FROM businessunits";
if ($result = $pdo->query($sql)) {
// get header row
for ($x=0; $x<$result->columnCount(); $x++) {
$meta = $result->getColumnMeta($x);
$sheet->write($rowCount, $x, $meta['name']);
}
// get data rows
$rowCount++;
while($row = $result->fetch()) {
foreach ($row as $key => $value) {
$sheet->write($rowCount, $key, $value);
}
$rowCount++;
}
} else {
echo "ERROR: Could not execute $sql. " . print_r($pdo->errorInfo());
}

// close connection
unset($pdo);

// save file to disk
if ($excel->close() === true) {
echo 'Spreadsheet successfully saved!';
} else {
echo 'ERROR: Could not save spreadsheet.';
}
?>

<html>
<head>
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

The problem I am encountering is that the value of the SQL results doesn't match with what the results show in the Excel. Instead of showing the correct results for the first column, it always show what are the query results of my last column. The sample is this:

UnitID SOLID RCNumber GroupNumber
0 9999 999 0
1 1901 270 1
1 1902 271 1
1 1901 274 1
3 1904 275 3
1 3401 276 1

What lines should I change in my php code in order for the excel results match what is in my database
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Fri Apr 18, 2008 11:35 am    Post subject: Reply with quote

How about adding a
Code:
var_dump($row);
after opening the foreach() loop? This will show you the values, and you'll see whether the column values (= keys of your array) are the right ones.

BTW, instead of fetching the values yourself and instead of writing the file yourself, you might want to take a look at Structures_DataGrid and its subpackages SDG_DataSource_PDO and SDG_Renderer_XLS. This combination could generate your Excel files in just a few lines of code.
Back to top
View user's profile Send private message
Babe Ruth



Joined: 18 Apr 2008
Posts: 6

PostPosted: Fri Apr 18, 2008 1:58 pm    Post subject: about var_dump($row) Reply with quote

Thanks for your reply. I did not expect a reply this early. Now when I run with the additional code ---var_dump($row); --- it is concluded from my script that for each row, the first column always takes the values of my last column.

I know there is a problem with my script or how do I conceptualize it.

Can anyone teach me a better code to put the sql results into the spreadsheet, but in a manner that I have the flexibility to put the results to whatever starting spreadsheet-cell that I want. For example, I would like the query results to be located not from cells 'A1 to J25' but from cells 'B2 to K26'

BTW, i have noted the Structures_DataGrid
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Fri Apr 18, 2008 2:52 pm    Post subject: Re: about var_dump($row) Reply with quote

Babe Ruth wrote:
Can anyone teach me a better code to put the sql results into the spreadsheet, but in a manner that I have the flexibility to put the results to whatever starting spreadsheet-cell that I want. For example, I would like the query results to be located not from cells 'A1 to J25' but from cells 'B2 to K26'


Well, currently you're using just your array keys for specifying the column (second parameter of the write() method). What you need is a second variable like your existing $rowCount variable, maybe $colCount. Initialize it with 1 (for the second column) directly before the foreach() loop (but in the while() loop). This ensures that each row starts at the second column.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    PEAR Forum Forum Index -> File Formats All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



PEAR Forum topic RSS feed 
Powered by phpBB © 2001, 2005 phpBB Group

Provided by Ministry of Web developement

'Actiemonitor' online projectmanagement software