nakome
1/18/2017 - 3:45 AM

Simple guestbook ( not finish)

Simple guestbook ( not finish)

<?php

/*
  Structure of demo
  controller/SimplyTpl.php
  storage/books.db
  index.php
*/

ini_set("display_errors", "1");
error_reporting(E_ALL);


// Root directory
define('ROOT', rtrim(dirname(__FILE__), '\\/'));
define('CONTROLLER', ROOT.'/controller');
define('STORAGE', ROOT.'/storage');
define('VIEWS', ROOT.'/views');
define('LAYOUTS', ROOT.'/layouts');

require CONTROLLER.'/comments.php';

$gb = new GuestBook(STORAGE.'/books.db');

//$gb->install();

//$gb->addNewMark('Alex', 'me@alexsnet.ru', 5, 'Good!', '127.0.0.1');

//print_r( $gb->getMessages(1,1) );

//$gb->delete(1);
<?php
class GuestBook {
     
    private $base = '';
    private $pass = false;
    private $db = '';
     
    private $connected = false;
     
    public function __construct( $database='', $basepass=false ){
        if( !file_exists($database) )
        {
            throw new Exception("Can not find database file.");
        }

        $this->base = $database;

        if ($this->db = new SQLite3($this->base))
        {
            $this->connected = true;
        }
        else
        {
            throw new Exception('Can not connect to database! Database error: ');
        }
    }
     
    public function install()
    {
        if( $this->connected )
        {
            $this->db->exec("
                CREATE TABLE if not exists Messages ( 
                    Messagesid INTEGER PRIMARY KEY AUTOINCREMENT, 
                    Name TEXT NOT NULL , 
                    Mail TEXT NOT NULL , 
                    Mark INTEGER NOT NULL , 
                    Message TEXT NOT NULL , 
                    IP TEXT 
                );");
        }
        else
        {
            throw new Exception('Not connected to database.');
        }
    }
     
    public function addNewMark($name='', $mail='', $mark=0, $message='', $ip='0.0.0.0'){
        if( !$this->connected )
        {
            throw new Exception('Not connected to database.');
        }
        $this->db->exec("
            INSERT INTO Messages (Name,Mail,Mark,Message,IP) 
            VALUES ('$name','$mail',$mark,'$message','$ip');
        ");
    }
     
    public function getMessages( $count=10){
        if( !$this->connected )
        {
            throw new Exception('Not connected to database.');
        }
        $q = $this->db->query("SELECT * FROM Messages LIMIT '$count'");
        return $q->fetchArray();
    }
     
    public function delete( $id ) {
        if( !$this->connected )
        { 
            throw new Exception('Not connected to database.');
        }
        $this->db->exec("DELETE FROM Messages WHERE MessagesID=$id");
    }
}
?>