Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 &raquo;</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 &raquo;</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!.

share|improve this question
 
you don't need nested loops, just an if to check if i%3 == 0 then close the row and start a new one –  koala_dev Jul 31 at 6:25
 
check you server error log and make sure the display_errors configuration directive is set to On... You are probably getting an error when you call next($headings) on the last row. –  Orangepill Jul 31 at 6:27
 
@koala_dev, i dont know if it's just the common cold i have right now messing with my brain or i just can't see having an if $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
 
@Orangepill, i checked my error_log file, there seems to be no abnormalies there, such as fatal error, but i do find this in my php_error_log: PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 133169152 bytes) in /opt/lampp_1.8.2/htdocs/my-codeigniter-starter-kit/application/views/welcome/wel‌​come_message.php on line 18 does this mean i'm having an infinite loop here, or it's just taking long? thanks. –  cj cabero Jul 31 at 7:12
 
@Mischa, I've already done research on that, it and seems its ok to leave the iteration on the for loop blank since i'm doing the iteration of the variable $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
show 2 more comments

1 Answer

up vote 1 down vote accepted

Here's how you can do it with just a check of the 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 &raquo;</a></p>  
        <?php next($headings); ?>
        <?php $i++; ?>
    </div> <!-- end of div.span4 -->
<?php endfor; ?>
</div><!-- end of div.row -->

EDIT I just realized you have string indexes, so I updated to use a for loop instead of a foreach

EDIT 2

The reason your first code is not returning anything is because you fall in an infinite loop when you break from the while the first time, that is when $i == 3, see in your next for iteration the while condition is not met and since your $i++ is inside the while the value of $i is now forever stuck at 3

share|improve this answer
 
hahaha! i see my fault now, your entirely correct, i do run into an infinite loop, when i included an iteration on my for loop for($i=1; $i < count($headings); $i++) it got out of the infinite loop, but it only shows in two's and not in three's. well i'll be erasing that anyway and use your code since its much much better. Thanks!. –  cj cabero Jul 31 at 8:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.