您好,欢迎访问一九零五行业门户网

php商品对比功能代码分享

前段时间,朋友叫我帮他写个商品对比功能,当时正好在逛绿森数码商城,点开商品列表,看到有商品对比功能,然后直接下载了他的js滚动文件和抽出了对比框的样式,
下面是自己亲自动手编写的代码,和大家一起学习研究。
商品对比调用的js文件(包含了商品对比框浮动js):
/*浮动窗口*/ (function(){var n=10;var obj=document.getelementbyid(goods-compare);if(!obj){return false;}var x=0;window.onscroll=function(){obj.style.top=(document.body.scrolltop||document.documentelement.scrolltop)+n+'px';};window.onresize=function(){obj.style.top=(document.body.scrolltop||document.documentelement.scrolltop)+n+'px';};})();//添加显示对比框 function addcompare(chk){$('#goods-compare').fadein().show();var count=$(.compare-box li).length;if (count>2)//这里可以修改对比的数据哦{alert('产品比较最多选3种哦');return;}$.ajax({type: 'post',url: 'ajax.php',data: {'action':'1','gid':chk.gid,//商品id'gname':chk.gname,//商品名称'gtype':chk.gtype//商品类别,类别不同时不能比较},cache: false,async: false,success: function(result) {if(result!=''){alert(result);}else{var url='http://www.lusen.com/product-'+chk.gid+'.html';//设置商品的链接地址$(.compare-box).append(
'+chk.gname+'
删除)$(#comids).val($(.compare-box li).map(function(){//将对比的所有商品id,赋值给#comidsreturn $(this).attr('id');}).get().join(,));}}}); }//删除对比产品 function removecompare(id) {$.ajax({type: 'post',url: 'ajax.php',data: {'action':'2','gid':id},cache: false,success: function(result) {$(#+id).remove();$(#comids).val($(.compare-box li).map(function(){return $(this).attr('id');}).get().join(,));}});} //清空对比产品 function clearcompare() {$.ajax({type: 'post',url: 'ajax.php',data: {'action':'3'},cache: false,success: function(result) {$(.compare-box).html('');$(#comids).val('');}}); } //显示对比框 function showcompare() {$.ajax({type: 'post',url: 'ajax.php',data: {'action':'4'},success: function(result) {if(result){$(.compare-box).append(result);$(#comids).val($(.compare-box li).map(function(){return $(this).attr('id');}).get().join(,));$('#goods-compare').fadein().show();}}});}//点击关闭对比框 $('.close-gc').click(function(){$('#goods-compare').fadeout().hide(); }); 商品对比调用ajax文件
2) {//商品比较数量echo 商品比较最多选3种;return;}foreach($arr as $val) {if($val[0]==$_post['gid']) {echo 该商品已经加入对比框;return;}}$info=array($_post['gid'],$_post['gname'],$_post['gtype']);$arr[]=$info;$arr_str=serialize($arr);setcookie('gid',$arr_str);}else {$info=array($_post['gid'],$_post['gname'],$_post['gtype']);$arr[]=$info;$arr_str=serialize($arr);setcookie('gid',$arr_str);} }else if($_post['action']=='2') {//delone$id=$_post['gid'];$arr_str = $_cookie['gid'];$arr=mb_unserialize($arr_str);foreach($arr as $key=>$val) {if($val[0]==$id) {unset ($arr[$key]);}}$arr_str=serialize($arr);setcookie('gid',$arr_str); }else if($_post['action']=='3') {//delallsetcookie('gid','');setcookie('gtype',''); }else if($_post['action']=='4') {//showlistif(isset($_cookie['gid'])) {$data='';$arr_str = $_cookie['gid'];$arr=mb_unserialize($arr_str);foreach ($arr as $val){$url=http://www.lusen.com/product-.$val[0]..html;$data.=
{$val[1]}
删除;}echo $data;}} ?> 以上就是商品对比功能实现代码,希望大家可以仔细研究,有好的想法大家一起探讨。

其它类似信息

推荐信息