排序 取6个而不是 count($ar)-1

解决方案 »

  1.   

    没看明白,如果你的 $_COOKIE["last_product_view"] 是一个数组,那么
    $i = 0;
    foreach($_COOKIE["last_product_view"] as $k=>$v) {
      echo $v;
      $i ++;
      if ($i == 6) break;
    }
    if($i == 0) { echo "Nothing";}
    这样看起来更简单一些吧?
      

  2.   


    代码是这个,3楼的貌似是前6个,不知道是否是我理解错误:
    $temp=$_COOKIE["last_product_view"];
    if(count($temp)>5)
    {
    $array_pop($temp);
    }
    if($action==1){
    if(is_array($temp)){
    $ar=array_keys($temp);
    for($i=count($ar)-1,$tmp=0;$i>=0 and $tmp<6;$i--,$tmp++){//改的地方
    echo " <li>".count($temp)." <a href=\"/website/html/product/show_pro_".$temp["$ar[$i]"].".html\" target=\"_blank\">".str_replace("__"," ",$ar[$i])." </a> </li>\n";
    }
    }else{
    echo "Nothing";
    }
      

  3.   

    不好意思想取5个,取完5个以后,新取得的替换最后一个
    也就是只显示最后点击的5个如果有重复,则把里面重复的这条记录提前到最前面用cookie
    也就是最近浏览的5条记录
      

  4.   

    完整代码如下
    $action=$_GET["action"];
    $temp=$_COOKIE["last_product_view"];
    if($action==1){
    if(is_array($temp)){
    $ar=array_keys($temp);
    for($i=count($ar)-1;$i>=0;$i--){
    echo "<li><a href=\"/website/html/product/show_pro_".$temp["$ar[$i]"].".html\" target=\"_blank\">".str_replace("__"," ",$ar[$i])."</a></li>\n";
    }
    }else{
    echo "Nothing";
    }
    }elseif($action==0){
    if($_GET["name"]!=""&&$_GET["link"]!=""){
    if(is_array($temp)){
    $ar=array_keys($temp);if(count($temp)==5)setcookie("last_product_view[".$ar[0]."]","",time()-3600);
    }
    $cookie_names=str_replace(" ","__",js_unescape($_GET["name"]));
    setcookie("last_product_view[".$cookie_names."]",$_GET["link"],time()+3600,"/");
    }
    }
      

  5.   


    改成五久可以了:
    $temp=$_COOKIE["last_product_view"];
    if(count($temp)>5)
    {
    $array_pop($temp);
    }
    if($action==1){
    if(is_array($temp)){
    $ar=array_keys($temp);
    for($i=count($ar)-1,$tmp=0;$i>=0 and $tmp<5;$i--,$tmp++){//改的地方
    echo " <li>".count($temp)." <a href=\"/website/html/product/show_pro_".$temp["$ar[$i]"].".html\" target=\"_blank\">".str_replace("__"," ",$ar[$i])." </a> </li>\n";
    }
    }else{
    echo "Nothing";
    }
    }elseif($action==0){
    if($_GET["name"]!=""&&$_GET["link"]!=""){
    if(is_array($temp)){
    $ar=array_keys($temp);if(count($temp)==5)setcookie("last_product_view[".$ar[0]."]","",time()-3600);
    }
    $cookie_names=str_replace(" ","__",js_unescape($_GET["name"]));
    setcookie("last_product_view[".$cookie_names."]",$_GET["link"],time()+3600,"/");
    }