商品的浏览记录


原理:把点击过的商品的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