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." ".$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,文章字符转换处理的类:
class usefun - {
-
- function unhtml($text)
- {
- $content=(nl2br(htmlspecialchars($text)));//htmlspecialchars() 函数把一些预定义的字符转换为 html 实体,nl2br() 函数在字符串中的每个新行 (/n) 之前插入 html 换行符 (
)。 $content=str_replace("[strong]","",$content);- $content=str_replace("[/strong]","
",$content); - $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=宋体]","",$content);
- $content=str_replace("[font face=隶书]","",$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]","",$content);
$content=str_replace("[fieldset][legend]"," ",$content);- return $content;
- }
- }
将以上四个类全部放到一个类文件system.class.inc.php里.
另外几个文件:
1、system.inc.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:
- 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()参考另文。 |