首页 > php框架 > php smarty模板 foreach循环块及缓存技术

php smarty模板 foreach循环块及缓存技术

function get_product_tree()
{
    $sql="select * from ecs_category";
    $rs = $GLOBALS['db']->getOne($sql);
    foreach ($rs AS $row)
    {
    $rows['cat_id']=$row['cat_id '];
    $rows['cat_name']=$row['cat_name'];

    }
    return $rows;
}

上面是我的代码,我的模板页是

<!--{foreach from=$product item=child}-->
     <li >$child.cat_name}</li>
     <!--{/foreach}-->
这样写怎么不行??? 问题补充  2009-08-11 20:32
<!--{foreach from=$product item=child}-->
     <li >{$child.cat_name}</li>
     <!--{/foreach}-->
<!--{foreach from=$product item=child}-->
     <li >$child.cat_name}</li>
     <!--{/foreach}-->

-------------------------------------------

<li> 标签附近是不是少了一个{的标界符?

 

检举

回答人的补充   2009-08-11 20:38 <!--{foreach from=$product item=child}-->

     <li ><!--{$child.cat_name}--></li>

 

PHP缓存技术工作原理:  首先看看adodb提供的数据缓存功能:

<?php
//load code common to ADOdb
include ('adodb.inc.php');
$ADODB_CACHE_DIR = '/usr/ADODB_cache';
// create a connection
$conn = & ADONewConnection('mysql');
// connect to MySQL, agora db
$conn->PConnect(' http://www.aboutstudy.net ', 'userid', '', 'agora');
$sql = 'select CustomerName, CustomerID from customers';
$rs = $conn->CacheExecute(15, $sql);
?>

如上,每次查询数据的时候,会把相应的结果序列化后保存到文件中,以后同样的查询语句就可以不用直接查询数据库,而是从缓存文件中获得。

再来看看Smarty提供的页面缓存功能:

<?php
require ('Smarty.class.php');
$smarty = new Smarty;
$smarty->caching = true;
if (!$smarty->is_cached('index.tpl')) {
    // No cache available, do variable assignments here.
    $contents = get_database_contents();
    $smarty->assign($contents);
}
$smarty->display('index.tpl');
?>

如上,每次访问页面的时候,都会先检测相应的缓存是否存在,如果不存在,就连接数据库,得到数据,完成模板变量的赋值

显示页面,同时生成缓存文件,这样下次访问的时候缓存文件就发挥作用了,而不会再执行if块的数据查询语句了。

当然,在实际使用中会有很多东西要考虑,比如,有效期的设置,缓存组的设置等等,具体可以查看Smarty手册中有关缓存(caching)的相关章节。

以上两个PHP流行组件缓存方式的侧重点是不同的,对于Adodb的缓存而言,它缓存的是数据,对于Smarty的缓存而言,它缓存的是页面。

其他提供缓存功能的组件还有很多(如:PEAR::Cache_Lite等等),实际编程中使用哪个方案要具体情况具体分析,也可能会综合使用。


本文地址:http://www.phprm.com/frame/smarty_foreach.html

转载随意,但请附上文章地址:-)

标签:none

发表留言