商品的浏览记录

 


原理:把点击过的商品的goods_id放入 cook==1,2,4,3,5 ;

   从数据库查询商品id在1,2,4,3,5中的商品分配到模版页
如果cook没有设置cook
如果有追加,去重,如果大于5,pop   

1
session_start(); 3 //商品的浏览记录 4 $goods_id=mt_rand(1,10); 5 if(empty($_COOKIE['user']['history'])){ 6 setcookie('user[history]',$goods_id,time()+3600*24*30); 7 }else{ 8 $hisOld=$_COOKIE['user']['history']; 9 $hisArray=explode(',',$hisOld); 10 array_unshift($hisArray,$goods_id); 11 $hisArray=array_unique($hisArray); 12 if(count($hisArray)>5){ 13 array_pop($hisArray); 14 } 15 $goods_id=implode(',',$hisArray); 16 setcookie('user[history]',$goods_id,time()+3600*24*30); 17 p($_COOKIE['user']['history']); 18 } 19 //传浏览记录 20 $history= $_COOKIE['user']['history'];// 1,7,2,9,3 21 $history = $db->getAll("select * from goods where goods_id in ({$history})"); 22 $tpl->assign('history',$history); 23 24 //过程 25 //goods_id=1 26 1 //如果没有,设置cook=1(goods_id) 27 [0=>1] //如果有把cook里的值分割成数组 //1,2,3,4,5 28 [1=>2,0=>1]//在数组的前边插入新浏览的记录,形成新的数组 29 //去重,判断count(cook)>5则pop 30 2,1 //在把新形成的数组变为字符串放入cook//2,1,3,4,5,

 

转载于:https://www.cnblogs.com/finddata/p/11059685.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现商品浏览记录功能,可以使用JSP中的session对象来存储浏览过的商品信息。 首先,在商品详情页中,可以通过JSP代码获取到当前浏览商品信息,并将其存储到session中。示例代码如下: ``` <% String productId = request.getParameter("productId"); // 获取当前浏览商品ID String productName = "商品名称"; // 根据商品ID查询商品名称等信息 String productPrice = "商品价格"; String productImage = "商品图片路径"; // 将商品信息存储到session中 HttpSession session = request.getSession(); List<String[]> historyList = (List<String[]>) session.getAttribute("historyList"); if (historyList == null) { historyList = new ArrayList<String[]>(); } String[] productInfo = {productId, productName, productPrice, productImage}; historyList.add(0, productInfo); if (historyList.size() > 5) { historyList.remove(5); } session.setAttribute("historyList", historyList); %> ``` 上述代码中,首先获取到当前浏览商品ID,然后查询该商品的名称、价格、图片路径等信息。接着,从session中获取商品浏览记录列表,如果列表不存在则创建一个新的列表。将当前浏览商品信息添加到列表的最前面,如果列表超过了5个则删除最后一个。最后,将更新后的列表重新存储到session中。 在商品浏览记录页面中,可以通过JSP代码获取到session中存储的商品浏览记录列表,并将其展示出来。示例代码如下: ``` <% HttpSession session = request.getSession(); List<String[]> historyList = (List<String[]>) session.getAttribute("historyList"); %> <!DOCTYPE html> <html> <head> <title>商品浏览记录</title> </head> <body> <h1>商品浏览记录</h1> <ul> <% for (String[] productInfo : historyList) { %> <li> <a href="productDetail.jsp?productId=<%=productInfo[0]%>"> <img src="<%=productInfo[3]%>" alt="<%=productInfo[1]%>"> <span><%=productInfo[1]%></span> </a> </li> <% } %> </ul> </body> </html> ``` 上述代码中,首先获取到session中存储的商品浏览记录列表。然后,使用JSP循环语句遍历列表中的每个商品信息,并将其展示出来。 以上就是实现商品浏览记录功能的JSP代码示例。需要注意的是,为了保证用户隐私安全,可能需要对商品浏览记录进行加密处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值