Smarty静态缓存

2025-11-13 13:24:08

1、先编辑PHP文件来查询显示数据库当中的文件;

<?php//定义 相对于这个页面的一个文件保存路径$filname ="../cache/huancun.html";//查询数据并显示include("../init.inc.php");include("../dbda.class.php");$db= new dbda();$sql = "select * from car";$arr = $db->query($sql);$smarty->assign("shuzu",$arr);$smarty->display("huancun.html");//调用display模版显示

2、建立HTML用来显示的样式

<h1>数据列表</h1><table width="100%" border="1" cellpadding="0" cellspacing="0">    <tr>        <td>代号</td>        <td>名称</td>        <td>操作</td>    </tr>  <{foreach $shuzu as $v}>      <tr>        <td><{$v[0]}></td>        <td><{$v[1]}></td>        <td>操作</td>    </tr>  <{/foreach}>

3、在PHP文件中加入缓存的代码; 打开网页就会显示,刷新一遍就会直接取问价夹中找了

<?php//定义 相对于这个页面的一个文件保存路径$filname ="../cache/huancun.html";//缓存文件存放的位置//判断缓存文件是否存在,如果缓存文件存在直接调用缓存,如果缓存文件不存在,重新缓存if(file_exists($filname))//判断文件是否存在{    include($filname);//直接拿来显示    }    else{        //重新缓存        ob_start();//开启内存缓存                //查询数据并显示include("../init.inc.php");include("../dbda.class.php");$db= new dbda();$sql = "select * from car";$arr = $db->query($sql);$smarty->assign("shuzu",$arr);$smarty->display("huancun.html");//调用display模版显示        //从缓存里取内容$str = ob_get_contents();        file_put_contents($filname,$str);//保存缓存                ob_flush();//关闭内存缓存        echo"WOWKWJWJWKJWKWJKWJWKWJWKJWKJW";                }

4、给页面加分页

$sall = "select count(*) from car ";$zts = $db->Strquery($sall);include("../page.class.php");$page = new Page($zts,5);$smarty->assign("fpage",$page->fpage());

让页面默认选第一页; 因为不可能只访问一个一页面所以要实现多个页面;

$p = 1;if(!empty($_GET["page"])){   $p = $_GET["page"];}//定义 相对于这个页面的一个文件保存路径$filname ="../cache/huancun{$p}.html";//缓存文件存放的位置

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
相关推荐
  • 阅读量:69
  • 阅读量:186
  • 阅读量:100
  • 阅读量:163
  • 阅读量:70
  • 猜你喜欢