PHP product comparison feature code sharing _php tips

Source: Internet
Author: User
Tags setcookie

Here is your own hands-on code, and we learn to study together.

Product comparison of the call JS file (contains the merchandise contrast box floating js):

/* Floating window * * (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 ';   
}; 
 
})(); 
  Add Display contrast box function Addcompare (CHK) {$ (' #goods-compare '). FadeIn (). Show (); 
  var count=$ (". Compare-box li"). length; 
    if (COUNT>2)//Here you can modify the contrast of the data Oh {alert (' Product comparison of 3 kinds of OH '); 
  Return 
      } $.ajax ({type: ' post ', url: ' ajax.php ', data: {' action ': ' 1 ', ' gid ': chk.gid,//Product ID ' Gname ': chk.gname,//commodity name ' Gtype ': chk.gtype//commodity category, different categories can not be compared}, Cache:false, Async:false, S 
      Uccess:function (Result) {if (result!= ") {alert (result); }else{var url= ' http://www.lusen.com/product-' +chk.gid+ '. html '///Set the link address of the item $ (". Compare-box"). Append ("<li class= ' Division Clearfix ' id= '" +chk.gid+ ') ' ><div class = ' span-3 ' ><a href= ' "+url+" ' target= ' _blank ' title= ' "+chk.gname+" ' > ' "+chk.gname+" ' </a></div> <span onclick=\ "Removecompare (' +chk.gid+"); \ "> Delete </span></li>") $ ("#comids"). Val ($ (". compare- 
        Box Li "). Map (function () {//All merchandise IDs that will be compared, assigned to #comids return $ (this). attr (' id '); 
      }). Get (). Join (",")); 
} 
    } 
  }); //delete Contrast product function Removecompare (ID) {$.ajax ({type: ' post ', url: ' ajax.php ', data: {' act 
      Ion ': ' 2 ', ' GID ': id}, Cache:false, success:function (Result) {$ ("#" +id). Remove (); 
      $ ("#comids"). Val ($ (". Compare-box li"). Map (function () {return $ (this). attr (' id '); 
    }). Get (). Join (",")); 
  
} 
  }); //Empty Contrast product function Clearcompare () {$.ajax ({type: ' post ', url: ' ajax.php ', data: {' action ': ' 3 ' 
    }, Cache:false, success:function (Result) {$ (". Compare-box"). html ('); 
    $ ("#comids"). Val ('); 
} 
  }); //Show Contrast box 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 (); 
  
} 
    } 
  }); 
//Click Close Contrast box $ ('. CLOSE-GC '). Click (function () {$ (' #goods-compare '). fadeout (). Hide (); 
 });

Product comparison invoke Ajax file

<?php function mb_unserialize ($serial _str) {$serial _str =stripslashes ($serial _str); 
Return Unserialize ($serial _str); 
      } if ($_post[' action ']== ' 1 ') {//add if (isset ($_cookie[' Gtype '))) {if ($_cookie[' Gtype ']!=$_post[']) { Echo ' Sorry, you selected different categories of products can not be added to the comparison, please select the same product or empty the current contrast bar and then select. 
      '; 
    Return 
  }}else {Setcookie (' gtype ', $_post[' gtype ')]; 
    } if (Isset ($_cookie[' gid ')) {$arr _str = $_cookie[' gid ']; 
    $arr =mb_unserialize ($arr _str); 
      if (count ($arr) >2) {//number of commodity comparisons echo "more than 3 kinds of commodity comparison"; 
    Return 
        foreach ($arr as $val) {if ($val [0]==$_post[' gid ']) {echo "The product has been added to the contrast box"; 
      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 ') {//delall setcookie (' gid ', '); 
Setcookie (' Gtype ', '); 
    }else if ($_post[' action ']== ' 4 ') {//showlist if (isset ' 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. = "<li id= ' {$val [0]} ' class= ' Division Clearfix ' ><div class= ' span-3 ' ><a href= ' {$url} ' target= ' _ Blank ' title= ' {$val [1]} ' >{$val [1]}</a></div><span onclick=\] Removecompare (' {$val [0]} '); \ > 
    Delete </span></li> "; 
  Echo $data;  }}?>

Above is the product comparison function to achieve the code, I hope you can study carefully, there are good ideas to discuss together.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.