0

I am trying to do so whenever my while loop reach each 4 result (4,8,12,16 etc) it will create a new table row.

It should be like this:

[table row]
[data] [data] [data] [data]
[/table row]

[table row]
[data] [data] [data] [data]
[/table row]

and so on...

Currently I have this:

  <?php 
$cat=mysql_query("SELECT * FROM kb_categories"); 
        $i = 0;
        while($catData=mysql_fetch_assoc($cat)):
        $i ++; 
        //Select the numbers of articles inside each category.
        $number=mysql_num_rows(mysql_query("SELECT * FROM kb_articles WHERE cat_id='".$catData['id']."'"));

        ?>

    <td><img src="/themes/dream/images/icons/folder.gif"><span><?php echo $catData['name']; ?></span> (<?php echo $number; ?>)</td>
    <?php endwhile; ?> 

Right now, it just generates this:

[table row]
[data] [data] [data] [data] [data] [data] etc.
[/table row]

I am just not sure how to use the $i in this example. Can someone help me?

1

2 Answers 2

0

Two solutions I can think of. Either check when i is equal to for and then insert the row and reset i back to 0. Or, see if it is exactly divisible by 4 and then insert the row. (Which is probably better because then i will equal the amount of data.)

<?php 
$cat=mysql_query("SELECT * FROM kb_categories"); 
$i = 0;
while($catData=mysql_fetch_assoc($cat)) :
    $i ++; 
    //Select the numbers of articles inside each category.
    $number=mysql_num_rows(mysql_query("SELECT * FROM kb_articles WHERE cat_id='".$catData['id']."'"));

    if($i==3) {
        // Insert row here, probably something like this:
        echo "</tr><tr>";
        $i = 0;
    }

?>

<td><img src="/themes/dream/images/icons/folder.gif"><span><?php echo $catData['name']; ?></span> (<?php echo $number; ?>)</td>
<?php endwhile; ?> 

Or the second method:

<?php 
$cat=mysql_query("SELECT * FROM kb_categories"); 
$i = 0;
while($catData=mysql_fetch_assoc($cat)) :
    $i ++; 
    //Select the numbers of articles inside each category.
    $number=mysql_num_rows(mysql_query("SELECT * FROM kb_articles WHERE cat_id='".$catData['id']."'"));

    if( (($i+1) % 4) == 0) {
        // Insert row here, probably something like this:
        echo "</tr><tr>";
    }

?>
<td><img src="/themes/dream/images/icons/folder.gif"><span><?php echo $catData['name']; ?></span> (<?php echo $number; ?>)</td>
<?php endwhile; ?> 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! I used the first method, and I just changed if($i==3) to if($i==5) (since I need 4 records per table row. Thanks!
0
  • First, get your data into array

  • Next, use array_chunk() to split it into several nested arrays

  • Finally, in the template use 2 nested foreach loops to output

  • And, for God's sake, learn to indent your code

  • Also, you should NOT count rows by using mysql_num_rows().
    Use SELECT count(*) instead.

  • And there shouldn't be nested queries at all - you should make it with one query using JOIN. If you have no clue what is it - ask another question here.

8 Comments

why should you not count rows by using mysql_num_rows() is there something inherently bad about it, or is that just an optimisation?
Very nice answer, but I think user169691 doesn't exactly have the skills to achieve that, without some more in depth examples, otherwise he wouldn't be asking this kind of question here. You've got to answer people's questions on their own level and let them learn from experience, you can't just chuck them straight in the deep end just because it's not semantically the best way of doing it.
@user, so, you're always doing stupid thing. If you need only number - ask your database for the number, not for all the rows.
@Col. Shrapnel I tend to disagree with you, There are plenty of quality questions and answers on stackoverflow. Stackoverflow is meant for amatuers and experts alike. If there is someone who doesn't know too much, just guide him gently, or move on - no one will shout at you if you don't answer a question.
So @Col. Shrapnel is saving the world, one PHP question at a time. Find a different website if you don't like this one.
|

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.