如何取最后的6条cookie? 排序 取6个而不是 count($ar)-1 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 没看明白,如果你的 $_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";}这样看起来更简单一些吧? 代码是这个,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";} 不好意思想取5个,取完5个以后,新取得的替换最后一个也就是只显示最后点击的5个如果有重复,则把里面重复的这条记录提前到最前面用cookie也就是最近浏览的5条记录 完整代码如下$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,"/");}} 改成五久可以了:$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,"/");}} 关于smarty定义绝对路径在<script 中使用 mysql存储“——”破折号的问题 弱弱的问一个问题 字符串连接,为何说我错了? 对PHP的疑问? smarty技术请教!希忘各位兄弟姐妹多多帮忙 关于数据库操作的写法 怎样从标题栏得到汉字变量,奇怪,为什么php报错 php指定时间内显示内容 【花生壳映射问题】 正则中关于posix和perl的小问题 linux下mysql的卸载
$i = 0;
foreach($_COOKIE["last_product_view"] as $k=>$v) {
echo $v;
$i ++;
if ($i == 6) break;
}
if($i == 0) { echo "Nothing";}
这样看起来更简单一些吧?
代码是这个,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";
}
也就是只显示最后点击的5个如果有重复,则把里面重复的这条记录提前到最前面用cookie
也就是最近浏览的5条记录
$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,"/");
}
}
改成五久可以了:
$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,"/");
}
}