Location: PHPKode > scripts > Textable > textable/textable.class.php
<?php   //////////////////////////////////////////////////
        //   Textable Class for PHP4                    //
        //   Class for store data table in a text file  //
        //                                              //
        //   Pal Sitkei                                 //
        //   hide@address.com                           //
        //   07.feb.2005.                               //
        //////////////////////////////////////////////////

class textable {
	var $error;
	var $filename;
	var $table;
	
	function textable($file){ 		//string
		$this->filename = $file;
		if (file_exists($this->filename)){
			$this->read_table();
  		}
	}
	function delete($data){          //array
		$indices = $this->get_keys($data);
		$err = "{Record not found}";
		$result = 0;
		foreach( $this->table as $line ){
			$coll = 0;
			$succ = NULL;
			foreach( $indices as $index){
		    	if ($line[$index] == $data[$index]){
					$coll++;
				}
				if ($coll == count($indices)){
					$err = "";
					$succ = 1;
					$result++;
					break;
				}
			}
			if (!$succ){
				$temp[] = $line;
			}
	    }
	    $this->table = $temp;
		$this->error .= $err;
		return $result;             //number of affected rows
	}
	function insert($data){         //array
		$result = 0;
		if (is_array($data[0])){
			$temp = $data;
		}else{
			$temp[] = $data;
		}
		foreach ($temp as $rec){
			if ($this->exists($rec)){
				$this->error .= "{Existing key field}";
			}else{
				$this->table[] = $rec;
				$result++;
			}
		}
		return $result;             //number of affected rows
	}
	function save(){
		$this->write_table();
	}
	function select($data){			//array
		$indices = $this->get_keys($data);
		$err = "{Record not found}";
		foreach( $this->table as $line ){
			$coll = 0;
			foreach( $indices as $index){
		    	if ($line[$index] == $data[$index]){
					$coll++;
				}
				if ($coll == count($indices)){
					$result[] = $line;
					$err = "";
					break;
		  		}
			}
		}
	    $this->error .= $err;
	    return $result;             //array of records or NULL
	}
	function update($where, $what){			//array, array
		$pos_indices = $this->get_keys($where);
		$val_indices = $this->get_keys($what);
		$result = 0;
  		$err = "{Record not found}";
		foreach( $this->table as $line ){
			$coll = 0;
			foreach( $pos_indices as $index){
		    	if ($line[$index] == $where[$index]){
					$coll++;
				}
				if ($coll == count($pos_indices)){
					if (($val_indices[0] == 0) and
					   	($this->exists($what))){
						$err = "{Existing key field}";
						break;
					}
					for ( $i = 0; $i < count($val_indices); $i++ ){
						$line[$val_indices[$i]] = $what[$val_indices[$i]];
					}
					$err .= "";
					$result++;
				}
			}
			$temp[] = $line;
	    }
	    $this->table = $temp;
	    $this->error .= $err;
	    return $result;             //number of affected rows
	}
	
//private methodes

	function exists($data){          //array
		if (count($this->table) == 0){return NULL;}
		foreach( $this->table as $line ){
	    	if ($line[0] == $data[0]){
				$result = $line;
		  		break;
		    }
	    }
		return $result;
	}
	function get_keys($data){
		for( $i = 0; $i < count($data); $i++ ){
			if ($data[$i] <> ""){
				$keys[] = $i;
	  		}
		}
		return $keys;
	}
	function read_table(){
    	$lines = file($this->filename);
		foreach ( $lines as $row ){
			if (strlen($row) > 1){
				$temp = explode("~", $row);
				$temp[count($temp) - 1] = rtrim($temp[count($temp) - 1]);
				$this->table[] = $temp;
			}
	  	}
	}
	function write_table(){
	 	$fp = fopen($this->filename, "w");
		foreach ( $this->table as $row ){
			fwrite($fp, implode("~", $row));
			fwrite($fp, "\n");
		}
	   	fclose($fp);
	}
}

?>
Return current item: Textable