PHP Class to use XML file(s) like a FlatFileDatabase
- PHP 5 +
- SimpleXML
- DOMXPath
Example of table news
with few records.
news.xml:
<?xml version="1.0" encoding="UTF-8"?>
<table name="news" lastID="4">
<row id="1">
<field name="title" type="string">My first news</field>
<field name="author" type="string">Grego</field>
<field name="content" type="string">
Lorem ipsum dolor sit amet enim. Etiam ullamcorper. Suspendisse a pellentesque dui, non felis. Maecenas malesuada elit lectus felis, malesuada ultricies.
</field>
</row>
<row id="4">
<field name="title" type="string">Some other news</field>
<field name="author" type="string">Admin</field>
<field name="content" type="string">
Curabitur et ligula. Ut molestie a, ultricies porta urna. Vestibulum commodo volutpat a, convallis ac, laoreet enim.
</field>
</row>
</table>
First of all you should define constant DB_PATH
containing absolute path to folder with XML files and include class file:
define('DB_PATH', __DIR__.'/db/');
require 'xmlDB.php';
I assume that our XML path looks like db/news.xml
.
Database::create('name_of_database');
Database::remove('name_of_database');
$news = Database::factory('news')
->select()->order_by('id', 'DESC')->find_all();
foreach($news as $post)
{
echo '<h1>'.$post->title.'</h1>';
echo '<small>author: '.$post->author.', ID: '.$post->id.'</small>';
echo '<p>'.$post->content.'</p>';
}
No need to use order_by_desc()
$news = Database::factory('news', 1)
->select();
echo '<h1>'.$news->title.'</h1>';
echo '<small>author: '.$news->author.', ID: '.$news->id.'</small>';
echo '<p>'.$news->content.'</p>';
Type ID of record after file name in factory()
method.
Don't use any methods after select()
You haven't to load all fields from record. Possible to specify names of fields you want to get in select()
method by typing them in array:
select(array('title', 'content'))
Will return only fields title
and content
-
find_all()
- returns array of rows. -
count()
- return count of rows. -
order_by()
- sort rows by key and in order. First parameter iskey
second (optional)order
. -
limit()
- return specified number of rows. Second parameter (optional) it's offset. -
where()
- return row(s) with specified field value - (column, operator, value). -
or_where()
- alias for where(). Other type of sorting results. Example:$row = Database::factory('news')->select()->where('author', '=', 'admin')->or_where('author', '=', 'me')->find_all(); //will return all news where author is Admin and Me
It's pretty simple:
$row = Database::factory('news');
$row->title = 'Inserted news';
$row->author = 'me';
$row->content = 'Some content to my inserted news';
$row->save();
It will add new row on the end of DOM.
It's very smilar to Inserting
, you must set ID of row you want to edit in factory()
second parameter.
$row = Database::factory('news',1); //Will edit row with ID 1
$row->select();
$row->title = 'Edited news';
$row->author = 'me';
$row->content = 'Some edited content to my inserted news';
$row->save();
Don't forget about to call select()
method
$row = Database::factory('news',1); //Will remove row with ID 1
$row->delete();
=======
$row = Database::factory('news',1); //Will remove row with ID 1
$row->delete();
Database::factory('news')->select()->where('author', '=', 'admin')->delete();
That's all for now, I think it is possible to write something else in future ;)
My homepage: http://greg0.ovh.org
If you like and using/want to use my repo send few words to my e-mail ;)