Solved

PEAR - Spreadsheet Excel Writer - File error

Posted on 2008-10-25
2
1,008 Views
Last Modified: 2013-11-10
I query the database and retrieve the following array:

Array
(
      [0] => Array
      (
            [id] => 5689
            [item] => YUIOPD
            [size] => 8
            [cost] => $99.99
      )

      [1] => Array
      (
            [id] => 7664
            [item] => TYJKIP
            [size] => 12
            [cost] => $199.99
      )

      [2] => Array
      (
            [id] => 2113
            [item] => HYTIUP
            [size] => 4
            [cost] => $79.99
      )
)

I'm trying to enter that data into an excel spreadsheet using the Spreadsheet Excel Writer PEAR pkg. But, I keep getting: File error: data may be lost, and only the ids are in the spreadsheet, each on its own row.  I'm trying to figure out how to put each element into its own row for each one.

Right now, the code looks like this:
$excel = new Spreadsheet_Excel_Writer('textexcel.xls');

$sheet =& $excel->addWorksheet('worksheet 1');

$i = 0;

  foreach($res as $data) {

		foreach($data as $k => $v) {

				$sheet->write($i, $k, $v);

		}

		$i++;

  }

$excel->close();

Open in new window

0
Comment
Question by:n00b0101
2 Comments
 
LVL 11

Accepted Solution

by:
MacAnthony earned 500 total points
ID: 22804254
I think the issue is you are using the associative array name for the column. Try this.
$excel = new Spreadsheet_Excel_Writer('textexcel.xls');

$sheet =& $excel->addWorksheet('worksheet 1');

$row = 0;

  foreach($res as $data) {

                $col = 0;

                foreach($data as $v) {

                                $sheet->write($row, $col, $v);

                                $col++;

                }

                $row++;

  }

$excel->close();

Open in new window

0
 

Author Closing Comment

by:n00b0101
ID: 31509971
Thanks so much!  
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
calculated column 12 74
File submission to SQL Server 1 39
.php tree directory? 5 56
updating table data with inner join 9 24
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now