Location: PHPKode > scripts > Class.DB.php > class-db-php/aDB.html
<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>Class.DC.php Documentation</TITLE>

<style type='text/CSS'>
* { font-family:  Arial, Verdana, Helvetica, sans-serif; 
h1,H2 { background-color:EDEDcc; color:black ; 
	  font-weight:bold;padding:10px 10px 10px 10px;}
pre { background-color:EDEDcc; color:black ; 
	  font-weight:bold;padding:10px 10px 10px 10px;}
span.b	{font-size:150%;}
span.bold	{font-weight:bold;}
td.r	{text-align:right;}
td.l	{text-align:left;}
input {width:80px;}
<p>Class.DB.php is a simple csv file database implementation to allow PHP based applications
to have database facilities where none are otherwise available.</p>
<p>All file operations are transient, with the file being held open for the 
barest minimim of time. The data is held in arrays and written back to 
the file as appropriate.<br />
Functions intended as the public interfaces for the class return true/false on
successful operation. false returns also signify that 
a text error has beed added to the errors class property.</p>

<h1>Class Synopsys</h1>
    var $dataFile = "data.dat";		// default filename
    var $dir = "data";			// default directory
    var $assoc = false ;		// default numeric indexes
    var $cryptKey = false;		// default encryption off
    var $db ;			// main database array
    var $newrows ;			// new lines array
    var $error = false;		// error string (no error=false)
    var $lastUpd = "";			// timestamp
    var $written = 0;			// no of rows written

<h3>class constructor </h3>
<pre>obj DB(string $filename, string $dirname, boolean $assoc_mode, string $cryptKey)</pre>
<p>this takes optional parameters to set filename, 
directory, associative array mode and encryption key
The parameters may also be set directly as class properties
<h3>Load database array from file</h3>
<pre>boolean result readDB()</pre>
<p>Opens specified file, returning true if successful, else false, 
an appropriate error text is loaded into the error property
array db is loaded with data from the csv file.
if associated mode is asserted, the first row of the csv file is used as
array keys else numeric keys are used. Setting a text string in cryptKey 
causes the csv file to be read via the decrypter.</p>

<h3>appends data to File</h3>
<pre>boolean appendB()</pre>
<p>Appends rows previously loaded into the newrows array to the csv file 
using encryption if cryptKey is set.
This function is useful for log writing and adding transaction entries to lists.
<h3>writes array to datafile</h3>
<pre>boolean writeDB(boolean $append, boolean $force)</pre>
<p>This function writes the contents of array db to a csv file.
using encryption if cryptKey is set.<br />
optional behavior modifier: append causes the array to be appended to the 
existing csv file, else file is overwritten.<br />
Optional behavior modifier: force, overrides a change detection mechanism. 
readDB() records the file's timestamp. If force is not asserted and the file's 
timestamp is found to have been changed between reading and writing,
an attempt to overwrite the csv file by this method will fail with an error.
force should be set to enable a new file to be created.</p>

<h3>Appends main buffer to datafile</h3>
<pre>int append()</pre>
<p>transfers lines from newlines buffer to main db array.
function returns the number of lines transferred, or boolean false on error</p>

<h3>Update array with new data</h3>
<pre>array update(int $rownumber, array $values)</pre>
<p>the value for $rownumber is generally obtained via a call to find().
$values is an array in appropriate format for the 
current mode of the db array, either having numeric keys for assoc = false
or associative key values  if assoc is true.<br />
all keys must exist in the db array or an error (false) is returned.</p>

<h3>deletes rows from array</h3>
<pre>array delete(int $row,int $count)</pre>
<p>deletes row indicated in $row plus additional rows as identified in $count
ex. if row = 5 and count = 3, rows 5,6,7 will be deleted.
count defaults to 1.<br />
<span class ='bold'>BEWARE - multiple calls to delete() in ASCENDING ORDER</span><br />
the main db array renumbers itself automatically, thus if row 5 is deleted, 
the old row 6 becomes 5, 7 becomes 6 etc.
calling delete(5), then delete(6) then delete(7) will actually 
delete rows 5,7,9 from the db array as seen before the row 5 deletion.
Deleting in DECENDING numerical order does NOT suffer this affect 
and is the recommended method.</p>

<h3>finds row numbers matching key/value criteria</h3>
<pre>array find( array $values)</pre>
<p>Returns an array of row numbers where the key/value pair in $value 
matches array db contents.<br />
results are returned in ascending order and should be rsort()ed 
before use with delete()</p>

<p>The encryption feature was borrowd many moons ago from someone 
who had themselves borrowed the idea from someone else.
To switch encryption on , provide a string vale for cryptKey
in place of a boolean false. This may be done 'on the fly' enabling 
encrypted files to be written in un-encrypted form and vice-versa.</p>

<h2>extended class constructor DBe()</h2>

<pre>string unpackDB()</pre>
<p>Returns entire database as a comma delimited bytestream.</p>

August 2003, Hertford UK<br />
Ron Barnett &lt;ron dot barnett at BCS dot Org dot uk &gt;
Return current item: Class.DB.php