$ 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 = "}";
}
}
?>
Copy Code
2, adodb, connect database class:
class conndb
{
var $dbtype;
var $host; var $user;
var $pwd;var $dbname;
var $debug;//false does not display debugging information, otherwise , display 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;
}
//Implement the database connection and return the connection object (personal understanding can be said to return the adodb object)
function getconnid()
{
include_once('adodb5/adodb.inc.php');
if($this-> dbtype == "mysql" || $this->dbtype == "mssql")
{
if($this->dbtype == "mysql")
{
$this->conn = newadoconnection(" mysql");//Create adodb object and declare the database type as 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()//Close the connection to the database
{
$this->conn->disconnection();
}
}
Copy code
3, Database operation class:
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() function converts the string to lowercase. if($sqltype = "select")
{
$array = $rs->getrows(); // Similar to the array returned by the mysql_fetch_array function
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;
}
}
}
}
Copy code
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.="首页 ";
}
else
{
$str.="首页 ";
}
$str.=" ";
if(!$this->rs->atfirstpage())
{
$str.="rs->absolutepage()-1).$anothersearchstr." class=".$class.">上一页 ";
}
else
{
$str.="上一页 ";
}
$str.=" ";
if(!$this->rs->atlastpage())
{
$str.="rs->absolutepage()+1).$anothersearchstr." class=".$class.">下一页 ";
}
else
{
$str.="下一页 ";
}
$str.=" ";
if(!$this->rs->atlastpage())
{
$str.="尾页 ";
}
else
{
$str.="尾页 ";
}
if(count($this->array)==0 || $this->rs==false)
{
return "";
}
else
{
return $str;
}
}
}
复制代码
5. Classes for article character conversion processing:
class usefun
{
function unhtml($text)
{
$content=(nl2br(htmlspecialchars($text))));//htmlspecialchars() function combines some predefined The characters are converted to HTML entities, and the nl2br() function inserts an HTML newline character ( ) before each new line (/n) in the string.
$content=str_replace("[strong]","",$content);
$content=str_replace("[/strong]","$content=str_replace("[em]","",$content);
$content=str_replace("[/em]","" ,$content);
$content=str_replace("[u]","",$content);
$content=str_replace("[/u]","",$ content);
$content=str_replace("[font color=#ff0000]","",$content);
$content=str_replace("[font color=#00ff00] ","",$content);
$content=str_replace("[font color=#0000ff]","",$content);
$content=str_replace("[font face=混体_gb2312]","",$content);
$content=str_replace("[font face=宋体]","< font face=新宋体>",$content);
$content=str_replace("[font face=official script]","",$content);
$content=str_replace(" [/font]","",$content);
//$content=str_replace(chr(32)," ",$content);
$content=str_replace("[font size=1 ]","",$content);
$content=str_replace("[font size=2]","",$content);
$content= str_replace("[font size=3]","",$content);
$content=str_replace("[font size=4]","", $content);
$content=str_replace("[font size=5]","",$content);
$content=str_replace("[font size=6]","< ;font size=6>",$content);
$content=str_replace("[fieldset][legend]","
",$content) ;
$content=str_replace("[/legend]","",$content);
$content=str_replace("[/fieldset]","",$content) ;
return $content;
}
}
Copy the code
Put all the above four classes into one class file system.class.inc.php.
A few more files:
1. system.inc.php:
session_start();
include_once("smarty_inc.php"); include_once("system.class.inc.php"); //Database connection class Instantiation $connobj = new conndb("mysql","localhost","root","vertrigo","db_fenye",false); $conn = $connobj->getconnid();
//Database operation Class instantiation
$admindb = new admindb();
//Paging class instantiation
$seppage=new seppage();
//Use common function class instantiation
$usefun=new usefun();
//Call smarty template
$smarty=new smartyproject();
function unhtml($params)
{
extract($params);
$text=$content;
global $usefun;
return $usefun->unhtml($text );
}
$smarty->register_function("unhtml","unhtml");
?>
Copy code
2、执行文件,index.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:
页模
{if $isbbs=="t"}
图书名称
图书内容
出版日期
图书作者
{php}
$i=1;
{/php}
{section name=bbsid loop=$arraybbs}
{unhtml content=$arraybbs[bbsid].bookname}
{$arraybbs[bbsid].bookintro}
{$arraybbs[bbsid].booktime}
{$arraybbs[bbsid].bookauthor}
{php}
$i++;
{/php}
{/section}
{/if}
{if $isbbstell=="f" && $isbbs=="f"}
{/if}
复制代码
总结:
需切记smarty,adodb文件引入的相关一些路径问题。
在执行文件index.html中的css路径,图片背景路径都是相对于index.php这个执行文件的(如css/css.cs和index.php在同一级),引入方法是:
adodb的分页函数:pageexecute,absolutepage()当前页码,atfirstpage()首页页码,atlastpage()尾页页码。(个人理解)
$_server['php_self']:当前执行的文件。
smarty的register_function()参考另文。