2016/05/05 smarty ①分页 ② 查询后分页 ③缓存
samrty 分页 查询后分页
0505fch.php
1 <?php 2 include("init.inc.php"); 3 include("DBDA.php"); 4 include("fpage.class.php"); 5 6 $cx=""; 7 $yuju=""; 8 $name=""; 9 if (empty($_POST["name"])) { 10 if (!empty($_GET["name"])) { 11 $name=$_GET["name"]; 12 $yuju="where AreaName like '%{$name}%' "; 13 } 14 } 15 else 16 { 17 $name=$_POST["name"]; 18 $cx="name={$name}"; 19 $yuju="where AreaName like '%{$name}%' "; 20 } 21 $smarty->assign("name",$name); 22 23 $db=new DBDA(); 24 $sqlall="select count(*) from ChinaStates ".$yuju; 25 26 $total=$db->StrQuery($sqlall,1,"test2"); 27 28 $page=new Page($total,10,$cx,true); 29 //true 从第一页开始显示 第三个"" 是做查询用的 30 31 $sql="select * from chinastates ".$yuju.$page->limit; 32 $attr=$db->Query($sql,1,"test2"); 33 34 $fp=$page->fpage(); 35 36 $smarty->assign("shuju",$attr); 37 $smarty->assign("fp",$fp); 38 39 40 $smarty->display("0505fch.html"); 41 ?>
0505fch.html 在模板 templates 中
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 </head> 7 <body> 8 <h1>主页面</h1> 9 10 <table width="100%" border="1" cellpadding="0" cellspacing="0"> 11 <tr> 12 <td>代号</td> 13 <td>姓名</td> 14 <td>生日</td> 15 </tr> 16 <{foreach $shuju as $sj}> 17 <tr> 18 <td><{$sj[0]}></td> 19 <td><{$sj[1]}></td> 20 <td><{$sj[4]}></td> 21 </tr> 22 <{/foreach}> 23 </table> 24 25 </body> 26 </html>
DBDA.php
1 <?php 2 3 class DBDA 4 { 5 public $host = "localhost"; //服务器地址 6 public $uid = "root"; //数据库的用户名 7 public $pwd = "123"; //数据库的密码 8 9 //执行SQL语句,返回相应结果的函数 10 //$sql是要执行的SQL语句 11 //$type是SQL语句的类型,0代表增删改,1代表查询 12 //$db代表要操作的数据库 13 public function Query($sql,$type,$db) 14 { 15 //造连接对象 16 $conn = new MySQLi($this->host,$this->uid,$this->pwd,$db); 17 18 //判断连接是否成功 19 !mysqli_connect_error() or die("连接失败!"); 20 21 //执行SQL语句 22 $result = $conn->query($sql); 23 24 //判断SQL语句类型 25 if($type==1) 26 { 27 //如果是查询语句返回结果集的二维数组 28 return $result->fetch_all(); 29 } 30 else 31 { 32 //如果是其他语句,返回true或false 33 return $result; 34 } 35 } 36 37 //Ajax调用返回JSON 38 public function JsonQuery($sql,$type=1,$db="test2") 39 { 40 //定义数据源 41 $dsn = "mysql:dbname={$db};host={$this->host}"; 42 //造pdo对象 43 $pdo = new PDO($dsn,"{$this->uid}","{$this->pwd}"); 44 45 46 //准备执行SQL语句 47 $st = $pdo->prepare($sql); 48 49 //执行预处理语句 50 if($st->execute()) 51 { 52 if($type==1) 53 { 54 $attr = $st->fetchAll(PDO::FETCH_ASSOC); 55 return json_encode($attr); 56 } 57 else 58 { 59 if($st) 60 { 61 return "OK"; 62 } 63 else 64 { 65 return "NO"; 66 } 67 } 68 69 } 70 else 71 { 72 echo "执行失败!"; 73 } 74 75 76 77 } 78 //Ajax调用返回字符串 79 public function StrQuery($sql,$type,$db) 80 { 81 //造连接对象 82 $conn = new MySQLi($this->host,$this->uid,$this->pwd,$db); 83 84 //判断连接是否成功 85 !mysqli_connect_error() or die("连接失败!"); 86 87 //执行SQL语句 88 $result = $conn->query($sql); 89 90 //判断SQL语句类型 91 if($type==1) 92 { 93 $attr = $result->fetch_all(); 94 $str = ""; 95 //如果是查询语句返回字符串 96 for($i=0;$i<count($attr);$i++) 97 { 98 for($j=0;$j<count($attr[$i]);$j++) 99 { 100 $str = $str.$attr[$i][$j]; 101 $str = $str."^"; 102 } 103 $str = substr($str,0,strlen($str)-1); 104 $str = $str."|"; 105 } 106 $str = substr($str,0,strlen($str)-1); 107 108 return $str; 109 } 110 else 111 { 112 //如果是其他语句,返回true或false 113 if($result) 114 { 115 return "OK"; 116 } 117 else 118 { 119 return "NO"; 120 } 121 } 122 } 123 124 125 }
init.inc.php
1 <?php 2 3 define("ROOT",str_replace("\\","/",dirname(__FILE__)).'/'); //常量ROOT中指定项目根目录 4 5 //echo str_replace("\\","/",dirname(__FILE__))."/"; 6 7 require ROOT.'libs/Smarty.class.php'; //加载Smarty类文件 8 9 $smarty = new Smarty(); //实例化Smarty对象<br> 10 11 12 //$smarty -> auto_literal = false; //就可以让定界符号使用空格 13 $smarty->setTemplateDir(ROOT.'templates/'); //设置所有模板文件存放位置 14 //$smarty->addTemplateDir(ROOT.'templates2/'); //添加一个模板文件夹 15 $smarty->setCompileDir(ROOT.'templates_c/'); //设置编译过的模板存放的目录 16 17 $smarty->addPluginsDir(ROOT.'plugins/'); //设置为模板扩充插件存放目录 18 $smarty->setCacheDir(ROOT.'cache/'); //设置缓存文件存放目录 19 $smarty->setConfigDir(ROOT.'configs/'); //设置模板配置文件存放目录 20 21 $smarty->caching = false; //设置Smarty缓存开关功能 22 $smarty->cache_lifetime = 60*60*24; //设置缓存模板有效时间一天 23 $smarty->left_delimiter = '<{'; //设置模板语言中的左结束符 24 $smarty->right_delimiter = '}>'; //设置模板语言中的右结束符 25 26 27 28 29 30 ?>
显示效果:
1,进入主页初始化
2,输入查询条件
3,显示查询结果
缓存
huancun.php
1 <?php 2 3 define("ROOT",str_replace("\\","/",dirname(__FILE__)).'/'); //常量ROOT中指定项目根目录 4 5 //echo str_replace("\\","/",dirname(__FILE__))."/"; 6 7 require ROOT.'libs/Smarty.class.php'; //加载Smarty类文件 8 9 $smarty = new Smarty(); //实例化Smarty对象<br> 10 11 12 //$smarty -> auto_literal = false; //就可以让定界符号使用空格 13 $smarty->setTemplateDir(ROOT.'templates/'); //设置所有模板文件存放位置 14 //$smarty->addTemplateDir(ROOT.'templates2/'); //添加一个模板文件夹 15 $smarty->setCompileDir(ROOT.'templates_c/'); //设置编译过的模板存放的目录 16 17 $smarty->addPluginsDir(ROOT.'plugins/'); //设置为模板扩充插件存放目录 18 $smarty->setCacheDir(ROOT.'cache/'); //设置缓存文件存放目录 19 $smarty->setConfigDir(ROOT.'configs/'); //设置模板配置文件存放目录 20 21 $smarty->caching = false; //设置Smarty缓存开关功能 22 $smarty->cache_lifetime = 60*60*24; //设置缓存模板有效时间一天 23 $smarty->left_delimiter = '<{'; //设置模板语言中的左结束符 24 $smarty->right_delimiter = '}>'; //设置模板语言中的右结束符 25 26 27 28 29 30 ?>
huancun.html 在templates模板文件夹中
1 <?php 2 include("init.inc.php"); 3 //缓存 4 $file="./cache/newhuncun.html"; 5 //当前页面对应的缓存页面 6 $cachetime=10;//定义缓存时间 7 if (!file_exists($file)||(filemtime($file)+$cachetime<time())) 8 //缓存文件创建时间加缓存时间 小于当前时间 9 { 10 ob_start();//开启内存 11 include("DBDA.php"); 12 $db=new DBDA(); 13 $sql="select * from Info"; 14 $attr=$db->Query($sql,1,"test2"); 15 $smarty->assign("shuju",$attr); 16 $smarty->display("huancun.html"); 17 $neirong=ob_get_contents(); 18 //从内存中获取内容 19 file_put_contents($file,$neirong); 20 //写入文件 21 22 23 ob_flush();//关闭内存 24 echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"; 25 } 26 else 27 { 28 include("$file"); 29 } 30 31 ?>
1,当第一次出现时 没有缓存文件 下面会显示 @@@@@@@@@@@ 行
2,当cache 缓存文件中有缓存文件后 不再显示 @@@@@@@@@行