我做了个新闻列表,但是有个点击量是不需要缓存的,所以我使用了SMARTY的局部缓存功能,但是刷新1下点击量就没了,青大家帮忙下,代码如下
PHP文件
<?php
$Smarty  = new Smarty();
$Smarty ->caching = true;
$Smarty ->cache_lifetime = 30;
$Smarty ->cache_dir = './library/Smarty/cache';
$Smarty ->compile_dir = './library/Smarty/templates_c/';
$Smarty ->template_dir = './templates/index';function smarty_block_nocache($param, $content, &$Smarty){
    return $content;
}
$Smarty->register_block('nocache', 'smarty_block_nocache', false);
$aa=array([0]=>array([title]=>"a",[hit]=>"1"),[1]=>array([title]=>"b",[hit]=>"2"));
$Smarty->assign("news",$aa);
$Smarty->display('index.html');
?>index文件
<{foreach from=$news item=news}>
  <h2>标题:<{$news.title}></h2>
  <h5>浏览: <{nocache}><{$news.hit}><{/nocache}></h5>
<{/foreach}>为什么刷新下浏览就没有值了呢··请帮忙!

解决方案 »

  1.   

    你查下缓存文件就知道了,我估计:foreach上面的都缓存了,第二次并没有news.hit这个变量
      

  2.   

    <{nocache}>
    <{foreach from=$news item=news}>
      <h2>标题:<{$news.title}></h2>
      <h5>浏览: <{$news.hit}></h5>
    <{/foreach}>
    <{/nocache}>
    试试.
      

  3.   

    楼上这方法可行,但是,我新闻列表想把他缓存,就只要浏览这项不缓存,你这样写,整个新闻列表不就没缓存了吗?
    恩,我检查了下第二次news.hit这个变量确实没值,我就搞不懂为什么会没值
      

  4.   

    $aa=array([0]=>array([title]=>"a",[hit]=>"1"),[1]=>array([title]=>"b",[hit]=>"2"));
    ------------------这是什么数组 ? php的语法写数组是这样的吗? 还是楼主把浏览器打印出来的直接贴上去?$aa = array(
       array(
       'title' => 'a',
       'hit'   => '1'
       )
    );//换成这样,有值吗?<{foreach from=$news item=new}>
      <h2>标题:<{$new.title}></h2>
      <h5>浏览: <{nocache}><{$new.hit}><{/nocache}></h5>
    <{/foreach}>
      

  5.   

    上面模板把参数换一下,变成from=$news item=old  
      

  6.   

    我是直接把浏览器的值贴上去的`不好意思``但是改成这样,from=$news item=old ,就直接报错了啊
      

  7.   

    $aa = array(
       array(
       'title' => 'a',
       'hit'   => '1'
       )
    );//换成这样,有值吗?<{foreach from=$news item=new}>
      <h2>标题:<{$new.title}></h2>
      <h5>浏览: <{nocache}><{$new.hit}><{/nocache}></h5>
    <{/foreach}>====================================
    这样还真有值``
      

  8.   

    第一次载入页面,链接数据库获取到了点击量 news.hit 载入页面完成后smarty会创建缓存文件,这是缓存文件中并没有news.hit的值,因为他没缓存对吧, 这时刷新页面,重新载入页面,smarty会判断是否有缓存,判断有缓存直接去读取缓存,所以跳过了连接数据库这步,所以读取不到news.hit,如果想实现,就得在判断有缓存的时候任然去执行查询输出点击数