24.5 公共文件设计

24.5 公共文件设计

视频讲解:光盘TMlx24公共文件设计.exe

公共模块就是将多个页面都可能使用到的代码写成单独的文件,在使用时只要用include或require语句将文件包含进来即可。如本系统中的数据库连接、管理和分页类文件,Smarty模板配置类文件,类的实例化文件,css样式表文件,JS脚本文件等。以前台系统为例,下面给出主要的公共文件,后台的公共文件与前台大同小异。

24.5.1 数据库连接、管理和分页类文件

在数据库连接、管理和分页类文件中,定义三个类,分别是ConDB数据库连接类,实现通过PDO连接mysql数据库;AdminDB数据库管理类,使用PDO类库中的方法执行对数据库中数据的查询、添加、更新和删除操作;SepPage分页类,用于对商城中的数据进行分页输出。

【例24.1】代码位置:光盘TMsl24systemsystem.class.inc.php

<?php //数据库连接类 class ConnDB{ var $dbtype; var $host; var $user; var $pwd; var $dbname; //构造方法 function ConnDB($dbtype, $host, $user, $pwd, $dbname){ $this->dbtype=$dbtype; $this->host=$host; $this->user=$user; $this->pwd=$pwd; $this->dbname=$dbname; } //实现数据库的连接并返回连接对象 function GetConnId(){ if($this->dbtype=="mysql" || $this->dbtype=="mssql"){ $dsn="$this->dbtype:host=$this->host; dbname=$this->dbname"; }else{ $dsn="$this->dbtype:dbname=$this->dbname"; } try {//初始化一个PDO对象,就是创建了数据库连接对象$pdo $conn = new PDO($dsn, $this->user, $this->pwd); $conn->query("set names utf8"); return $conn; } catch (PDOException $e) { die ("Error! : " . $e->getMessage() . "<br/>"); } } } //数据库管理类 class AdminDB{ function ExecSQL($sqlstr, $conn){ $sqltype=strtolower(substr(trim($sqlstr),0,6)); $rs=$conn->prepare($sqlstr); //准备查询语句 $rs->execute(); //执行查询语句,并返回结果集 if($sqltype=="select"){ $array=$rs->fetchAll(PDO::FETCH_ASSOC); //获取结果集中的所有数据 if(count($array)==0 || $rs==false) return false; else return $array; }elseif ($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 ShowData($sqlstr, $conn, $pagesize, $nowpage){ //定义方法 if(! isset($nowpage)||$nowpage=="") //判断变量值是否为空 $this->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); @$this->array=$this->rs->GetRows(); //获取记录数 if(count($this->array)==0 || $this->rs==false) return false; else return $this->array; } function ShowPage($contentname, $utits, $anothersearchstr, $anothersearchstrs, $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&parameter1=".$anothersearchstr."&parameter2=".$anothersearchst rs." 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)."&parameter1=".$anothersearchstr."& parameter2=".$anothersearchstrs." 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)."&parameter1=".$anothersearchstr."& parameter2=".$anothersearchstrs." class=".$class.">下一页</a>"; else $str.="<font color='#555555'>下一页</font>"; $str.="&nbsp; "; if(! $this->rs->AtLastPage()) $str.="<a href=".$_SERVER['PHP_SELF']."? page=".$pagecount."&parameter1=".$anothersearchstr."&parameter2=".$an othersearchstrs." class=".$class.">尾页</a>"; else $str.="<font color='#555555'>尾页</font>"; if(count($this->array)==0 || $this->rs==false) return ""; else return $str; } } ?>

24.5.2 Smarty模板配置类文件

在Smarty模板配置类文件中配置Smarty模板文件、临时文件、配置文件等文件路径。system.smarty.inc.php文件的代码如下:

【例24.2】代码位置:光盘TMsl24systemsystem.smarty.inc.php

<?php require("smarty/Smarty.class.php"); //调用Smarty类文件 class SmartyProject extends Smarty{ //定义类,继承Smarty父类 function SmartyProject(){ //定义方法,配置Smarty模板 $this->template_dir="./"; //指定模板文件存储在根目录下 $this->compile_dir="./system/templates_c/"; //指定编译文件存储位置 $this->config_dir="./system/configs/"; //指定配置文件存储位置 $this->cache_dir="./system/cache/"; //指定缓存文件存储位置 } } ?>

24.5.3 执行类的实例化文件

在system.inc.php文件中,通过require语句包含system.smarty.inc.php和system.class.inc.php文件,执行类的实例化操作,并定义返回对象。完成数据库连接类的实例化后,调用其中的GetConnId方法连接数据库。system.inc.php文件的代码如下:

【例24.3】代码位置:光盘TMsl24systemsystem.inc.php

<?php require("system.smarty.inc.php"); //包含Smarty配置类 require("system.class.inc.php"); //包含数据库连接和操作类 $connobj=new ConnDB("mysql", "localhost", "root", "111", "db_database24"); //数据库连接类实例化 $conn=$connobj->GetConnId(); //执行连接操作,返回连接标识 $admindb=new AdminDB(); //数据库操作类实例化 $seppage=new SepPage(); //分页类实例化 $usefun=new UseFun(); //使用常用函数类实例化 $smarty=new SmartyProject(); //调用Smarty模板 function unhtml($params){ extract($params); $text=$content; global $usefun; return $usefun->UnHtml($text); } $smarty->register_function("unhtml", "unhtml"); //注册模板函数 ?>

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

微信扫一扫

微信扫一扫

微信扫一扫,分享到朋友圈

24.5 公共文件设计