PHP Classes

File: aguencyentry.cls.php

Recommend this page to a friend!
  Packages of Duy A. Nguyen   Small MySQL Connection   aguencyentry.cls.php   Download  
File: aguencyentry.cls.php
Role: Application script
Content type: text/plain
Description: sample class
Class: Small MySQL Connection
MySQL database access wrapper
Author: By
Last change:
Date: 20 years ago
Size: 3,103 bytes
 

Contents

Class file image Download
<?php
class AguEncyEntry extends MagicObject implements IDataObject, IRenderHTML
{
    private $table_name;
    
    //constructor
      function __construct() 
    {
          //get the global configuration
          global $g_config;
        
        //get the table name
        $this->table_name = $g_config['table_prefix'] . 'entries';
          
          //init all data member
        $this->data = array(
            'id'             =>    0,
            'name'            => '',
            'description'    => '',
            'screenshoot'    => 0,
            'iscat'            => 0,
            'parentid'        => 0
        );
       }
    //add an item to table (implement for IDataObject)
    public function add()
    {
          //prepare sql statement for insert
        $sql = "insert into $this->table_name(name, description, screenshoot, iscat, parentid) values(?, ?, ?, ?, ?)";
        
        //get the connection
        $db = MySQLConnection::init();
        
        //execute sql statement
        $ret = $db->executeNonQuery($sql, 
            $this->name, 
            $this->description, 
            $this->screenshoot,
            $this->iscat,
            $this->parentid
        );
        
        //if excution is success then return true, otherwise false will be returned
        return ($ret == 1);
        
    }
    //update an item with associate id (implement for IDataObject)
    public function update()
    {
          //prepare sql statement for update
          $sql = "update $this->table_name set 
                      name = ?, 
                    description = ?,
                    screenshoot = ?,
                    iscat = ?,
                    parentid = ?
                where id = ?";
        
        $db = MySQLConnection::init();
        
        $ret = $db->executeNonQuery($sql, 
            $this->name, 
            $this->description, 
            $this->screenshoot,
            $this->iscat,
            $this->parentid,
            $this->id
        );
        
        return ($ret == 1);    
    }
    
    public function delete()
    {
          if( !$this->load() )
              return false;
          
          //check if this record has children?
          if( $this->hasChildren() )
              return false;
          
          //generate the deleting sql statement
        $sql = "delete from $this->table_name where id = ?";
        
        $db = MySQLConnection::init();
        
        $ret = $db->executeNonQuery($sql, $this->id);
        
        return ($ret == 1);    
    }
    
    public function load()
    {
          $sql = 'select name, description, screenshoot, iscat, parentid where id = ?';
          
          $db = MySQLConnection::init();
        
        $ret = $db->executeQuery($sql, $this->id);
        
        if( $ret )
        {
            $this->name = $ret[0]['name'];
            $this->description = $ret[0]['description'];
            $this->screenshoot = $ret[0]['screenshoot'];
            $this->iscat = $ret[0]['iscat'];
            $this->parentid = $ret[0]['parentid'];
            return true;
        }
        
        return false;
    }
    
    public function getChildren()
    {
          $sql = 'select id, name, description, screenshoot, iscat where parentid = ?';
          
          $db = MySQLConnection::init();
        
        $ret = $db->executeQuery($sql, $this->id);
        
        return $ret;
    }
    
    public function hasChildren()
    {
          $sql = 'select id where parentid = ?';
          
          $db = MySQLConnection::init();
        
        $ret = $db->getNumRecordOfSql($sql, $this->id);
        
        return $ret > 0;
    }
    
    public function outputHTML()
    {
          echo('<pre>');
          print_r($this->data);
          echo('</pre>');
    }

}

?>