On Mar 13, 2009, at 11:44 AM, Shawn McKenzie wrote:

Rahul S. Johari wrote:

On Mar 13, 2009, at 10:01 AM, Bastien Koert wrote:

On Fri, Mar 13, 2009 at 9:56 AM, Rahul S. Johari <
sleepwal...@rahulsjohari.com> wrote:

Ave,

I'm trying to retrieve data from a DBF database and write it to a CSV
file
in a comma delimited format. I'm able to get the data and write it to
CSV,
but it only writes the last row/record ... not all the records. I know I
don't have the correct code and I'm hoping someone can help me...

_____________
#CREATE CSV
$date = date('mdy');
$_file = 'CSV/TransferData_'.$date.'.csv';
$_fp = @fopen( $_file, 'w' );

     #SELECT DBF TO OPEN - READ ONLY
     $db = dbase_open("mydata.dbf", 0);
     #PULL UP RECORD
     if ($db) {
       $record_numbers = dbase_numrecords($db);
       for ($i = 1; $i <= $record_numbers; $i++) {
             $row = dbase_get_record_with_names($db, $i);

             #WRITE ROWS TO VARIABLE
             $_csv_data =
trim($row['PHONE']).",".trim($row['DATE']).","."\n"; <-- THIS is
where my
problem is! This only writes the last row!!
       }
     }

#WRITE TO CSV
@fwrite( $_fp, $_csv_data );
@fclose( $_fp );
_____________

Thanks!

---
Rahul Sitaram Johari
Founder, Internet Architects Group, Inc.

[Email] sleepwal...@rahulsjohari.com
[Web]   http://www.rahulsjohari.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

@fwrite( $_fp, $_csv_data );
You are overwriting the variable ($csv_data) that holds the row, add a
period (concatenator) operator to build the up the data

#WRITE ROWS TO VARIABLE
             $_csv_data .=
trim($row['PHONE']).",".trim($row['DATE']).","."\n";
<-- THIS is where my problem is! This only writes the last row!!

--

Bastien

Cat, the other other white meat


AH!!! The Simplest Solution!! It works!!!! Absolutely 100% Perfect!!

Much Thanks :)

Or even simpler and won't build a huge string if you have millions of
rows, just move your fwrite() up into the loop to write each record to
the csv:

#WRITE ROWS TO VARIABLE AND WRITE LINE TO CSV
$_csv_data = trim($row['PHONE']).",".trim($row['DATE']).","."\n";
@fwrite( $_fp, $_csv_data );

--
Thanks!
-Shawn
http://www.spidean.com


Very, Very Interesting Snippet - works perfectly fine and I agree, more efficient!

Thanks!!

---
Rahul Sitaram Johari
Founder, Internet Architects Group, Inc.

[Email] sleepwal...@rahulsjohari.com
[Web]   http://www.rahulsjohari.com





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to