Solved

PEAR - Spreadsheet Excel Writer - File error

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
echo button 13 48
How to remove decimals from calculation amount 8 25
sql sentence 2 13
Why does my array not "dump?" 5 21
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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.

744 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

12 Experts available now in Live!

Get 1:1 Help Now