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”}增加到你需要的位置就可以了。
但是这个功能会导致你的数据表一直增加,所以适用于商品不是太多的 网站,如果商品多的话只要定时更新数据表即可。

转载于:https://my.oschina.net/u/561668/blog/69045

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值