分享下smarty+adodb+php分页方法,模板设计采用的是smarty模板,链接数据库使用的是adodb技术。
设计思路:
下载smarty,adodb,到站点根目录page下的fenye文件夹下。
smarty:
在smarty文件夹下分别建立三个文件夹templates,templates_c,smarty_cache.
如图:
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,连接数据库类:
3,数据库操作类:
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." ".$record." ".$utits." 每页显示 ".$this->pagesize." ".$utits." 第 ".$this->rs->absolutepage()." 页/共 ".$pagecount." 页";
$str.=" ";
if(!$this->rs->atfirstpage())
{
$str.="<a href=".$_server['php_self']."?page=1".$anothersearchstr." 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).$anothersearchstr." 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).$anothersearchstr." class=".$class.">下一页</a>";
}
else
{
$str.="<font color='#555555'>下一页</font>";
}
$str.=" ";
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:
3、解析文件,index.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()参考另文。