参照学习的是《PHP开发典型模块大全》这本书里的例子。
smarty+adodb+php分页,模板设计采用的是smarty模板,链接数据库使用的是ADODB技术,由于PHP初学,smarty和adodb也是初次接触,现记录学习所得,备忘之。
大体的设计思路是这样的:下载smarty,adodb,到站点根目录page下的fenye文件夹下。
Smarty:
在smarty文件夹下分别建立三个文件夹templates,templates_c,smarty_cache.
如图:
对smarty进行配置操作的文件,Smarty_inc.php,代码如下:
<?php
include_once("Smarty.class.php");
class SmartyProject extends Smarty
{
function __construct()
{
$this->config_dir="smarty/Smarty/Config_File.class.php";
$this->caching=false;
$this->template_dir = "smarty/templates/";
$this->compile_dir = "smarty/templates_c/";
$this->cache_dir = "smarty/smarty_cache/";
$this->left_delimiter = "{";
$this->right_delimiter = "}";
}
}
?>
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();
}
}
数据库操作类:
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;
}
}
}
}
分页类:
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;
}
}
}
另加一个文章字符转换处理的类:
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里.
另外还有几个文件如下:
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");
?>
执行文件,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");
?>
解析文件,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> </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"> </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" > <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}> <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"> {$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"> </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()参考另文。