没想到啊

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

http://www.oschina.net/code/snippet_162279_6957

/**
* 商品历史浏览记录
* $data 商品记录信息
*/
private function _history($data)
{
if(!$data || !is_array($data))
{
return false;
}

//判断cookie类里面是否有浏览记录
if($this->_request->getCookie('history'))
{
$history = unserialize($this->_request->getCookie('history'));
array_unshift($history, $data); //在浏览记录顶部加入

/* 去除重复记录 */
$rows = array();
foreach ($history as $v)
{
if(in_array($v, $rows))
{
continue;
}
$rows[] = $v;
}

/* 如果记录数量多余5则去除 */
while (count($rows) > 5)
{
array_pop($rows); //弹出
}

setcookie('history',serialize($rows),time() + 3600 * 24 * 30,'/');
}
else
{
$history = serialize(array($data));

setcookie('history',$history,time() + 3600 * 24 * 30,'/');
}
}



/**
02  * 商品历史浏览记录
03  * $data 商品记录信息
04  */
05 private function _history($data)
06 {
07     if(!$data || !is_array($data))
08     {
09         return false;
10     }
11      
12     //判断cookie类里面是否有浏览记录
13     if($this->_request->getCookie('history'))
14     {
15         $history = unserialize($this->_request->getCookie('history'));
16         array_unshift($history, $data); //在浏览记录顶部加入
17  
18         /* 去除重复记录 */
19         $rows = array();
20         foreach ($history as $v)
21         {
22             if(in_array($v, $rows))
23             {
24                 continue;
25             }
26             $rows[] = $v;
27         }
28  
29         /* 如果记录数量多余5则去除 */
30         while (count($rows) > 5)
31         {
32             array_pop($rows); //弹出
33         }
34  
35         setcookie('history',serialize($rows),time() + 3600 * 24 * 30,'/');
36     }
37     else
38     {
39         $history = serialize(array($data));
40  
41         setcookie('history',$history,time() + 3600 * 24 * 30,'/');
42     }
43 }
posted on 2011-11-07 10:24  没想到啊  阅读(376)  评论(0)    收藏  举报