php - a while loop inside a for loop not working in codeigniter view file -
so have codeigniter view file has code inside this:
<?php for($i=1; $i <= count($headings); ): ?> <div class="row"> <?php while(($i%3) != 0 ): ?> <div class="span4"> <?php $heading = current($headings); ?> <h2><?= key($headings); ?></h2> <p><?= $heading['description']; ?></p> <p><a class="btn" href="<?= $heading['link']; ?>">view details »</a></p> <?php next($headings); ?> <?php $i++; ?> </div> <!-- end of div.span4 --> <?php endwhile; ?> </div> <!-- end of div.row --> <?php endfor; ?>
what want achieve here loop through array ($headings)
three's. want produce this:
<div class="row"> <div class="span4"><!-- $heading element --></div> <div class="span4"><!-- $heading element --></div> <div class="span4"><!-- $heading element --></div> </div> <div class="row"> <div class="span4"><!-- $heading element --></div> <div class="span4"><!-- $heading element --></div> <div class="span4"><!-- $heading element --></div> </div> <div class="row"> <div class="span4"><!-- $heading element --></div> <div class="span4"><!-- $heading element --></div> </div>\
at above example $heading
array contains 8 elements, produce 2 div
's class of row, inside 3 div
's class of span4
. third div of class row containing 2 div
's of class span4
.
now, when try run on web server, returns empty page, no html tags whatsoever or php error. , i'm pretty sure php error_reporting
set e_all
. tried remove , page loads fine (of course without div's code in question should create).
i resolve problem using different logic. see below
<?php for($i=1; $i <= count($headings); ): ?> <div class="row"> <?php for($k=1; $k <= 3; $k++): ?> <div class="span4"> <?php $heading = current($headings); ?> <?php if ($heading !== false): ?> <h2><?= key($headings); ?></h2> <p><?= $heading['description']; ?></p> <p><a class="btn" href="<?= $heading['link']; ?>">view details »</a></p> <?php endif ?> <?php next($headings); ?> <?php $i++; ?> </div> <!-- end of div.span4 --> <?php endfor; ?> </div> <!-- end of div.row --> <?php endfor; ?>
but still i'm wondering why code in question doesn't work when logically should fine.
am missing or codeigniter prevents nested loop of such way. bug?
thanks in advance answers!.
here's how can check of element index
<div class="row"> <?php for($i=0; $i < count($headings); ): ?> <?if($i > 0 && $i%3 == 0):?> </div><!-- end of div.row --> <div class="row"> <?endif?> <div class="span4"> <?php $heading = current($headings); ?> <h2><?= key($headings); ?></h2> <p><?= $heading['description']; ?></p> <p><a class="btn" href="<?= $heading['link']; ?>">view details »</a></p> <?php next($headings); ?> <?php $i++; ?> </div> <!-- end of div.span4 --> <?php endfor; ?> </div><!-- end of div.row -->
edit realized have string indexes, updated use for
loop instead of foreach
edit 2
the reason first code not returning because fall in infinite loop when break while first time, when $i == 3, see in next for
iteration while condition not met , since $i++ inside while value of $i forever stuck @ 3
Comments
Post a Comment