So I'm working on a class that will allow you to use flatfiles(aka textfiles) for databases....this is what i have so far...
currently you can create an empty file, or a file with just fields, or a file with fields and data AND you can delete files...it doesn't sound like a lot i just want to check this so far before I continue with reading and searching.
EDIT: i added the ability to get all data from a file, it returns an assoc array
<?php
class kurtz_ffdb
{
private $sep = "|"; // the seperator between the field and data
private $db_ext;
private $data_dir;
private $handle;
private $output;
// params:
// $dataDir - the directory where files are stored
// $dbExt - the extension that all db files will have
public function __construct($dataDir='data/', $dbExt='.ffdb')
{
if(!is_dir($dataDir)){ $this->error("Data Directory Not Found!"); }
$this->db_ext = $dbExt;
$this->data_dir = $dataDir;
}
// params:
// $name - the name of the new db file
// $fields - array - the name of every field you wish to create
// $data - array - the data corresponding to above fields
public function create($name, $fields=array(), $data=array())
{
if($this->data_file_exists($name))
{
$this->error("Cannot Create File, (".$name.$this->db_ext.") Already Exists!");
}
else
{
$this->handle = fopen($this->data_dir.$name.$this->db_ext, "w+") or $this->error("Data File Couldn't Be Created!");
if(!empty($fields) && is_array($fields))
{
if(!empty($data) && is_array($data))
{
if(count($fields) !== count($data)){ $this->error("Invalid Data/Field Array!"); }
else
{
$write_data = array_combine($fields, $data);
foreach($write_data as $key => $value)
{
fwrite($this->handle, $key.$this->sep.$value."\r\n") or $this->error("Cannot Write To File!");;
}
}
}
else
{
fwrite($this->handle, implode($this->sep."\r\n", $fields).$this->sep) or $this->error("Cannot Write To File!");
}
}
}
unset($this->handle);
}
// params:
// $name - the name of field to delete
public function del($name)
{
if(!$this->data_file_exists($name)){ $this->error("File Not Found!"); }
unlink($this->data_dir.$name.$this->db_ext) or $this->error("File Could Not Be Deleted!");
}
// params:
// $name - the name of the data file to get info from
public function get_all($name)
{
$this->handle = fopen($this->data_dir.$name.$this->db_ext, "r");
$read_data = fread($this->handle, filesize($this->data_dir.$name.$this->db_ext));
$rows = explode("\r\n", $read_data);
foreach($rows as $row)
{
$split = explode($this->sep, $row);
$fields[] = $split[0];
$data[] = $split[1];
}
$this->output = array_combine($fields, $data);
$last_row = count($this->output);
unset($this->output[$last_row]);
return $this->output;
}
// INTERNAL FUNC
// params:
// $name - name of data file to check existance
private function data_file_exists($name)
{
if(file_exists($this->data_dir.$name.$this->db_ext)){ return true; }
else{ return false; }
}
// INTERNAL FUNC
// params:
// $error_txt - the text to display in error
// $error_prefix - the prefix to the error
private function error($error_txt, $error_prefix="<b>Error: </b>")
{
die($error_prefix.$error_txt);
}
}
$ffdb = new kurtz_ffdb();
$db_fields = array("id", "name", "email");
$db_data = array("111", "shawn", "shawnkurtz@gmail.com");
$users = $ffdb->get_all("users");
foreach($users as $key => $value)
{
echo ucfirst($key).": ". $value ."<br />";
}
?>