Solved

PEAR - Spreadsheet Excel Writer - File error

Posted on 2008-10-25
2
1,013 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
[X]
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
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

717 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