Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP] TextDB flat file database

Experienced Elementalist
Joined
Jul 23, 2012
Messages
201
Reaction score
128
hello, im changing my own made flat db to noSQL or sqlLite, so i decided to share my old source :D

PHP:
<?php
/**
 * Titan Web Engine
 *
 * @File:       TextDB.lib.php
 * @Created:    06/03/2013
 *
 */
    if(!defined(__SYSTEM__)){die();}

class TextDB
{
	/**
	 * @var array Stores database information
	 */
	private $database = array();

	function __construct($database,$columns = array())
	{
		$this->database['file']     = $database;
		$this->database['columns']  = $columns;
		$this->database['read']     = file($database);
		$this->database['db']       = '';
        $this->database['count']    = 0;
		$this->readDatabase();
	}
	/**
	 * Reads database contents
	 */
	private function readDatabase()
	{
		foreach($this->database['read'] as $data)
		{
			if(strlen($data) < 10 OR eregi('#',$data))
                continue;

			$item  = explode('|',$data);
			$first = true;
			foreach($this->database['columns'] as $id => $name)
			{
				if($first == false)
					$item[$id] =  $this->DecodeData($item[$id],CFG_ENCRYPTION_PASSWORD);

				$this->database['db'][$item[0]][$name] = $item[$id];

				$first = false;


			}
            $this->database['count'] =  $this->database['count'] + 1;
		}
	}
	/**
	 * Updated item in selected database
	 *
	 * @param string $id       database item key to be updated
	 * @param string $column   column name to be updated
	 * @param string $value    database item new value
	 */
	function update($id,$column,$value)
	{
		//if (array_key_exists($id, $this->database['db']))
		{
			$this->database['db'] [$id] [$column] = $value;
		}
	}
	/**
	 * Deletes specified item in database
	 *
	 * @param string $id
	 */
	function delete($id)
	{
		//if (array_key_exists($id, $this->database['db'][$id]))
		{
			unset($this->database['db'][$id]);
		}
	}
	/**
	 * Returns selected database item values
	 *
	 * @param string $id    database item key
	 *
	 * @return array
	 */
	function getItemByID($id)
	{
		return $this->database['db'][$id];
	}
    /**
     * Compares and returns specified database item based or column and it's value.
     * Columns value must be unique for example account id or email.
     * If results will contain multiple items only first item will be returned.
     *
     * @param string $column
     * @param string $requiredValue
     *
     * @return bool/array
     */
    function getItemByColumn($column,$requiredValue)
    {
        foreach($this->getAllItems() as $item)
        {
            if($item[$column] == $requiredValue)
            {
                return $item;
            }
        }
        return false;
    }
	/**
	 * Returns all items stored in database
	 *
	 * @return array
	 */
	function getAllItems()
	{
		return $this->database['db'];
	}
	/**
	 * Inserts new item in database
	 *
	 * @param  $id      NULL when item was added, time()
	 * @param array $data   data array of database items
	 */
	function insert($id,$data = array())
	{
		$this->database['db'][$id] = $data;
	}
    function getRowCount()
    {
        return $this->database['count'];
    }
	/**
	 * Stores all modified and added information to database in file
	 */
	function save()
	{
		$db = fopen($this->database['file'],'w');

        fwrite($db,'#<? die("<b>TextDB Library Engine</b>:You do not have premission to view db files."); ?>'.PHP_EOL);

		foreach ($this->database['db'] as $index  => $item)
		{
			if(count($item) < count($this->database['columns']))
				continue;

			$first = true;

			foreach($item as $dataIndex => $dataItem)
			{
				if($first == false)
					$item[$dataIndex] = $this->EncodeData($dataItem,CFG_ENCRYPTION_PASSWORD);

				$first = false;
			}
			fwrite( $db,implode("|", $item).'|'.PHP_EOL);
		}

		fwrite($db,PHP_EOL);
		fclose($db);
	}


	function EncodeData($string,$key)
	{
        // MAKE YOUR OWN
	}

	function DecodeData($string,$key)
	{
        // MAKE YOUR OWN
	}
}

it's nothing special simple flat file database engine(if i may call it that way, lol)

Usage

PHP:
$TextDB       = new TextDB('db_file.db.php',array('id','title','author','language','date','content'));

foreach($this->getTextDB->getAllItems() as $news)
{
	TWE_Template::writeNews($news['title'],$news['content'],$news['author'],$news['date']),$news['language']);
}
		
                        $registerID = time();
                        $registerData = array
                        (
                            'id'                => $registerID,
                            'account'           => $_POST['user_nick'],
                            'firstname'         => NULL,
                            'lastname'          => NULL
                        );

                        $this->getUserDB->Insert($registerID,$registerData);
                        $this->getUserDB->save();
 
Back
Top