so I have a codeigniter view file that has a code inside like 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 I want to achieve here is to loop through the array ($headings)
by three's. I want to produce something like 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 the above example the $heading
array contains 8 elements, so it produce 2 div
's with a class of row, inside is 3 div
's with a class of span4
. With the third div of class row only containing 2 div
's of class span4
.
Now, when I try to run this on the web server, it returns an empty page, no HTML tags whatsoever or PHP error. And i'm pretty sure that my php error_reporting
is set to E_ALL
. Tried to remove it and the page loads fine (of course without div's that the code in question should create).
i resolve this problem by using a 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 as to why the code in question doesn't work when logically it should be fine.
Am I missing something or does codeIgniter prevents nested loop of such way. Is it a bug?
Thanks in advance for the answers!.
i%3 == 0
then close the row and start a new one – koala_dev Jul 31 at 6:25$i%3 == 0
inside my for loop do the job. Can you please provide the bare code/pseudo code of what's in your mind? thanks alot!. – cj cabero Jul 31 at 7:11$i
inside my while loop. as proof, it works fine on my solution code (see the last code block on my post) Thanks. – cj cabero Jul 31 at 7:13