Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

PEAR - Spreadsheet Excel Writer - File error

Posted on 2008-10-25
2
1,011 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

839 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