PHP头条
热点:

php试用smarty和ADODB实现对数据分页读取


<?php 
define('BASE_PATH',$_SERVER['DOCUMENT_ROOT']); 
define('SMARTY_PATH','\smartTest\Smarty\\'); 
require BASE_PATH.SMARTY_PATH.'Smarty.class.php'; 
/*$dir2的这种路径显示到表现页是这下面字符串是一样,导致smarty找不到templates路径*/ 
//$dir2 = "../Smarty/tempplates/"; 
class SmartyProject extends Smarty{ 
     
    function SmartyProject(){ 
        /*必须加这个parent::__construct();,否则,Smarty不会被构造,truncate等不能用,
         * 但这似乎没有道理,但事实上,
         * 这么做确实解决了truncate不能使用的问题*/ 
        parent::__construct(); 
        $this->template_dir = BASE_PATH.SMARTY_PATH.'/templates/'; 
        $this->compile_dir = BASE_PATH.SMARTY_PATH.'/templates_c/'; 
        $this->config_dir = BASE_PATH.SMARTY_PATH.'/configs/'; 
        $this->cache_dir = BASE_PATH.SMARTY_PATH.'/cache/'; 
    } 

 
class ConnDB{ 
    var $dbtype; 
    var $host; 
    var $user; 
    var $pwd; 
    var $dbname; 
    var $debug; 
    var $conn; 
    function ConnDB($dbtype, $host, $user, $pwd, $dbname, $debug=FALSE){//构造函数 
        $this->dbtype=$dbtype; 
        $this->host=$host; 
        $this->user=$user; 
        $this->pwd=$pwd; 
        $this->dbname=$dbname; 
        $this->debug=$debug;  
    } 
    function GetConnId(){ 
        require BASE_PATH.'/smartTest/adodb5/adodb.inc.php';//将adodb的主文件引用进来 
        if($this->dbtype == 'mysql'){ 
            $this->conn = NewADOConnection('mysql'); 
            $this->conn->Connect($this->host, $this->user, $this->pwd, $this->dbname); 
        }else if($this->dbtype == 'mssql'){ 
            $this->conn = NewADOConnection('mssql'); 
            $this->conn->Connect($this->host, $this->user, $this->pwd, $this->dbname); 
        }else if($this->dbtype == 'access'){ 
            $this->conn = NewADOConnection('access'); 
            $this->conn->Connect("Driver={Microsoft Access Driver(*.mdb)};Dbq=".$this->dbname.";Uid=".$this->user.";Pwd=".$this->pwd.";"); 
        } 
        $this->conn->Execute("set names utf-8"); 
        if($this->dbtype == 'mysql') 
            $this->conn->debug = $this->debug; 
        return $this->conn; 
    } 
    function CloseConnId(){ 
        $this->conn->Disconnect(); 
    } 

 
class AdminDB{ 
    function ExecSQL($sqlstr, $conn){ 
        $sqltype = strtolower(substr(trim($sqlstr), 0,6));//去sql语句的前6个字符 
        $rs = $conn->Execute($sqlstr); 
        if($sqltype == 'select'){ 
            $array = $rs->GetRows(); 
            if(count($array)==0 || $rs == false) 
                return false; 
            else  
                return $array; 
        }else if($sqltype == 'update'||$sqltype == 'insert'||$sqltype == 'delete'){ 
            //这写都是不返回结果集的,或者说返回空集合 
            if($rs) 
                return true; 
            else  
                return false; 
        } 
    } 

class SepPage{ 
    var $rs; 
    var $pagesize; 
    var $nowpage; 
    var $array; 
    var $conn; 
    var $sqlstr; 
    function ShowData($sqlstr,$conn,$pagesize,$nowpage){ 
        if(!isset($nowpage)||$nowpage==""){ 
            $this->nowpage = 1; 
        }else{ 
            $this->nowpage = $nowpage; 
        } 
        $this->pagesize = $pagesize; 
        $this->conn = $conn; 
        $this->sqlstr = $sqlstr; 
        //执行查询语句 
        $this->rs = $this->conn->PageExecute($this->sqlstr,$this->pagesize,$this->nowpage);//调用ADODO类中的这个方法 
        @$this->array = $this->rs->GetRows(); 
        if(count($this->array)==0||$this->rs == false) 
            return  false; 
        else  
            return  $this->array; 
    } 
    function ShowPage($contentname, $utits, $anotherserchstr, $anotherserchstrs, $class){ 
        $allrs = $this->conn->Execute($this->sqlstr); //执行查询语句 
        $record = count($allrs->GetRows());//统计记录总数 
        $pagecount = ceil($record/$this->pagesize);//计算共有多少页 
        @$str.="共有".$contentname." ".$record." ".$utits." 每页显示 ".$this->pagesize. 
        " ".$utits." 第 ".$this->rs->AbsolutePage()." 页/共 ".$pagecount." 页"; 
        $str.="    "; 
        if(!$this->rs->AtFirstPage()) 
            $str.="<a href=".$_SERVER['PHP_SELF']."?page=1&parameter1=".$anotherserchstr."&parameter2=". 
            $anotherserchstrs."class=".$class.">首页</a>"; 
        else  
            $str.="<font color='#555555'>首页</font>"; 
        $str.=" "; 
        if (!$this->rs->AtFirstPage()) 
            $str.="<a href=".$_SERVER['PHP_SELF']."?page=".($this->rs->AbsolutePage()-1)."&parameter1=".$anotherserchstr."&parameter2=". 
            $anotherserchstrs."class=".$class.">上一页</a>"; 
        else 
            $str.="<font color='#555555'>上一页</font>"; 
        $str.=" "; 
        if(!$this->rs->AtLastPage()) 
            $str.="<a href=".$_SERVER['PHP_SELF']."?page=".($this->rs->AbsolutePage()+1)."&parameter1=".$anotherserchstr."&parameter2=". 
            $anotherserchstrs."class=".$class.">下一页</a>";   
        else 
            $str.="<font color='#555555'>下一页</font>"; 
        $str.=" "; 
        if(!$this->rs->AtLastPage()) 
            $str.="<a href=".$_SERVER['PHP_SELF']."?page=".$pagecount."&parameter1=".$anotherserchstr."&parameter2=". 
            $anotherserchstrs."class=".$class.">尾页</a>"; 
        else 
            $str.="<font color='#555555'>尾页</font>"; 
        if(count($this->array)== 0||$this->rs==false) 
            return ""; 
        else 
            return $str; 
    } 

以上代码定义了几个类,用来实现数据库的连接啊,操作数据库,分页实现,数据库的存储,以及一个smarty类的子类,其实这个子类也没有实现什么特别的功能。将其命名为
smartyDAO.php或这随便取个什么名字都可以,当然,还有一点要注意的是,我里面试用了require语句,对于这些路径,大家都要该到自己对应的目录去。

<?php 
require 'smartyDao.php'; 
//数据库类实例 
$conobj  = new ConnDB("mysql", "localhost", "root", "brave", "bank"); 
$conn = $conobj->GetConnId(); 
//数据库操作类对象 
$admindb = new AdminDB(); 
//Smarty模板配置类对象 
$smarty = new SmartyProject(); 
 
$seppage = new SepPage();//实例化分页类 

这个文件就是对上面那个文件中类的实例化,可以命名为 smartyDaoImpl.php,或者命名为你自己喜欢的名字,作用很简单,是吧
然后要做的工作就是随便建立一个测试用例,来测试分页代码了,当然,这个测试也包含两个文件,一个是控制器,另外一个视图,控制器的代码是:

<?php 
include_once 'conn/smartyDaoImpl.php'; 
$arr_page = $seppage->ShowData("select * from localinfo", $conn, 5, $_GET['page']);//执行分页查询 
if(!$arr_page) 
    $smarty->assign('page',"F"); 
else { 
    $smarty->assign('page','T'); 
    $smarty->assign('showpage',$seppage->ShowPage("记录", "个", "", "", "a1"));//赋值分页模版 
    $smarty->assign('arr_page',$arr_page);//将查询记录复制给模版 

$smarty->display('view/fenyeShow.html');//指定显示数据的模版 
取个名吧,就叫做smarty_ADODB_fenye.php吧。可以看到,我们在开头用到了:

include_once 'conn/smartyDaoImpl.php'; 
这里,说明一下,前面那两个文件都是放在conn文件夹下的,测试用例写在conn同级目录下
然后视图文件,视图文件放的地方可别放错了看到我们写的第一个类中有这样一条语句

$this->template_dir = BASE_PATH.SMARTY_PATH.'/templates/'; 
这里就是指定了视图文件的绝对路径,因为,

$smarty->display('view/fenyeShow.html');//指定显示数据的模版 
这条语句,

$smarty->display('view/fenyeShow.html');//指定显示数据的模版 
所以,我们要在

BASE_PATH.SMARTY_PATH.'/templates/ 
路径下建立一个文件夹名为view,然后在先建一个文件名为fenyeShow.html,当然你可以取你喜欢的名字,但是要保证程序中指定的一致性
视图模版里面的内容是以下:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 
<table> 
{section name=id loop=$arr_page} 
<tr> 
<td height = "20" align="center" bgcolor='#FFFFFF'>{$arr_page[id].Id}</td> 
<td height = "20" align="center" bgcolor='#FFFFFF'>{$arr_page[id].temperature}</td> 
<td height = "20" align="center" bgcolor='#FFFFFF'>{$arr_page[id].illumination}</td> 
<td height = "20" align="center" bgcolor='#FFFFFF'>{$arr_page[id].moisture}</td> 
<td height = "20" align="center" bgcolor='#FFFFFF'>{$arr_page[id].times|truncate:5:"..."}</td> 
</tr> 
{/section} 
 
<tr> 
<td height = "35" colspan="3" align ="center" bgcolor="#FFFFFF">{$showpage}</td> 
</tr> 
</table> 
</body> 
</html> 

好了,做完了,只要在浏览器中定位到smarty_ADODB_fenye.php这个文件,启动服务器,OK,将会看到分页效果了。
我在做的过程中,也遇到了一些问题,一个是不能试用 truncate语句,这个问题很奇怪,到smarty官网查了下,让来,smarty子类实例化时,不会实例化其父类Smarty,因此,会导致truncate不可以用,解决的办法,自然是在子类中调用父类构造方法。

摘自 0+0+0+...=1

www.phpzy.comtrue/phprm/19033.htmlTechArticlephp试用smarty和ADODB实现对数据分页读取 ?php define(BASE_PATH,$_SERVER[DOCUMENT_ROOT]); define(SMARTY_PATH,\smartTest\Smarty\\); require BASE_PATH.SMARTY_PATH.Smarty.class.php; /*$dir2的这种路径显示到表现页是这下面字...

相关文章

    暂无相关文章

PHP之友评论

今天推荐