- CREATE TABLE `sc_history` (
- `id` int(10) unsigned NOT NULL auto_increment,
- `user_id` int(10) unsigned NOT NULL,
- `goods_id` int(10) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
首先在goods.php中找到
- /*更新浏览历史*/
- $sql = “select count(*) from “.$GLOBALS['ecs']->table(‘history’).” where goods_id=’$goods_id’”;
- $num = $GLOBALS['db']->getOne($sql);
- if(empty($num)){
- $user_id = !empty($_SESSION['user_id'])?intval($_SESSION['user_id']):0;
- $sql = “insert into “.$GLOBALS['ecs']->table(‘history’).” (goods_id,user_id) values(‘$goods_id’,'$user_id’)”;
- $GLOBALS['db']->query($sql);
- }
将浏览的 商品放入 数据库。
在 lib_insert中增加一个函数
- insert_db_history();
- function insert_db_history()
- {
- $str = ”;
- $res = array();
- $sql = “select goods_id from “.$GLOBALS['ecs']->table(‘history’);
- $res = $GLOBALS['db']->getAll($sql);
- $ins = ”;
- foreach($res as $key=>$val){
- $ins .= $val['goods_id'].’,';
- }
- $ins = rtrim($ins,’,');
- if (!empty($ins))
- {
- $where = db_create_in($ins, ‘goods_id’);
- $sql = ‘SELECT goods_id, goods_name, goods_thumb, shop_price FROM ‘ . $GLOBALS['ecs']->table(‘goods’) .
- ” WHERE $where AND is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0″;
- $query = $GLOBALS['db']->query($sql);
- $res = array();
- while ($row = $GLOBALS['db']->fetch_array($query))
- {
- $goods['goods_id'] = $row['goods_id'];
- $goods['goods_name'] = $row['goods_name'];
- $goods['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
- $goods['goods_thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
- $goods['shop_price'] = price_format($row['shop_price']);
- $goods['url'] = build_uri(‘goods’, array(‘gid’=>$row['goods_id']), $row['goods_name']);
- $res[] = $goods;
- }
- }
- $GLOBALS['smarty']->assign(‘db_history’,$res);//
- $output = $GLOBALS['smarty']->fetch(‘library/db_history.lbi’);
- return $output;
- }
在增加一个db_history.lbi就完成了 所有功能了。
如果你想添加到任何 页面的话 值需要将{insert name=”db_history”}增加到你需要的位置就可以了。
但是这个功能会导致你的数据表一直增加,所以适用于商品不是太多的 网站,如果商品多的话只要定时更新数据表即可。