dbf to text file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

dbf to text file

Post by bouncer »

Code: Select all

$db = dbase_open('Exist.dbf', 0) or die (error());

if ($db) {
  $record_numbers = dbase_numrecords($db);
  for ($i = 1; $i <= $record_numbers; $i++) {

  }
}

dbase_close($db);
i have this code to open a dbf file, now how can i export/writte the info from dbf to a text file inside loop ?


thanks in advance
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Get the records with dbase_get_record_with_names and write the array's values to the file, e.g. via fwrite
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

Code: Select all

$filename = 'out.txt';

$db = dbase_open('Exist.dbf', 0) or die (error());

if ($db) {
     $record_numbers = dbase_numrecords($db);
     $fp = fopen($filename, "w") or die (error());

      for ($i = 1; $i <= $record_numbers; $i++) {
           $row = dbase_get_record_with_names($db, $i);
           fwrite($fp, " ");
     }

     fclose($fp);
}

dbase_close($db);
what come inside " ", and if the file already exists it will be overwrited ?

thanks in advance
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

Code: Select all

$filename = 'out.txt';

$db = dbase_open('Exist.dbf', 0) or die (error());

if ($db) {
	$record_numbers = dbase_numrecords($db);
	$fp = fopen($filename, "w") or die (error());

	for ($i = 1; $i <= $record_numbers; $i++) {
		$row = dbase_get_record_with_names($db, $i);
		fwrite($fp, $row "\r\n");
	}

	fclose($fp);
}

dbase_close($db);
can someone tell me what is wrong in fwrite function ?
if i make a print_r of $row i see something like this:
Array ( [MREF] => 907108 [MARM] => 40 [MEXIS] => 0 [MEXCL] => 0 [MEXFR] => 0 [deleted] => 0 )
how can i transform that into something like this:
907108,40,0,0,0
thanks in advance
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Code: Select all

$filename = 'out.txt'; 

$db = dbase_open('Exist.dbf', 0) or die (error()); 

if ($db) { 
        $record_numbers = dbase_numrecords($db); 
        $fp = fopen($filename, "w") or die (error()); 

        for ($i = 1; $i <= $record_numbers; $i++) { 
                $row = dbase_get_record_with_names($db, $i);
                $StringOfDBaseValues = implode(",",$row); 
                fwrite($fp, $StringOfDBaseValues."\r\n"); //dont forget to use the . when joining the variable to "\r\n"
        } 

        fclose($fp); 
} 

dbase_close($db);
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

thanks mikeq
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Code: Select all

$filename = 'out.txt'; 

$db = dbase_open('Exist.dbf', 0) or die (error()); 

if ($db) { 
        $record_numbers = dbase_numrecords($db); 
        $fp = fopen($filename, "w") or die (error()); 

        for ($i = 1; $i <= $record_numbers; $i++) { 
                $row = dbase_get_record_with_names($db, $i); 
                $StringOfDBaseValues = implode(",",$row);
                //find position of last comma in string
                $LastCommaPosition = strrpos($StringOfDBaseValues,",");
                //then use this to chop the string down
                $StringOfDBaseValues = substr($StringOfDBaseValues, 0, $LastCommaPosition - 1);
                fwrite($fp, $StringOfDBaseValues."\r\n"); //dont forget to use the . when joining the variable to "\r\n" 
        } 

        fclose($fp); 
} 

dbase_close($db);
something along those lines would work
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

thanks mikeq :wink:
Post Reply