2013-12-08 47 views
-1

腳本的第一部分獲取數組中的前4個值並將結果輸出爲列表。腳本的第二部分應該得到其餘的值,如5,6,10等,如果有的話,並將它們全部放在一個div中,但目前我的腳本的第二部分仍然抓住了所有的值該數組從第一個開始。如何忽略第一個4,然後使用foreach輸出其餘值PHP

這裏是我的腳本

$counter = 0; 
    foreach ($value as $category_topic) 
    if(++$counter <= 4){ 
    $template_image = '<img src="'.$image_path.$category_topic['ImagePath'].DS.$category_topic['templateImage'].'" width="'.$category_topic['templimgwidth'].'" height="'.$category_topic['templimgheight'].'" alt="'.$category_topic['templateTitle'].'" title="'.$category_topic['templateTitle'].'">'; 
    $template_link ='<a href="'.DST.$category_topic['ImagePath'].DS.$category_topic['referring_url'].'">'.$category_topic['templateTitle'].'</a>'; 
    print<<<END 

     <li> 
     <ul> 
     <li>{$template_image}</li> 
     <li class="bot_link">{$template_link}</li> 
     </ul> 
     </li> 
    END; 
    } 
    print <<<END 

    </ul> 
    <div> 

    END; 
    foreach ($value as $category_topic) 
    if(++$counter > 4){ 
    $template_link[++$counter] ='<a href="'.DST.$category_topic['ImagePath'].DS.$category_topic['referring_url'].'">'.$category_topic['templateTitle'].'</a>'; 
    print<<<END 

    {$template_link} 
    END; 
    } 
    print <<<END 

    </div> 
    </div> 

    END; 

回答

1

我可能會使用for的建議,但這裏是一個備用:

foreach(array_slice($value, 0, 4) as $category_topic) { 
    //...this is the first 4 
} 

//... 

foreach(array_slice($value, 4) as $category_topic) { 
    //...this is 5 to the end 
} 
+0

甜美,像魔術一樣工作,感謝您的回答 – AlexB

2

製作使用for代替foreach

for($i = 4; $i < count($value); $i++) 
{ 
    // your code...... 
} 

編輯:

對於第1部分。

for($i=0;$i<4;$i++) 
{ 
//your code... 
} 

第二部分。

for($i=4;$i<count($value);$i++) 
{ 
//your code... 
} 
+0

謝謝,但在劇本,頂部或底部的哪一部分? – AlexB

+0

@AlexB爲什麼不能同時使用? (或者讓我改述一下:你想從指定索引開始的每個部分) – kero

+0

對於兩者都不起作用,我已經嘗試過了 – AlexB

相關問題