2012-02-01 117 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