Pregunta

This is my foreach list:

    foreach ($items as $key => $item):
        if (++$i == 21) break;
        $output.='<div class="row-fluid"><div class="span12 block">
                                <div class="pull-left">
                        <a href="'.$item->link.'" target="_blank">'.$item->title.'</a>
                </div>
                <div class="pull-right">
                        <p class="muted">'.date("m/d/Y", $item->date).'</p>
                </div>
                <div class="clearfix"></div>
            </div></div>';
endforeach;

echo $output;

The result is a well orderd list of 21 items picked from an xml feed. What i'm trying to do is to add a custom row between (for example) line 10 and 11.

Can anyone suggest me a good way?

¿Fue útil?

Solución

You can do that by adding a simple if statement. If you want every 10th line to have that you can use mod operator %.

foreach ($items as $key => $item):
        if (++$i == 21) break;
            if ($i == 9) {
               $output .= '<div>NEW LINE </div>';
            }
        $output.='<div class="row-fluid"><div class="span12 block">
                                <div class="pull-left">
                        <a href="'.$item->link.'" target="_blank">'.$item->title.'</a>
                </div>
                <div class="pull-right">
                        <p class="muted">'.date("m/d/Y", $item->date).'</p>
                </div>
                <div class="clearfix"></div>
            </div></div>';
endforeach;

echo $output;

Otros consejos

You can use flag variables.

Before loop set initial value of a variable $rowcount=1

and between loop you can increment this variable from +1.

and check

 <code>
 if($rowcount=10) 
 {
 //do something
 }
 </code>

Use this solution

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top