Tuesday, January 19, 2016

php - oracle - script

final class TESTconn

{
    // Hold an instance of the class
    private static $instance;
    public static $conn;
    // The singleton method
    public static function open()
    {
        if (!isset(self::$instance)) {
            self::$instance = new TESTconn();
        }
        return self::$instance;
    }
    public function __construct()
    {
        //echo time(),"..";
        $DBuser='username';            //bisa diganti
        $DBpwd='password';             //bisa diganti
        $DBstring='localhost/xe'; //bisa diganti
        self::$conn = oci_connect($DBuser,$DBpwd,$DBstring);
        if (!self::$conn) {
            $e = oci_error();
            trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
        }
    }
}


==LIST==

define("ROW_PER_PAGE",20);

include 'TESTconn.class.php';
class TESTList 
{
    var $sqlID;
    var $sqlText;
    var $currentPage;
    var $resCount;
    var $resData;
    public function __construct($newSqlID)
    {
        $this->setSqlID($newSqlID);
    }   
    public function setSqlID($newSqlID){
        $this->sqlID=$newSqlID;
        $this->currentPage=1;
        $this->sqlText = apc_fetch($newSqlID.'.sql');
    }
    public function getData($newPage)
    {
        if($newPage<1 br="">            $this->currentPage=1;
        else
            $this->currentPage=$newPage;
       
        //echo $this->sqlText; die();
        $sampai = ($this->currentPage - 1) * ROW_PER_PAGE + ROW_PER_PAGE + 1;
        $dari = ($this->currentPage - 1) * ROW_PER_PAGE;
        $modifSqlText="select * from (select TEST.*, rownum as nomor_urut from ("
            .$this->sqlText.") TEST where rownum<=".$sampai.") where nomor_urut>".$dari;
        //echo $modifSqlText; die();

        $DB=TESTconn::open();
        $stid = oci_parse($DB::$conn, $modifSqlText);
        oci_execute($stid);
        //$nrows = oci_fetch_all($stid, $res);
        $this->resCount = oci_fetch_all($stid, $this->resData, null, null, OCI_FETCHSTATEMENT_BY_ROW);
        var_dump($this->resData);
    }
}

No comments:

Post a Comment