用php实现类似淘宝,拍拍,易趣的最近浏览商品的功能模型代码,该如何处理

WBOY
Release: 2016-06-13 13:45:06
Original
841 people have browsed it

用php实现类似淘宝,拍拍,易趣的最近浏览商品的功能模型代码
最近在做一个项目,要用到类似淘宝,拍拍,易趣的最近浏览商品的功能,在网络上遍寻不及,遂找出一ASP代码用PHP自行改写,得以下片段,先发上来和大家共享。此代码片段只包含cookie写入部分,读出处理的部分大家可以自行编写。最先跟帖提出建议的给以加分。

/*
RecentlyGoods File
Urchin Studio FoxCMS
Version: 1.1.6
Author: luckyfox (xhx@163.net)
Copyright: Urchin Studio (www.itocean.net)
Last Modified: 2008-9-3

*/

//TempNum 显示临时记录数

$TempNum=5; 

//setcookie("RecentlyGoods", "12,31,90,39");

//RecentlyGoods 最近商品RecentlyGoods临时变量

if (isset($_COOKIE['RecentlyGoods']))
{
$RecentlyGoods=$_COOKIE['RecentlyGoods'];
$RecentlyGoodsArray=explode(",", $RecentlyGoods);
$RecentlyGoodsNum=count($RecentlyGoodsArray); //RecentlyGoodsNum 当前存储的变量个数

}

if($_GET['Id']!="")
{

$Id=$_GET['Id']; //ID 为得到请求的字符


//如果存在了,则将之前的删除,用最新的在尾部追加

if (strstr($RecentlyGoods, $Id)) 
{
//echo "已经存在,则不写入COOKIES


";
}
else
{
if($RecentlyGoodsNum {
if($RecentlyGoods=="")
{
setcookie("RecentlyGoods",$Id,time()+3600);
}
else
{
$RecentlyGoodsNew=$RecentlyGoods.",".$Id;
setcookie("RecentlyGoods", $RecentlyGoodsNew,time()+3600);
}
}
else //如果大于了指定的大小后,将第一个给删去,在尾部再加入最新的记录。
{
$pos=strpos($RecentlyGoods,",")+1; //第一个参数的起始位置
$FirstString=substr($RecentlyGoods,0,$pos); //取出第一个参数
$RecentlyGoods=str_replace($FirstString,"",$RecentlyGoods); //将第一个参数删除
$RecentlyGoodsNew=$RecentlyGoods.",".$Id; //在尾部加入最新的记录
setcookie("RecentlyGoods", $RecentlyGoodsNew,time()+3600);
}

}


}
?>


------解决方案--------------------
收藏。谢谢
------解决方案--------------------
学习..收藏。
------解决方案--------------------
up
------解决方案--------------------

------解决方案--------------------
up
------解决方案--------------------
jf
------解决方案--------------------
看看
------解决方案--------------------
支持.
------解决方案--------------------
if (strstr($RecentlyGoods, $Id)) 

//echo "已经存在,则不写入COOKIES
"; 
}

如果我看了100号,100保存了,然后我再看10号,那就不能保存了
------解决方案--------------------
很好,收藏了。。
------解决方案--------------------
学习..收藏。

------解决方案--------------------
对id的处理用字符串查找来处理,这样的方式其实不太合理。
PHP code

define('MAX_ITEMS', 5);

$id = intval($_GET['id']);  // 确保id是整数
if (isset($_COOKIE['recentlyGoods'])) {
  // 已有历史数据,追加新数据
  $goods = explode(',', $_COOKIE['recentlyGoods']);  // 取得id数组
  $key = array_search($id, $goods); // 在历史数据中查找当前id
  if ($key !== false) {
    unset($goods[$key]);  // 找到则删除
  }
  $goods[] = $id; // 追加当前浏览的id
} else {
  // 尚未有历史数据,创建并将当前浏览id加入
  $goods = array($id);
}

// 如果数组中历史数据条目超出限制,则截取最后一段
if (count($goods) > MAX_ITEMS) {
  // 从倒数第MAX_ITEMS条开始提取MAX_ITEMS条记录
  $goods = array_slice($goods, 0 - MAX_ITEMS, MAX_ITEMS);
}

setcookie('recentlyGoods', join(',', $goods), time()+3600);
 <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!