Home >Backend Development >PHP Tutorial >谁帮忙看看 这个代码为什么不会循环

谁帮忙看看 这个代码为什么不会循环

WBOY
WBOYOriginal
2016-06-23 14:38:181083browse

<?phpfor($i=0;$i<3;$i++){?><div class="dptj1_coloum xxx<?php echo $i ?>"><?php$items = array();	$widths=273;	$heights=273;  $szzkj = explode(",",$_MODULE["szzkj"]);switch($_MODULE[huoqubaobei]){	   case "1":    $items = $itemManager->queryByKeyword($_MODULE[guanjianzi],$_MODULE[paixu],$_MODULE[geshu]);	break;    case "2":        $arr = explode(',',$_MODULE[item_1]);        foreach($arr as $value){            $item=$itemManager-> queryById ($value);            array_push($items, $item);        } break;    case "3":	$arr = explode(',',$_MODULE[cate_1]);        $items = $itemManager->queryByCategory($categoryId,$_MODULE["paixu"],$_MODULE["geshu"]);		break;	 case "4":	 $srtpdz = explode(",",$_MODULE["srtpdz"]);	  $srtpljdz = explode(",",$_MODULE["srtpljdz"]);	    $srbbbt = explode(",",$_MODULE["srbbbt"]);		    $srbbjg = explode(",",$_MODULE["srbbjg"]);			    $srxssl = explode(",",$_MODULE["srxssl"]);				break;}if($_MODULE[huoqubaobei]!="4"){	$i=-1;    foreach($items as $item){		$i++;        $itemUrl = $uriManager->detailURI($item);         $itemPicUrl = $item->getPicUrl(310);        $itemTitle = $item->title;        $itemPrice = $item->price;        $itemsoldCount = $item->soldCount; echo'<div class="dptj1_coloum1"><div class="dptj1_coloum1_dri"><div class="jiantou"><img  src="assets/images/youjiantou.png" / alt="谁帮忙看看 这个代码为什么不会循环" ></div><div class="content"><div class="wz"><a target="_blank" href="'.$itemUrl.'">'.$itemTitle.'</a></div><div class="price"><span class="price1">RMB:'.$itemPrice.'</span><span class="price2">¥ '.$szzkj[$i].'</span></div><div class="yishou">已售'.$itemsoldCount.'件</div></div></div><div class="dptj1_coloum1_pic"><a target="_blank" href="#"><img    style="max-width:90%" src="'.$itemPicUrl.'" / alt="谁帮忙看看 这个代码为什么不会循环" ></a></div></div>';	}}else if($_MODULE[huoqubaobei]=="4"){		$num = count($srtpdz);	    for($i=0;$i<$num;$i++)	    {			echo'<div class="dptj1_coloum1"><div class="dptj1_coloum1_dri"><div class="jiantou"><img  src="assets/images/youjiantou.png" / alt="谁帮忙看看 这个代码为什么不会循环" ></div><div class="content"><div class="wz"><a target="_blank" href="'.$srtpljdz[$i].'">'.$srbbbt[$i].'</a></div><div class="price"><span class="price1">RMB:'.$srbbjg[$i].'</span><span class="price2">¥ '.$szzkj[$i].'</span></div><div class="yishou">已售'.$srxssl[$i].'件</div></div></div><div class="dptj1_coloum1_pic"><a target="_blank" href="'.$srtpljdz[$i].'"><img    style="max-width:90%".$_MODULE["tpgd"].'px;" src="'.$srtpdz[$i].'" / alt="谁帮忙看看 这个代码为什么不会循环" ></a></div></div>';		}}?></div><?php}?>


为什么div不会循环 我手动复制可以哦 用for就不行了


回复讨论(解决方案)

for($i=0;$i
    for($i=0;$i 两个$i了

for($i=0;$i
    for($i=0;$i 两个$i了 刚改了 发现不一样啊  复制了好多好多

循环时把变量名注意一下,不要弄重了

if($_MODULE[huoqubaobei]!="4"){    $i=-1;    foreach($items as $item){        $i++;        $itemUrl = $uriManager->detailURI($item);         $itemPicUrl = $item->getPicUrl(310);        $itemTitle = $item->title;        $itemPrice = $item->price;        $itemsoldCount = $item->soldCount; 


foreach里的$i每循环一次+1,但你的外层for循环$i不能超过三,所以,你的foreach的$i影响了for的$i,也就循环一次了

Statement:
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