一个利用模板做输出缓冲+数据库随即抽取显示的例子_PHP
- WBOYOriginal
- 2016-06-01 12:32:421034browse
//这是一个简单的缓存例子:
//在本页面中需要随即从几个表中抽取N条记录显示, 因为对数据库操作频繁,
//所以采用缓存方法, 24小时内保持不变, 超过则重新生成.
//本例子还借用了模板的功能来生成页面.
//本文有两种随即产生记录的方法, 分别适应索引Id连续和不连续两种情况,
//仅供参考, 具体情况请具体分析.
//steeven@kali.com.cn
//缓存文件名:
$cache_file = 'index.inc';
if (time()-filemtime($cache_file) 3600*24){
//如果缓存文件的修改时间没超过24小时
include ($cache_file);
exit;
}
//home page of learn
require "../lib/template.php";
require "../lib/common.php";
require "../lib/config.php";
//require "common.php";
//require "config.php";
$IllnessCount = 5;
$AssayCount = 5;
$RadiologyCount =5;
$IllnessList = &get_rand_field('Illness','Name',$IllnessCount);
$AssayList = &get_rand_field('Assay','Name',$AssayCount);
$RadiologyList = &get_rand_field('Radiology','Name',$RadiologyCount);
function &get_rand_field($table,$field,$n){
//$table is identified by Id
$q = "select count(*) from $table";
$total = mysql_result(mysql_query($q),0,0);
$n = min($total,$n);
//如果数据的主索引按Id连续:
$id_list=array();
while (sizeof($id_list)$n){
mt_srand ((double) microtime() * 1000000);
$rand = mt_rand(0,$total-1);
if (in_array($rand,$id_list))
continue;
$id_list[] = $rand;
}
$q = "select Id,$field from $table where Id in(".implode($id_list,',').")";
$r = mysql_query($q) or error_die("查询出错");
return $r;
/* 如果主索引Id非连续: (中间记录可能被删除过)
for ($i=0; $i
$id_lists= implode($id_list,',');
mt_srand ((double) microtime() * 1000000);
$rand = mt_rand(0,$total-1);
$q = "select Id,$field from $table ";
if ($id_lists != ')
$q .= "where Id not in($id_lists) ";
$q .= "limit $rand,1";
$r = mysql_query($q) or error_die("查询出错");
$id_list[] = mysql_result($r,0,0);
mysql_data_seek($r,0);
$r_list[] = mysql_fetch_object($r);
$total--;
}
return $r_list;
*/
}
//output to html
$t = new Template(".", "keep");
$t->set_file(array("page" => "index.ihtml"));
$t->set_var(array(
"EmptyBlock" => " "
));
$t->set_block("page", "Loop_Illness", "a");
foreach($IllnessList as $row) {
$t->set_var(array(
"RandIllness" => html_echo($row->Name),
"IllnessId" => $row->Id,
));
$t->parse("a", "Loop_Illness", true);
}
if (sizeof($IllnessList)==0)
$t->parse("a", "EmptyBlock", true);
$t->set_block("page", "Loop_Assay", "loop_assay");
foreach($AssayList as $row) {
$t->set_var(array(
"RandAssay" => html_echo($row->Name),
"AssayId" => $row->Id,
));
$t->parse("loop_assay", "Loop_Assay", true);
}
if (sizeof($AssayList)==0)
$t->parse("loop_assay", "EmptyBlock", true);
$t->set_block("page", "Loop_Radiology", "loop_radiology");
foreach($RadiologyList as $row) {
$t->set_var(array(
"RandRadiology" => html_echo($row->Name),
"RadiologyId" => $row->Id,
));
$t->parse("loop_radiology", "Loop_Radiology", true);
}
if (sizeof($RadiologyList)==0)
$t->parse("loop_radiology", "EmptyBlock", true);
//输出新生成的页面
$t->pparse("out", array("page"));
//保存缓冲页面
$fp = fopen($cache_file,'w');
fputs($fp,$t->subst("out"));
fclose($fp);
?&
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn