php+smarty分页原理与分页代码分享

发布时间:2020-02-25编辑:脚本学堂
本文介绍了php分页原理与分页代码的实现方法,smarty+adodb+php分页代码实现方法详解,需要的朋友参考下。

分享下smarty+adodb+php分页方法,模板设计采用的是smarty模板,链接数据库使用的是adodb技术。

设计思路:
下载smarty,adodb,到站点根目录page下的fenye文件夹下。

专题推荐:php分页代码大全

smarty:
在smarty文件夹下分别建立三个文件夹templates,templates_c,smarty_cache.

如图:
php分页原理代码

1,对smarty进行配置操作的文件,smarty_inc.php代码:
 

复制代码 代码示例:

<?php
include_once("smarty.class.php");
class smartyproject extendssmarty
{
function __construct()
 {

 $this->config_dir="smarty/smarty/config_file.class.php";
 $this->caching=false;
 $this->template_dir = "smarty/templates/";
 $this->cache_dir = "smarty/smarty_cache/";
 $this->left_delimiter = "{";
 $this->right_delimiter = "}";
 }
}
?>

2,adodb,连接数据库类:
 

复制代码 代码示例:
class conndb
{
var $dbtype;
var $host;
var $user;
var $pwd;
var $dbname;
var $debug;//false不显示侦错信息,反之,显示
var $conn;
function __construct($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;
}
//实现数据库的连接并返回连接对象(个人理解可以说返回adodb对象)
function getconnid()
{
 include_once('adodb5/adodb.inc.php');
 if($this->dbtype == "mysql" || $this->dbtype == "mssql")
 {
if($this->dbtype == "mysql")
{
 $this->conn = newadoconnection("mysql");//创建adodb对象,声明数据库类型为mysql
}
else
{
 $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->disconnection();
}
}

3,数据库操作类:
 

复制代码 代码示例:
class admindb
{
 var $sqlstr;
 var $conn;
 var $sqltype;
 var $rs;
 var $array;
 function execsql($sqlstr,$conn)
 {
$rs = $conn->execute($sqlstr);
$sqltype = strtolower(substr(trim($sqlstr),0,6)); //strtolower() 函数把字符串转换为小写。
if($sqltype = "select")
{
 $array = $rs->getrows(); // 类似mysql_fetch_array函数返回的数组
 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;
 }
}
 }
}
 

 
4,分页类:
 

复制代码 代码示例:

class seppage
{
var $rs;
var $pagesize;
var $nowpage;
var $array;
var $conn;
var $sqlstr;
function showdate($sqlstr,$conn,$pagesize,$nowpage)
{
 if(!isset($nowpage)||$nowpage=="")
 {
$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);
 //pageexecute($sql, $nrows, $page, $inputarr=false) 使用资料集的页码功能,叁数 $page 是以 1 为启使值
$this->array = $this->rs->getrows();
if(count($this->array) == 0 || $this->rs == false)
{
 return false;
}
else
{
 return $this->array;
}
 
}

function showpage($contentname,$utits,$anothersearchstr,$class)
{

 $allrs=$this->conn->execute($this->sqlstr);
 $record=count($allrs->getrows());
 $pagecount=ceil($record/$this->pagesize);


 $str.="共有".$contentname."&nbsp;".$record."&nbsp;".$utits."&nbsp;每页显示&nbsp;".$this->pagesize."&nbsp;".$utits."&nbsp;第&nbsp;".$this->rs->absolutepage()."&nbsp;页/共&nbsp;".$pagecount."&nbsp;页";

 $str.="&nbsp;&nbsp;&nbsp;&nbsp;";

 if(!$this->rs->atfirstpage())
 {
$str.="<a href=".$_server['php_self']."?page=1".$anothersearchstr." class=".$class.">首页</a>";
 }
 else
 {
$str.="<font color='#555555'>首页</font>";
 }
 $str.="&nbsp;";

 if(!$this->rs->atfirstpage())
 {
$str.="<a href=".$_server['php_self']."?page=".($this->rs->absolutepage()-1).$anothersearchstr." class=".$class.">上一页</a>";
 }
 else
 {
$str.="<font color='#555555'>上一页</font>";
 }
 $str.="&nbsp;";

 if(!$this->rs->atlastpage())
 {
$str.="<a href=".$_server['php_self']."?page=".($this->rs->absolutepage()+1).$anothersearchstr." class=".$class.">下一页</a>";
 }
 else
 {
$str.="<font color='#555555'>下一页</font>";
 }
 $str.="&nbsp;";
 

 if(!$this->rs->atlastpage())
 {
$str.="<a href=".$_server['php_self']."?page=".$pagecount.$anothersearchstr." class=".$class.">尾页</a>";
 }
 else
 {
$str.="<font color='#555555'>尾页</font>";
 }
 if(count($this->array)==0 || $this->rs==false)
 {
 return "";
 }
 else
 {
 return $str;
 }
 }
}

5,文章字符转换处理的类:
 

复制代码 代码示例:

class usefun
{
 
 function unhtml($text)
 {
$content=(nl2br(htmlspecialchars($text)));//htmlspecialchars() 函数把一些预定义的字符转换为 html 实体,nl2br() 函数在字符串中的每个新行 (/n) 之前插入 html 换行符 (<br />)。

$content=str_replace("[strong]","<strong>",$content);
$content=str_replace("[/strong]","</strong>",$content);
$content=str_replace("[em]","<em>",$content);
$content=str_replace("[/em]","</em>",$content);
$content=str_replace("[u]","<u>",$content);
$content=str_replace("[/u]","</u>",$content);
 
 
$content=str_replace("[font color=#ff0000]","<font color=#ff0000>",$content);
$content=str_replace("[font color=#00ff00]","<font color=#00ff00>",$content);
$content=str_replace("[font color=#0000ff]","<font color=#0000ff>",$content);
 
$content=str_replace("[font face=楷体_gb2312]","<font face=楷体_gb2312>",$content);
$content=str_replace("[font face=宋体]","<font face=新宋体>",$content);
$content=str_replace("[font face=隶书]","<font face=隶书>",$content);
 $content=str_replace("[/font]","</font>",$content);
//$content=str_replace(chr(32)," ",$content);
$content=str_replace("[font size=1]","<font size=1>",$content);
$content=str_replace("[font size=2]","<font size=2>",$content);
$content=str_replace("[font size=3]","<font size=3>",$content);
$content=str_replace("[font size=4]","<font size=4>",$content);
 $content=str_replace("[font size=5]","<font size=5>",$content);
$content=str_replace("[font size=6]","<font size=6>",$content);

$content=str_replace("[fieldset][legend]","<fieldset><legend>",$content);
$content=str_replace("[/legend]","</legend>",$content);
$content=str_replace("[/fieldset]","</fieldset>",$content);
return $content;
 }
}

将以上四个类全部放到一个类文件system.class.inc.php里.

另外几个文件:
1、system.inc.php:
 

复制代码 代码示例:

<?php
 session_start();
 include_once("smarty_inc.php");
 include_once("system.class.inc.php");
 //数据库连接类实例化
 $connobj = new conndb("mysql","localhost","root","vertrigo","db_fenye",false);
 $conn = $connobj->getconnid();
 //数据库操作类实例化
 $admindb = new admindb();
 //分页类实例化
 $seppage=new seppage();
 //使用常用函数类实例化
 $usefun=new usefun();
 //调用smarty模板
 $smarty=new smartyproject();
 function unhtml($params)
 {
 extract($params);
 $text=$content;
 global $usefun;
 return $usefun->unhtml($text);
 }

 $smarty->register_function("unhtml","unhtml");
?>

2、执行文件,index.php:
 

复制代码 代码示例:
<?php
include_once("system.inc.php");
$arraybbstell = $admindb->execsql("select * from tb_bookinfo",$conn);
if(!$arraybbstell)
{
 $smarty->assign("isbbstell","t");
}
else
{
 $smarty->assign("isbbstell",t);
 $smarty->assign("arraybbstell",$arraybbstell);
}
$arraybbs = $seppage->showdate("select * from tb_bookinfo",$conn,1,$_get["page"]);
if(!$arraybbs)
{
 $smarty->assign("isbbs",f);
 
}
else
{
 $smarty->assign("isbbs",t);
 $smarty->assign("showpage",$seppage->showpage("帖子","条","","a1"));
 $smarty->assign("arraybbs",$arraybbs);
}
$smarty->display("index.html");
?>

3、解析文件,index.html:
 

复制代码 代码示例:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>页模</title>
</head>
<link rel="stylesheet" href="css/css.css"/>
<body>
<div style="margin:auto; width:1003px; height:auto;">
<img src="images/sy_1.jpg" width="1002" height="154">
<table width="1002" border="0" cellspacing="0" cellpadding="0" >
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td><img src="images/sy_3.jpg" width="1002" height="65" border="0" usemap="#map"></td>
</tr>
</table>
<table width="1002" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="89">&nbsp;</td>
<td width="825" align="center"><table width="825" border="1" cellpadding="1" cellspacing="1" bordercolor="#ffffff" bgcolor="#d0d0d0">
<tr>
<td align="center" bgcolor="#ffffff">
{if $isbbs=="t"}
<table width="96%" height="5" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#ffffff"></td>
</tr>
</table>
<table width="96%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#349ed8">
<tr>
<td bgcolor="#ffffff"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td width="10%" class="style4" height="22" bgcolor="#58bae9" >&nbsp;&nbsp;<font color="#ffffff">图书名称</font></td>
<td width="60%" class="style4" bgcolor="#58bae9" ><div align="center"><font color="#ffffff">图书内容</font></div></td>
<td width="8%" class="style4" height="22" bgcolor="#58bae9" ><div align="center"><font color="#ffffff">出版日期</font></div> </td>
<td width="17%" class="style4" bgcolor="#58bae9" ><div align="center"><font color="#ffffff">图书作者</font></div></td>
</tr>
{php}
$i=1;
{/php}
{section name=bbsid loop=$arraybbs}
<tr>
<td height="22" colspan="4" bgcolor='#f5f5f5'></td>
</tr>
<tr>
<td height="22" class="style4" {php} if($i%2==0){ echo "bgcolor='#f5f5f5'";}{/php}>&nbsp;<a href="index.php?id={$arraybbs[bbsid].tb_book_id}" target="_blank">{unhtml content=$arraybbs[bbsid].bookname}</a></td>
<td class="style4" {php} if($i%2==0){ echo "bgcolor='#f5f5f5'";}{/php}><div align="center">{$arraybbs[bbsid].bookintro}</div></td>
<td height="22" class="style4"{php} if($i%2==0){ echo"bgcolor='#f5f5f5'";}{/php}><div align="center">{$arraybbs[bbsid].booktime}</div></td>
<td height="22" class="style4" {php} if($i%2==0){ echo "bgcolor='#f5f5f5'";}{/php}><div align="center">{$arraybbs[bbsid].bookauthor}</div></td>
</tr>
{php}
$i++;
{/php}
{/section}
</table></td>
</tr>
</table>
 <table width="96%" height="5" border="0" align="center" cellpadding="0" cellspacing="0">
 <tr>
 <td bgcolor="#ffffff"></td>
 </tr>
 </table>
 <table width="96%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#cccccc">
 <tr>
 <td bgcolor="#ffffff"><table width="100%" height="23" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#ffffff">
 <tr>
 <td bgcolor="#e9ebeb"><table width="100%" height="22" border="0" align="center" cellpadding="0" cellspacing="0">
 <tr>
 <td class="style4">&nbsp;{$showpage}</td>
 </tr>
 </table></td>
 </tr>
 </table></td>
 </tr>
 </table>
 {/if}
 {if $isbbstell=="f" && $isbbs=="f"}
 <table width="96%" height="30" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#58bae9">
<tr>
<td bgcolor="#ffffff"><div align="center"><font color="#ff0000">{ldelim}暂时没有内容{rdelim}</font></div></td>
</tr>
</table>
{/if}
</td>
</tr>
</table></td>
<td width="88">&nbsp;</td>
</tr>
</table>
<img src="images/sy_21.jpg" width="1003" height="45">
</div>
</body>
</html>

总结:
需切记smarty,adodb文件引入的相关一些路径问题。
在执行文件index.html中的css路径,图片背景路径都是相对于index.php这个执行文件的(如css/css.cs和index.php在同一级),引入方法是:<link rel="stylesheet" href="css/css.css"/>

adodb的分页函数:pageexecute,absolutepage()当前页码,atfirstpage()首页页码,atlastpage()尾页页码。(个人理解)
$_server['php_self']:当前执行的文件。
smarty的register_function()参考另文。