ecshop增长 浏览过该商品的人还浏览了那些商品 功能

这个 功能比较简单 可是须要增长一个 数据


  1. CREATE TABLE `sc_history` (
  2. `id` int(10) unsigned NOT NULL auto_increment,
  3. `user_id` int(10) unsigned NOT NULL,
  4. `goods_id` int(10) NOT NULL,
  5. PRIMARY KEY (`id`)
  6. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
复制代码



首先在goods.php中找到


  1. /*更新浏览历史*/
  2. $sql = “select count(*) from “.$GLOBALS['ecs']->table(‘history’).” where goods_id=’$goods_id’”;
  3. $num = $GLOBALS['db']->getOne($sql);
  4. if(empty($num)){
  5. $user_id = !empty($_SESSION['user_id'])?intval($_SESSION['user_id']):0;
  6. $sql = “insert into “.$GLOBALS['ecs']->table(‘history’).” (goods_id,user_id) values(‘$goods_id’,'$user_id’)”;
  7. $GLOBALS['db']->query($sql);
  8. }
复制代码


将浏览商品放入 数据库
在 lib_insert中增长一个函数

  1. insert_db_history();
  2. function insert_db_history()
  3. {
  4. $str = ”;
  5. $res = array();
  6. $sql = “select goods_id from “.$GLOBALS['ecs']->table(‘history’);
  7. $res = $GLOBALS['db']->getAll($sql);
  8. $ins = ”;
  9. foreach($res as $key=>$val){
  10. $ins .= $val['goods_id'].’,';
  11. }
  12. $ins = rtrim($ins,’,');
  13. if (!empty($ins))
  14. {
  15. $where = db_create_in($ins, ‘goods_id’);
  16. $sql = ‘SELECT goods_id, goods_name, goods_thumb, shop_price FROM ‘ . $GLOBALS['ecs']->table(‘goods’) .
  17. ” WHERE $where AND is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0″;
  18. $query = $GLOBALS['db']->query($sql);
  19. $res = array();
  20. while ($row = $GLOBALS['db']->fetch_array($query))
  21. {
  22. $goods['goods_id'] = $row['goods_id'];
  23. $goods['goods_name'] = $row['goods_name'];
  24. $goods['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
  25. $goods['goods_thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
  26. $goods['shop_price'] = price_format($row['shop_price']);
  27. $goods['url'] = build_uri(‘goods’, array(‘gid’=>$row['goods_id']), $row['goods_name']);
  28. $res[] = $goods;
  29. }
  30. }
  31. $GLOBALS['smarty']->assign(‘db_history’,$res);//
  32. $output = $GLOBALS['smarty']->fetch(‘library/db_history.lbi’);
  33. return $output;
  34. }
复制代码



在增长一个db_history.lbi就完成了 全部功能了。
若是你想添加到任何 页面的话 值须要将{insert name=”db_history”}增长到你须要的位置就能够了。
可是这个功能会致使你的数据表一直增长,因此适用于商品不是太多的 网站,若是商品多的话只要定时更新数据表便可。
相关文章
相关标签/搜索