下一篇 » « 上一篇

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

作者:爱好者    时间:2008-01-22    来源:php之家    点击:6294    本文共1篇文章 字体:[ ]
标签:php模版

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

<?php

//这是一个简单的缓存例子:

//在本页面中需要随即从几个表中抽取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"; www.444p.com

require "../lib/config.php";

//require "common.php";

//require "config.php";



$IllnessCount = 5;

$AssayCount = 5;

$RadiologyCount =5;

www.444p.com





$IllnessList = &get_rand_field('Illness','Name',$IllnessCount);

$AssayList = &get_rand_field('Assay','Name',$AssayCount);

$RadiologyList = &get_rand_field('Radiology','Name',$RadiologyCount); www.444p.com





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);

php学习之家



    $n = min($total,$n);



//如果数据的主索引按Id连续:

    $id_list=array();

    while (sizeof($id_list)<$n){ www.444p.com

        mt_srand ((double) microtime() * 1000000);   

        $rand
= mt_rand(0,$total-1);

        if (in_array($rand,$id_list))
php学习之家


            continue;

        $id_list[] = $rand;

    }

    $q = "select Id,$field from $table where Id in(".implode($id_list,',').")"; php学习之家

    $r = mysql_query($q) or error_die("查询出错");

    return $r;



/* 如果主索引Id非连续: (中间记录可能被删除过)

    for ($i=0; $i<$n; $i++){

        $id_lists= implode($id_list,',');

        mt_srand ((double) microtime() * 1000000);   

        $rand = mt_rand(0,$total-1); 本文来自 www.444p.com

        $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
www.444p.com版权所有


$t
= new Template(".", "keep");

$t->set_file(array("page" => "index.ihtml"));



$t->set_var(array( www.444p.com

    "EmptyBlock" => " "

    ));



$t->set_block("page", "Loop_Illness", "a");

foreach($IllnessList as $row) {

www.444p.com php学习之家



    $t->set_var(array(

        "RandIllness" => html_echo($row->Name),

        "IllnessId" => $row->Id,
php学习之家http://www.444p.com


        ));

    $t->parse("a", "Loop_Illness", true);

}

if (sizeof($IllnessList)==0) php学习之家

    $t->parse("a", "EmptyBlock", true);



$t->set_block("page", "Loop_Assay", "loop_assay");

foreach($AssayList as $row) {

php学习之家



    $t->set_var(array(

        "RandAssay" => html_echo($row->Name),

        "AssayId" => $row->Id, php学习之家

        ));

    $t->parse("loop_assay", "Loop_Assay", true);

}

if (sizeof($AssayList)==0) www.444p.com php学习之家

    $t->parse("loop_assay", "EmptyBlock", true);



$t->set_block("page", "Loop_Radiology", "loop_radiology");

foreach($RadiologyList as $row) { php学习之家

    $t->set_var(array(

        "RandRadiology" => html_echo($row->Name),

        "RadiologyId" => $row->Id, www.444p.com

        ));

    $t->parse("loop_radiology", "Loop_Radiology", true);

}

if (sizeof($RadiologyList)==0)

    $t->parse("loop_radiology", "EmptyBlock", true);



//输出新生成的页面

$t
->pparse("out", array("page"));

php学习之家





//保存缓冲页面

$fp
= fopen($cache_file,'w');

fputs($fp,$t->subst("out"));

fclose($fp);

?&
发表评论
密码: (游客不需要密码)
记住我【Alt+S 或 Ctrl+Enter 快速提交】

搜索工具


热门搜索: [html][html][else][MYsql][mysql][调用][date][print][配置][配置]

《PHP精通》点击排行