Export/Write rows from DBF to CSV

Export/Write rows from DBF to CSV

am 13.03.2009 14:56:09 von Rahul Sitaram Johari

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] sleepwalker@rahulsjohari.com
[Web] http://www.rahulsjohari.com





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

Re: Export/Write rows from DBF to CSV

am 18.02.2010 23:31:00 von OBXer

I'm trying to adopt this piece of code for my use. I fixed the csv_data .=
trim error. Does anyone know how I can fix empty fields? Everything is
dumping to a csv file but information is not matching up. I don't know if I
can insert a space or something if field is blank?


Rahul S. Johari-2 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 );
>
>

--
View this message in context: http://old.nabble.com/Export-Write-rows-from-DBF-to-CSV-tp22 496924p27646825.html
Sent from the PHP - General mailing list archive at Nabble.com.


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

Re: Export/Write rows from DBF to CSV

am 19.02.2010 05:11:58 von Paul M Foster

On Thu, Feb 18, 2010 at 02:31:00PM -0800, OBXer wrote:

>
> Rahul S. Johari-2 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 );
> >
> >
>
> I'm trying to adopt this piece of code for my use. I fixed the csv_data .=
> trim error. Does anyone know how I can fix empty fields? Everything is
> dumping to a csv file but information is not matching up. I don't know if I
> can insert a space or something if field is blank?
>

What do you mean "fix" empty fields? And what do you mean by
"information is not matching up"?

You shouldn't insert spaces where there are blank fields. Just leave
them blank. The commas are supposed to delimit all the fields, including
the empty ones. Like:

John,Smith,,123 Main St,Tucson,AZ,,,,

Paul

--
Paul M. Foster

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

Re: Export/Write rows from DBF to CSV

am 19.02.2010 15:56:34 von Andrew Ballard

On Thu, Feb 18, 2010 at 5:31 PM, OBXer wrote:
>
> I'm trying to adopt this piece of code for my use.  I fixed the csv_=
data .=3D
> trim error.  Does anyone know how I can fix empty fields?  Ever=
ything is
> dumping to a csv file but information is not matching up.  I don't k=
now if I
> can insert a space or something if field is blank?
>
>

If you're trying to write csv data, why not use fputcsv($_fp, $row) to
write each line rather than concatenating all the lines into a single
value and then writing that string to a file? It will correctly handle
empty values (as long as the key is still present in the array) and
I've found it to be much faster at writing larger data sets than
trying to concatenate the CSV in code.

http://www.php.net/manual/en/function.fputcsv.php


Andrew

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