文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>一个利用模板做输出缓冲+数据库随即抽取显示的例子

一个利用模板做输出缓冲+数据库随即抽取显示的例子

时间:2007-02-17  来源:PHP爱好者

<?php
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
//这是一个简单的缓存例子:
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
//在本页面中需要随即从几个表中抽取N条记录显示, 因为对数据库操作频繁,
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
//所以采用缓存方法, 24小时内保持不变, 超过则重新生成.
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
//本例子还借用了模板的功能来生成页面.
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
//本文有两种随即产生记录的方法, 分别适应索引Id连续和不连续两种情况,
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
//仅供参考, 具体情况请具体分析.
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
//[email protected]
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex

c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
//缓存文件名:
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$cache_file = 'index.inc';
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex

c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
if (time()-filemtime($cache_file) <3600*24){
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
//如果缓存文件的修改时间没超过24小时
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
include ($cache_file);
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
exit;
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
}
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex

c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
//home page of learn
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
require "../lib/template.php";
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
require "../lib/common.php";
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
require "../lib/config.php";
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
//require "common.php";
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
//require "config.php";
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex

c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$IllnessCount = 5;
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$AssayCount = 5;
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$RadiologyCount =5;
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex

c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$IllnessList = &get_rand_field('Illness','Name',$IllnessCount);
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$AssayList = &get_rand_field('Assay','Name',$AssayCount);
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$RadiologyList = &get_rand_field('Radiology','Name',$RadiologyCount);
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex

c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex

c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
function &get_rand_field($table,$field,$n){
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
//$table is identified by Id
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$q = "select count(*) from $table";
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$total = mysql_result(mysql_query($q),0,0);
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$n = min($total,$n);
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex

c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
//如果数据的主索引按Id连续:
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$id_list=array();
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
while (sizeof($id_list)<$n){
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
mt_srand ((double) microtime() * 1000000);   
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$rand = mt_rand(0,$total-1);
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
if (in_array($rand,$id_list))
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
continue;
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$id_list[] = $rand;
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
}
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$q = "select Id,$field from $table where Id in(".implode($id_list,',').")";
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$r = mysql_query($q) or error_die("查询出错");
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
return $r;
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex

c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
/* 如果主索引Id非连续: (中间记录可能被删除过)
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
for ($i=0; $i<$n; $i++){
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$id_lists= implode($id_list,',');
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
mt_srand ((double) microtime() * 1000000);   
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$rand = mt_rand(0,$total-1);
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$q = "select Id,$field from $table ";
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
if ($id_lists != '')
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$q .= "where Id not in($id_lists) ";
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$q .= "limit $rand,1";
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$r = mysql_query($q) or error_die("查询出错");
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$id_list[] = mysql_result($r,0,0);
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
mysql_data_seek($r,0);
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$r_list[] = mysql_fetch_object($r);
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$total--;
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
}
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
return $r_list;
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
*/
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
}
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex

c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
//output to html
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$t = new Template(".", "keep");
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$t->set_file(array("page" => "index.ihtml"));
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex

c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$t->set_var(array(
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
"EmptyBlock" => " "
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
));
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex

c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$t->set_block("page", "Loop_Illness", "a");
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
foreach($IllnessList as $row) {
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$t->set_var(array(
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
"RandIllness" => html_echo($row->Name),
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
"IllnessId" => $row->Id,
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
));
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$t->parse("a", "Loop_Illness", true);
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
}
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
if (sizeof($IllnessList)==0)
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$t->parse("a", "EmptyBlock", true);
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex

c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$t->set_block("page", "Loop_Assay", "loop_assay");
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
foreach($AssayList as $row) {
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$t->set_var(array(
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
"RandAssay" => html_echo($row->Name),
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
"AssayId" => $row->Id,
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
));
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$t->parse("loop_assay", "Loop_Assay", true);
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
}
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
if (sizeof($AssayList)==0)
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$t->parse("loop_assay", "EmptyBlock", true);
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex

c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$t->set_block("page", "Loop_Radiology", "loop_radiology");
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
foreach($RadiologyList as $row) {
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$t->set_var(array(
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
"RandRadiology" => html_echo($row->Name),
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
"RadiologyId" => $row->Id,
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
));
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$t->parse("loop_radiology", "Loop_Radiology", true);
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
}
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
if (sizeof($RadiologyList)==0)
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$t->parse("loop_radiology", "EmptyBlock", true);
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex

c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
//输出新生成的页面
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$t->pparse("out", array("page"));
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex

c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
//保存缓冲页面
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
$fp = fopen($cache_file,'w');
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
fputs($fp,$t->subst("out"));
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
fclose($fp);
c hina it power . c omsIwzv54GIXFTfltxF8dLUO7Ex
?>
php爱好者站 http://www.phpfans.net PHP|MySQL|javascript|ajax|html.
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载