Combine XML, Files and SQLite3 with PHP 5

Take a text file, a XML file and the SQLite3 database from PHP - and create you own small web application. This example shows you how to do it if you have no MySql DBMS at hand and a not to extensive amount of data to expect (see screenshots below). The XML file (holds all basic data we want to make available in the administration by choice), the text file will handle all the chosen data and the SQLite3 will contain all data submitted from users - as an extra we want to trigger the most important actions as data inserts and changes in text files with error_log(). PHP has everything you need: SimpleXML and XPath to handle the XML data, the fopen-flock-fwrite-fclose functions to handle text files and the SQLite3 statements and features to handle database content. A configuration class concerns about the connections, a debugger class writes the log messages and a exception class of one's own secures that text files are always in fine read and write condition or checks if the submitted data has not been manipulated. The function escapeString prevents from submitting harmful code in the SQLite3 database

The scripts snippets you see here (the complete script is downloadable) is only a small part of the complete application. That's it. The SQLite3 code for the database action and other stuff is all prepared for your convenience (create-table-, css- and connection-file included). Best regards Claudio Biesele


Part of the PHP 5 Configuration Class Code:

class FastConfiguration extends FastException {

const STORE_DB = 1;
const STORE_SQLite = 2;
const STORE_XML = 3;
const STORE_FILE = 4;

public static function getStore($type = self::STORE_XML) {

switch ($type) {
   case self::STORE_DB:
   return new Configuration_DB();

   case self::STORE_SQLite:
   return new Configuration_SQLite();

   case self::STORE_XML:
   return new Configuration_XML();

   case self::STORE_FILE:
   return new Configuration_FILE();
   }
}

class ConfigurationDB {
// ....
}

class Configuration_SQLite {
   public $sqlite_config;
   protected $sqlitefile;

 function __construct() {
   $sqlitefile = 'data/vrcustomer.sqlite';
   if(!file_exists($sqlitefile) || filesize($sqlitefile) == 0) {
      die("<span class=\"mess\">No access to database.</span>");
   }
   else {
      $this->sqlite_config = new SQLite3($sqlitefile);
      return $this->sqlite_config;
   } } }

class Configuration_XML {
   public $xml_config;

 function __construct() {
   $this->xml_config = $_SERVER[DOCUMENT_ROOT]
   .'/xxx/MyRentABike/data/det_vehicles.xml';

   if (!file_exists($this->xml_config)) {
      die("<span class=\"mess\">Some error occurred.\n</span>"); }
   else {
      return $this->xml_config;
   } } }

class Configuration_FILE {
   public $file_config;

 function __construct() {
   $this->file_config = $_SERVER[DOCUMENT_ROOT]
   .'/xxx/MyRentABike/data/fleet.txt';

   if (!file_exists($this->file_config)) {
      die("<span class=\"mess\">Some error occurred.\n</span>"); }
   else {
      return $this->file_config;
   } } } 


Part of the XML Handler Class Code:

public function getXMLSelLand($land) {

   $xml = simplexml_load_file($this->xml);
   $path ="/root/laender/land";

   if (!$res = $xml->xpath($path))
   { die("<span class=\"mess\">Some error occurred.</span>"); }
   else {
   $this->xmlhtml ="<select name=\"selectLand\" size=\"1\">";

   foreach($res as $val) {
   if($val == "Europe" || $val == "USA") {
      $this->xmlhtml .= "<optgroup label=\"{$val}\">"; }
   elseif($land == $val) {
      $this->xmlhtml .= "<option selected value=\"{$val}\">{$val}</option>"; }
   else {
      $this->xmlhtml .= "<option value=\"{$val}\">{$val}</option>";
   }}
   $this->xmlhtml .= "</optgroup>";
   $this->xmlhtml .= "</select>";
   return $this->xmlhtml;
   }}


 
Part of the PHP 5 Class DebuggerFile:

class vrdebuggerfile extends vrdebuggerecho implements Debugger {

   protected $logfile = null;
   private static $instances = array();

   public static function getInstance($logfile) {
   if (!isset(self::$instances[$logfile])) {
      self::$instances[$logfile] = new vrdebuggerfile($logfile);
   }
   return self::$instances[$logfile];
   }

   protected function __construct($logfile) {
      $this->logfile = $logfile;
   }

   private function __clone() {}
   
   public function debug($message) {
      error_log("{$message}\n", 3, $this->logfile);
   }}}


Picture MyRentABike001.jpg
1. View: Overview of the small web application which allows to manage a vehicle fleet.
Picture MyRentABike002.jpg
2. View: The basic data for the bicycles is stored in a XML file and is submitted to a text file.
Picture MyRentABike003.jpg
3. View: On the same page you can fill the SQLite3 table with customers.
Picture MyRentABike004.jpg
4. View: Customers can look at the properties of the bicycles in general and in detail.
Picture MyRentABike005.jpg
5. View: The more complex look at the administration of the vehicle fleet.
Picture MyRentABike006.jpg
6. View: Change the rent start and end date in the text file with the fleet data.
Download the full script at www.fastproject.ch. The downloaded script is free from Copyright restrictions, Zurich, 22th of Mai 2016.
Donate with PayPal:
placeholder