当前位置:首页 > CMS教程 > ecshop > 列表

ecshop中的浏览历史

发布:smiling 来源: PHP粉丝网  添加日期:2014-07-17 17:00:01 浏览: 评论:0 

不难看出,在你每次浏览一件商品的同时,会在左侧中记录您的浏览记录,在ecshop中是通过cookie来记录的,在goods.php里可以查到如下代码:

  1. if (!emptyempty($_COOKIE['ECS']['history'])) 
  2. { 
  3.     $history = explode(',', $_COOKIE['ECS']['history']); 
  4.     array_unshift($history, $goods_id); 
  5.     $history = array_unique($history); 
  6.  
  7.     while (count($history) > $_CFG['history_number']) 
  8.     { 
  9.         array_pop($history); 
  10.     } 
  11.  
  12.     setcookie('ECS[history]', implode(',', $history), gmtime() + 3600 * 24 * 30); 
  13. } 
  14. else 
  15. { 
  16.     setcookie('ECS[history]', $goods_id, gmtime() + 3600 * 24 * 30); 
  17. } 

每一次浏览,都会记录$good_id(商品的id),放到cookie里.

在模版里goods.dwt里是引用了 在history.lbi里可以看到 {insert name='history'},基本上学过smarty的都知道,这是局部不缓冲用到的,那么它肯定存在一个方法:insert_history(),果然,在lib_insert.php中找到了,其实lib_insert.php 就是一个动态内容函数库.

  1. function insert_history() 
  2. { 
  3.     $str = ''; 
  4.  
  5.     if (!emptyempty($_COOKIE['ECS']['history'])) 
  6.     { 
  7.         $where = db_create_in($_COOKIE['ECS']['history'], 'goods_id'); 
  8.         $sql   = 'SELECT goods_id, goods_name FROM ' . $GLOBALS['ecs']->table('goods') . 
  9.                 " WHERE $where AND is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0"; 
  10.         $query = $GLOBALS['db']->query($sql); 
  11.  
  12.         $res = array(); 
  13.         while ($row = $GLOBALS['db']->fetch_array($query)) 
  14.         { 
  15.             $res[$row['goods_id']] = $row; 
  16.         } 
  17.  
  18.         $tureorder = explode(',', $_COOKIE['ECS']['history']); 
  19.  
  20.         foreach ($tureorder AS $key => $val) 
  21.         { 
  22.             $goods_name = htmlspecialchars($res[$val]['goods_name']); 
  23.             if ($goods_name) 
  24.             { 
  25.                 $short_name = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($goods_name, $GLOBALS['_CFG']['goods_name_length']) : $goods_name; 
  26.                 $str .= ' 
  27. ' . $short_name . ' 
  28. '; 
  29.             } 
  30.         } 
  31.     } 
  32.  
  33.     return $str; 
  34. } 

其实该函数返回的字符串就是history.lbi里的所需内容,需要说明一下的是像代码中类似 $_CFG['history_number'],一般是系统定义的常量或是数据库中保存的字段,查了一下发现是在 ecs_shop_config表里,剩下的就是看一下代码,怎么通过商品的id,获取商品的信息了,不再多说,提醒一下,上面用到了几个针对数据函数,如array_unshift,array_unique还是经常遇到的,顺便巩固一下.

Tags: ecshop浏览历史 ecshop记录

分享到: