tp商城(10)

本文介绍了一种在商品详情页中记录用户浏览历史的方法,通过使用PHP将商品信息存储到用户的session中,实现商品浏览历史的功能。具体步骤包括检查session是否存在、创建历史记录并限制记录数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


tp 浏览信息!!!!!!!!!


//放在session 不能放在变量里记录 因为 变量会销毁


----------------------------------
每次浏览一条商品 就把 session 信息存起来
应该在 goods 方法下 存起来

public function goods(){
    $goods = D('Goods');
    $g = $goods->cache(true)->find(I('get.goods_id'));
    if(empty($g)) {
        $this->redirect('/');
    }
    $this->assign('g' , $g);

    if(IS_POST) {
        $comm = M('Comment');
        $comm->create(); // 先收取POST,收到content,email
        $comm->user_id = cookie('user_id') ? cookie('user_id') : 0;
        $comm->pubtime = time();
        $comm->goods_id = $goods->goods_id;
        //print_r($g);exit;
        $comm->add();
    }

    // 取商品的评论
    //$comm->where('goods_id='.$goods->goods_id)->order('pubtime desc');
    $this->assign('comm' , $goods->relationGet('Comment'));

    $cat = D('Cat');
    $this->assign('cats' , $cat->getMbx($g['cat_id']));

--------------                           -------------- -----
------    // 把商品信息写入历史记录(session)   ----------------
    $this->history($g);
-------------                               ------------------
    $this->display();

--------------------------------------------------------------------------------
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
————————————————————————————————————————————————————————————————————————————————


如果浏览信息 应该先判断

如果没有那么 造一个空的session history 字段
如果有  那么 向里边添加


public  function history($g){
  $history = session('?history');
  if(isset($history)){
        $history= array();
  }else{
    $row = array();
    $row['goods_name']= $g['goods_name'];
    $row['shop_price']= $g['shop_price'];
    $row['thumb_img']= $g['thumb_img'];

    $history[$g['goods_id']]=$row;
  }
}



if(count($history) > 6) {
    $key = key($history); // 第1个单元的键
    unset($history[$key]);
}

session('history' , $history);
}









public function history($g) {
    $history = session('history');

    if(empty($history)) {
        $history = array();
    }

    if(isset($history[$g['goods_id']])) {
        unset($history[$g['goods_id']]);
    }

    $row = array();

    //$g 来自于 浏览商品时候的img
    $row['thumb_img'] = $g['thumb_img'];
    $row['goods_name'] = $g['goods_name'];
    $row['shop_price'] = $g['shop_price'];

    $history[$g['goods_id']] = $row;

















--
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

银色种子

打赏 >100 请留言,并私信

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值