1

I have my markup structure as below:

<div>
    <div>value1</div>
    <div>value2</div>
    <div>value3</div>
    <div>value4</div>

    <div class="clear"></div>
</div>
<div>
    <div>value5</div>
    <div>value6</div>
    <div>value7</div>
    <div>value8</div>

    <div class="clear"></div>
</div>

I have my data in a PHP result set, let's say I have 9 records so the structure should be as below:

<div>
    <div>value1</div>
    <div>value2</div>
    <div>value3</div>
    <div>value4</div>

    <div class="clear"></div>
</div>
<div>
    <div>value5</div>
    <div>value6</div>
    <div>value7</div>
    <div>value8</div>

    <div class="clear"></div>
</div>
<div>
    <div>value9</div>

    <div class="clear"></div>
</div>

So, the while loop should run in a way so that it will print the parent div after 4 records printed successfully. But in above I have 9 records so it should close the dive if its the last record.

Please help, Thanks!

8
  • That doesn't matter, whatever data is there. I just want to run the loop in a way so that it will print the markup in required way. Commented Dec 9, 2010 at 11:40
  • Understood, but it's unclear what exactly you are trying to do, at least for me. Commented Dec 9, 2010 at 11:43
  • have you been a member for like 10 years? Commented Dec 9, 2010 at 11:43
  • Updated question, hope it will help to understand the question, please check! Commented Dec 9, 2010 at 11:48
  • 1
    I see, the logic you need can be found at stackoverflow.com/questions/4263983/…, just tweak it a bit. Please let us know if you get stuck. Commented Dec 9, 2010 at 11:53

5 Answers 5

5

The preconfig...

<?php
    $num_of_results = sizeof($your_array);  
    $loops = ceil($num_of_results/4);
    $k = 0;
?>

In your web

<?php for($p = 0; $p < $loops; $p++) { ?>
    <div>
        <div>
        <?php for($i = 0; $i < 4 && $k < $num_of_results; $i++) { ?>
            <div><?php echo $your_array[$k]; $k++;?></div>
        <?php } ?>
        <div class="clear"></div>
        </div>
    </div>
    <?php } ?>

That's your problem isnt it?

Sign up to request clarification or add additional context in comments.

2 Comments

Don't want to run 2 loops... :(
You have a syntax error on your "$k ++;". And why do you use 3 indexes when you can use only two. Just calculate $k as ($p * 4 + $i).
1

By getting some idea from logic given here I tried following and it works.

<div> <!-- started main div -->

<?php 
    $icount = 1;
    $itotal = mysql_num_rows($result_rs);
    while ($rs = mysql_fetch_array($result_rs)) {
        echo '<div>'.$rs['value'].'</div>'; 

        if ($icount % 4 == 0 && $icount != $itotal){
            echo '<div class="clear"></div>';
            echo '</div>'; //closed main div

            echo '<div>'; //started new main div
        }

        $icount++;
    }
?>

</div> <!-- closed main div -->

That, solved my problem.

Edited: added itotal condition, so when you will have only 4 records per page then also this will work properly.

Comments

0

Right, now I know what you're after. I've done this before when showing items in a grid and you need to break each row because of that browser.

Anyway, it's ugly but I don't think it gets any easier than this

<?php for ($i = 0, $total = count($resultSet); $i < $total; $i += 4) : ?>
<div>
    <?php
    for ($j = $i; $j < ($i + 4); $j++) :
    if (!isset($resultSet[$j])) :
    ?>
    <div class="clear"></div>
    </div>
    <?php break 2; endif ?>
    <div><?php echo htmlspecialchars($resultSet[$j]) ?></div>
    <?php endfor ?>
    <div class="clear"></div>
</div>
<?php endfor ?>

1 Comment

@Prashant Updated my answer, should work for you as I've had to do the same before
0
<div>
    <?php for ($i = 1; $i <= 9; $i++): ?>
    <?php if ($i%4 == 1 && $i != 1): ?>
    <div class="clear"></div>
</div>
<div>
    <?php endif; //$i%4 == 1 && $i != 1 ?>
    <div>Value <?php echo $i ?></div>
    <?php endfor; //$i = 1; $i <= 9; $i++ ?>
    <div class="clear"></div>
</div>

or with an array:

<div>
    <?php foreach ($arr as $k=>$v): ?>
    <?php if (($k+1)%4 == 1 && $k != 0): ?>
    <div class="clear"></div>
</div>
<div>
    <?php endif; //($k+1)%4 == 1 && $k != 0 ?>
    <div><?php echo $v ?></div>
    <?php endforeach; //$arr as $k=>$v ?>
    <div class="clear"></div>
</div>

or with a mysqli resultset:

<div>
    <?php $count = 1 ?>
    <?php while ($row = mysqli_fetch_array($result)): ?>
    <?php if ($count%4 == 1 && $count != 1): ?>
    <div class="clear"></div>
</div>
<div>
    <?php endif; //$count%4 == 1 && $count != 1 ?>
    <div><?php echo $row['value'] ?></div>
    <?php $count++ ?>
    <?php endwhile; ?>
    <div class="clear"></div>
</div>

Comments

0

Why not use modulo to "close" a div?

<div>
<?php foreach($data as $key => $value) : ?>
    <div><?php echo $value ?></div>
<?php if($key % 4 == 0 && $key != 0) : // add a clearing div, close the first group and open another one ?>
    <div class="clear"></div>
</div>
<div>
<? endforeach ?>
<?php if($key % 4 != 0) : // div has not been closed as the number of records % 4 was not equal 0 ?>
    <div class="clear"></div>
 </div>
<? endif ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.