note 78946 deleted from ref.dbase by tularis
| From: | tularis@php.net | Date: | Sun, 04 Nov 2007 10:09:55 +0000 |
| Subject: | note 78946 deleted from ref.dbase by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-131990@lists.php.net to get a copy of this message | ||
Note Submitter: Michael A
----
Takes Hadi's code a step further, implementing dbase_open, get_num_records,
get_record_with_names, and dbase_close
<?php
class dbf {
var $dbfname;
var $dbfheaders;
var $dbfentries;
function dbf($name) {
$this->dbfname = $name;
$this->get_dbf_header();
}
function get_num_records() {
return count($this->dbfentries);
}
function get_record_num($index) {
return $this->dbfentries[$index];
}
function get_dbf_header() {
$dbfname = $this->dbfname;
$fdbf = fopen($dbfname, 'r');
$dbfhdrarr = array();
$buff32 = array();
$i = 1;
$goon = true;
while($goon) {
if(!feof($fdbf)) {
$buff32 = fread($fdbf, 32);
if($i > 1) { // ignore the first line
if(substr($buff32, 0, 1) == chr(13)) {
//this means we now have the data coming from the dbf file
$goon = false;
}
else {
$pos = strpos(substr($buff32, 0, 10), chr(0));
$pos = ($pos == 0 ? 10 : $pos);
$fieldname = substr($buff32, 0, $pos);
$fieldtype = substr($buff32, 11, 1);
$fieldlen = ord(substr($buff32, 16, 1));
$fielddec = ord(substr($buff32, 17, 1));
array_push($dbfhdrarr, array("name" => $fieldname, "type" =>
$fieldtype, "length" => $fieldlen, "dec" => $fielddec));
}
}
$i++;
}
else {
$goon = false;
}
}
fclose($fdbf);
$this->dbfheaders = $dbfhdrarr;
//so now we can start reading the data from the file!
$fdbf = fopen($dbfname, 'r');
$num = $i - 1; //number of lines to skip (header lines)
$dbfarray = array();
$i = 1;
$goon = true;
while($goon) {
if(!feof($fdbf)) {
if($i >= $num) { // ignore the first line, plus the header lines
if($i == $num) {
//ignore 2 extra spaces
$buff32 = fread($fdbf, 2);
}
//so now you need to pull in piece by piece
//first off, each new entry starts with 1 blank space, so pull that in and ignore it
$buff32 = fread($fdbf, 1);
$newArr = array();
//next, start pulling the headers out and seeing their length, then pull that much!
for($j = 0; $j < count($dbfhdrarr); $j++) {
//check to see the length, then pull it
$buff32 = fread($fdbf, $dbfhdrarr[$j]['length']);
//ignore whitespace of $buff32
$buff32 = chop($buff32);
$newArr[$dbfhdrarr[$j]['name']] = $buff32;
}
$dbfarray[] = $newArr;
}
else {
//read in header lines, but just ignore for now
$buff32 = fread($fdbf, 32);
}
$i++;
}
else {
$goon = false;
}
}
fclose($fdbf);
//$dbfarray will always have one blank entry at the end, get rid of it
$temp = array();
for($i = 1; $i < count($dbfarray); $i++) {
$temp[$i - 1] = $dbfarray[$i - 1];
}
$dbfarray = $temp;
$this->dbfentries = $dbfarray;
}
}
?>