缓存实例

实现上个作业中的学生成绩不缓存,当数据库中学生成绩发生变化时,页面数据也发生变化。

stu.php

 

<?php

include("connect.php");
include("../libs/Smarty.class.php");
$smarty=new Smarty();
$smarty->caching=1;
$smarty->reInitSmarty("../demo/templates","../demo/templates_c","../demo/config","../demo/cache");

if(!$smarty->is_cached("stu.tpl")){
$q="select * from stu";
$result=mysql_query($q);
$array=array();
$i=0;
while($row=mysql_fetch_assoc($result)){
$array[$i]=$row;
$i++;
};
$smarty->assign("array",$array);
echo "当前模板没有被缓存...";
}
$smarty->display("stu.tpl");

 

?>

 

 

stu.tpl

 

<{config_load file="stu.conf" }>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>学生信息</title>
</head>

<body>
<table align="center" border="1" bgcolor="<{#table_bgcolor#}>" >
<tr bgcolor="<{#head_color#}>">
<th>学号</th><th>姓名</th><th>生日</th><th>成绩</th><th>描述</th>
</tr>

<{section name=stu loop=$array}>
<{if $smarty.section.stu.index is odd}>
<tr bgcolor="<{#row_color1#}>">
<td><{$array[stu].id}></td>
<td><{$array[stu].sname}></td>
<td><{$array[stu].birthday}></td>
<td><{insert name=getStu stu_id=$array[stu].id}></td>
<td><a href="details.php?id=<{$array[stu].id}>">详细信息</a></td>
</tr>
<{else}>
<tr bgcolor="<{#row_color2#}>">
<td><{$array[stu].id}></td>
<td><{$array[stu].sname}></td>
<td><{$array[stu].birthday}></td>
<td><{insert name=getStu stu_id=$array[stu].id}></td>
<td><a href="details.php?id=<{$array[stu].id}>">详细信息</a></td>
</tr>
<{/if}>
<{/section}>
</table>
<br />
最后访问时间:<{insert name=getDate}>
<hr />
利用insert调用getStu查看学生基本信息(不缓存):<{insert name=getStu stuName=$array[0].name}>
</body>
</html>

 

details.php

 

<?php
include("connect.php");
include("../libs/Smarty.class.php");
$smarty=new Smarty();
$smarty->caching=1;
$smarty->reInitSmarty("../demo/templates","../demo/templates_c","../demo/config","../demo/cache");
if(!$smarty->is_cached("stu_details.tpl",$_GET["id"])){
$q="select * from stu where id=".$_GET["id"];
$result=mysql_query($q);
$array=array();
$i=0;
while($row=mysql_fetch_assoc($result)){
$array[$i]=$row;
$i++;
};

$smarty->assign("stu_details",$array);
echo "缓存没有被设置...";
}
$smarty->display("stu_details.tpl",$_GET["id"]);

 

stu_details.tpl

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>学生信息</title>
</head>

<body>
<{section name=stu_detail loop=$stu_details}>
<h1><{$stu_details[stu_detail].sname}>的详细信息</h1>
<hr />
学号:<{$stu_details[stu_detail].id}><br />
姓名:<{$stu_details[stu_detail].sname}><br />
生日:<{$stu_details[stu_detail].birthday}><br />
成绩:<{$stu_details[stu_detail].score}><br />
<{/section}>
</body>
</html>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值