2012-01-06 74 views
11

cómo limitar este bucle .just ti loops..thanks para ayudarcómo limitar bucle foreach a tres bucles

<?php 
    foreach($section['Article'] as $article) : 
?> 
<tr> 
    <td> 
     <?php 
      if ($article['status'] == 1) { 
       echo $article['title']; 
      } 
     ?> 
    </td> 
    <td> 
     <?php 
      if($article['status']== 1) { 
       echo '&nbsp;'.$html->link('View', '/articles/view/'.$article['id']); 
      } 
     ?> 
    </td> 
</tr> 
<?php 
    endforeach; 
?> 
+0

donde hace $ sección [ 'artículo'] ¿viene de? –

+1

posible duplicado de [limitar el número de veces que se ejecuta un ciclo en php] (http://stackoverflow.com/questions/1998204/limiting-number-of-times-a-loop-runs-in-php) –

Respuesta

23

primero, preparar los datos

$i = 1; 
$data = array(); 
foreach($section['Article'] as $article) { 
    if($article['status']== 1) { 
    $article['link'] = $html->link('View', '/articles/view/'.$article['id']); 
    $data[] = $article; 
    if ($i++ == 3) break; 
    } 
} 
$section['Article'] = $data; 

a continuación, mostrar que

<?php foreach($section['Article'] as $article): ?> 
<tr> 
    <td><?php echo $article['title'] ?></td> 
    <td>&nbsp;<?php echo $article['link']?></td> 
</tr> 
<?php endforeach ?> 
-3

un bucle foreach no sería el mejor si es necesario limitarlo. Intenta usar un bucle for.

<?php 

for(i=1; i<=3; i++) 
{ 
    $article = $section['Article']; 


        ?> 
        <tr> 
         <td><?php if($article['status']== 1){echo $article['title'];} ?></td> 
         <td><?php if($article['status']== 1){echo '&nbsp;'.$html->link('View', '/articles/view/'.$article['id']);}?></td> 
        </tr> 
        <?php } ?> 

Este código hará que el texto se repita 3 veces.

+1

qué idioma el código es? –

8

Esto ayudará si la matriz está indexado numéricamente

foreach($section['Article'] as $i => $article): 

    if ($i > 3) break; 

De lo contrario - incrementar manualmente el contador:

$i = 0; 
foreach($section['Article'] as $article): 

    if ($i++ > 3) break; 
6

Sería más fácil de usar un bucle for() para hacer esto, pero para responder a la pregunta:

<? 
$i = 0; 
foreach ($section['Article'] AS $article): 
    if ($i == 3) { break; } 
?> 
... 
<? 
$i++; 
endforeach 
?> 
66

Corta la matriz.

foreach(array_slice($section['Article'], 0, 3) as $article): 
+9

+1 Esta debería ser la respuesta correcta. No usa declaraciones if, logra su objetivo en una línea y obviamente es la forma en que foreach está configurado para manejar este problema. Bueno, incluso si no va a obtener la marca correcta, es la que voy a utilizar. No quiero 10 líneas de código para abordar un problema simple. – gcoulby

-1

una impresionante debe probar este

<?php $count = 0; $pages = get_pages('child_of=1119&sort_column=post_date&sort_order=desc'); foreach($pages as $page) { 
$count++; 
if ($count < 50) { // only process 10 ?> 
<div class="main_post_listing"> <a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a><br /></div> 
<?php 
} } ?>