2012-02-01 123 views
-6

如何從循環如何限制for循環的項目?

<?php    
    for ($i=0; $i< count($contentdinamit["chart"]["songs"]["song"]); $i++) { 
     echo'<li class="up"><a href="' 
      .$contentdinamit["chart"]["songs"]["song"]["$i"]["artist_name"].'"><strong>' 
      .$contentdinamit["chart"]["songs"]["song"]["$i"]["song_name"].'</strong></a><br /><a href="' 
      .$contentdinamit["chart"]["songs"]["song"]["$i"]["artist_name"].'">' 
      .$contentdinamit["chart"]["songs"]["song"]["$i"]["artist_name"].'</a></li>'; 
    } 
?> 
+4

解釋請。什麼你想做的事從MySQL得到條目的數量? ? – joshua 2012-02-01 10:33:36

+0

//將循環限制設置爲10 for($ i = 0; $ i <= 10; How It Works? – 2012-02-01 10:35:54

+2

您不應該在for聲明中count()。使用它像$我<$ count – djot 2012-02-01 10:39:17

回答

1

不知道你在說什麼。但這裏是我從你的問題

<?php  

    for ($i=0; $i< count($contentdinamit["chart"]["songs"]["song"]); $i++) { 

    if(($i+1)<=10){//limit your item by 10 
     echo'<li class="up"><a href="' 
      .$contentdinamit["chart"]["songs"]["song"]["$i"]["artist_name"].'"><strong>' 
      .$contentdinamit["chart"]["songs"]["song"]["$i"]["song_name"].'</strong></a><br /><a href="' 
      .$contentdinamit["chart"]["songs"]["song"]["$i"]["artist_name"].'">' 
      .$contentdinamit["chart"]["songs"]["song"]["$i"]["artist_name"].'</a></li>'; 
    } 
} 
?> 
+1

你爲什麼要循環一切?爲什麼要第二個櫃檯? – PiTheNumber 2012-02-01 10:41:54

+0

對不起,我改變了它。我的錯誤:P – 2012-02-01 10:46:12

+0

如果計數是100萬,這將需要永遠。如果你插入一個像Lucifer Orichalcum這樣的休息,那就是不好的風格。 – PiTheNumber 2012-02-01 11:00:17

-1

限制的物品將其限制在20個項目。嘗試

$limit = min(20, count($contentdinamit["chart"]["songs"]["song"])); 
for ($i=0; $i < $limit; $i++) 
+0

爲什麼投下了票? – PiTheNumber 2012-02-01 10:39:36

+0

不明白爲什麼這是被投票。我認爲這是一個很好的答案。但我更喜歡使用'if($ i + 1 <= 10)'。那麼我認爲它取決於程序員的風格。 – 2012-02-01 10:50:44

+0

T h a n k s! ! ! – 2012-02-01 11:28:29

0

你的意思是像限制輸出?只需調整你的for循環,只回放你想要的條目,而不是循環直到達到你的數組的數量。假設你只想要5個項目,那麼就這樣做:

$maximum = 5; // Set whatever number you want here as a maximum, and then... 
for($i = 1;$i <= $maximum;$i++) { 
    // Echo goes here 
} 
+0

如果count()是1,這將不起作用 – PiTheNumber 2012-02-01 10:40:41

+0

@PiTheNumber:良好的調用,更新瞭解決該問題的答案。 – Oldskool 2012-02-01 10:41:40

+0

不能解決它。您必須使用min(),請參閱http://stackoverflow.com/a/9094385/956397 – PiTheNumber 2012-02-01 10:50:24

0

如果你覺得你的代碼產生太大的輸出,做一些在你的循環,如:

if($i > 10)// 10 is your limit. 
    break; 
+1

這在foreach循環中很有用,但在for循環中,您可以將停止條件設置爲第二個參數。 – Oldskool 2012-02-01 10:40:31

+0

@Oldskool你錯了:http://php.net/manual/en/control-structures.break.php – PiTheNumber 2012-02-01 10:42:53

+0

@PiTheNumber:我沒有說這個答案是錯誤的,如果你可以設置它並不是很有效在for循環參數中也是一樣的。這只是不必要的額外的代碼行。 – Oldskool 2012-02-01 10:44:04

0

設置循環限制到10對於($ I = 0; $ I < = 10;如何進行

設置循環限制你必須指定在loop continuation condition循環的數量;

for ($i=0; $i <= 10; $i++) { 

} 
+0

T h a n k s! ! ! – 2012-02-01 11:28:46

1

明白,如果你只想要條目數n,然後使用限制

select * from table_name limit $max; 
+0

不錯的主意。如果OP使用mysql,這將是一條路。 – PiTheNumber 2012-02-01 11:06:21

+0

什麼是OP? ... – 2012-02-01 13:12:34

+0

OP表示原始海報。 http://www.acronymfinder.com/;) – PiTheNumber 2012-02-01 13:24:23