문제

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?

도움이 되었습니까?

해결책

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;

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top