Insert tr after every third loop












7















I'm making a forum in PHP. I have to display all forum categories in a table, and to do so, I have used a while loop. However, I want to have only 3 td's in every table row. To loop through the categories, I'm using a while loop with the query, so I don't think I can use modulus here.










share|improve this question

























  • show us your code

    – Dor Shemer
    Jan 25 '12 at 18:58











  • You could use the modulus operator % by simply creating an increment variable $i=0 prior to the loop and incrementing it ++$i in each iteration of the while loop.

    – rdlowrey
    Jan 25 '12 at 19:01


















7















I'm making a forum in PHP. I have to display all forum categories in a table, and to do so, I have used a while loop. However, I want to have only 3 td's in every table row. To loop through the categories, I'm using a while loop with the query, so I don't think I can use modulus here.










share|improve this question

























  • show us your code

    – Dor Shemer
    Jan 25 '12 at 18:58











  • You could use the modulus operator % by simply creating an increment variable $i=0 prior to the loop and incrementing it ++$i in each iteration of the while loop.

    – rdlowrey
    Jan 25 '12 at 19:01
















7












7








7


1






I'm making a forum in PHP. I have to display all forum categories in a table, and to do so, I have used a while loop. However, I want to have only 3 td's in every table row. To loop through the categories, I'm using a while loop with the query, so I don't think I can use modulus here.










share|improve this question
















I'm making a forum in PHP. I have to display all forum categories in a table, and to do so, I have used a while loop. However, I want to have only 3 td's in every table row. To loop through the categories, I'm using a while loop with the query, so I don't think I can use modulus here.







php html-table modulus






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 20 '17 at 11:12









Brian Tompsett - 汤莱恩

4,2031337101




4,2031337101










asked Jan 25 '12 at 18:56









user1169875user1169875

71129




71129













  • show us your code

    – Dor Shemer
    Jan 25 '12 at 18:58











  • You could use the modulus operator % by simply creating an increment variable $i=0 prior to the loop and incrementing it ++$i in each iteration of the while loop.

    – rdlowrey
    Jan 25 '12 at 19:01





















  • show us your code

    – Dor Shemer
    Jan 25 '12 at 18:58











  • You could use the modulus operator % by simply creating an increment variable $i=0 prior to the loop and incrementing it ++$i in each iteration of the while loop.

    – rdlowrey
    Jan 25 '12 at 19:01



















show us your code

– Dor Shemer
Jan 25 '12 at 18:58





show us your code

– Dor Shemer
Jan 25 '12 at 18:58













You could use the modulus operator % by simply creating an increment variable $i=0 prior to the loop and incrementing it ++$i in each iteration of the while loop.

– rdlowrey
Jan 25 '12 at 19:01







You could use the modulus operator % by simply creating an increment variable $i=0 prior to the loop and incrementing it ++$i in each iteration of the while loop.

– rdlowrey
Jan 25 '12 at 19:01














3 Answers
3






active

oldest

votes


















13














Why can't you use modulus? Just add a counter somewhere, and if it hits % 3 == 0 reset the counter and do your stuff.



You might need to do some extra if's for first and last and stuff like that, but there is no reason not to use a modulo with a while.



$i=0;
while(guard()){
if($i % 3 == 0){
//ploing
}
$i++
}





share|improve this answer
























  • Ah, brilliant! I have to get the hang of this modulus thing.

    – user1169875
    Jan 25 '12 at 19:08






  • 7





    +1 for excellent sound effects...

    – squarecandy
    Oct 31 '13 at 3:59



















13














This code will close any extra rows:



<table>
<?php
$columns = 3;
$i = 0;
while($row = mysql_fetch_array($result)){
$i++;
//if this is first value in row, create new row
if ($i % $columns == 1) {
echo "<tr>";
}
echo "<td>".$row[0]."</td>";
//if this is last value in row, end row
if ($i % $columns == 0) {
echo "</tr>";
}
}
//if the counter is not divisible by the number of columns, we have an open row
$spacercells = $columns - ($i % $columns);
if ($spacercells < $columns) {
for ($j=1; $j<=$spacercells; $j++) {
echo "<td></td>";
}
echo "</tr>";
}
?>
</table>





share|improve this answer





















  • 1





    Thank you man! This community is absolutely the most helpful i've come across!

    – user1169875
    Jan 25 '12 at 19:40






  • 1





    Thanks man.. This is really helpful.

    – Abhishek Kumbhani
    Nov 11 '18 at 20:48



















2














I haven't tested the code, but the logic should work:



<Table>
<?php
$i = 0;
while($row = mysql_fetch_array($result)){
if($i == 0){
echo"<TR>";
}
echo"<td>".$row[0]."<TD>";
$i++;
if($i == 3)
{
$i = 0;
echo"</tr>"
}
}
if($i ==1){
echo "<td></td><td></td></tr>";
}
if($i ==2)
{
echo "<td></td></tr>";
}
?>
<table>





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9008522%2finsert-tr-after-every-third-loop%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    13














    Why can't you use modulus? Just add a counter somewhere, and if it hits % 3 == 0 reset the counter and do your stuff.



    You might need to do some extra if's for first and last and stuff like that, but there is no reason not to use a modulo with a while.



    $i=0;
    while(guard()){
    if($i % 3 == 0){
    //ploing
    }
    $i++
    }





    share|improve this answer
























    • Ah, brilliant! I have to get the hang of this modulus thing.

      – user1169875
      Jan 25 '12 at 19:08






    • 7





      +1 for excellent sound effects...

      – squarecandy
      Oct 31 '13 at 3:59
















    13














    Why can't you use modulus? Just add a counter somewhere, and if it hits % 3 == 0 reset the counter and do your stuff.



    You might need to do some extra if's for first and last and stuff like that, but there is no reason not to use a modulo with a while.



    $i=0;
    while(guard()){
    if($i % 3 == 0){
    //ploing
    }
    $i++
    }





    share|improve this answer
























    • Ah, brilliant! I have to get the hang of this modulus thing.

      – user1169875
      Jan 25 '12 at 19:08






    • 7





      +1 for excellent sound effects...

      – squarecandy
      Oct 31 '13 at 3:59














    13












    13








    13







    Why can't you use modulus? Just add a counter somewhere, and if it hits % 3 == 0 reset the counter and do your stuff.



    You might need to do some extra if's for first and last and stuff like that, but there is no reason not to use a modulo with a while.



    $i=0;
    while(guard()){
    if($i % 3 == 0){
    //ploing
    }
    $i++
    }





    share|improve this answer













    Why can't you use modulus? Just add a counter somewhere, and if it hits % 3 == 0 reset the counter and do your stuff.



    You might need to do some extra if's for first and last and stuff like that, but there is no reason not to use a modulo with a while.



    $i=0;
    while(guard()){
    if($i % 3 == 0){
    //ploing
    }
    $i++
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 25 '12 at 19:00









    NanneNanne

    55.7k1497142




    55.7k1497142













    • Ah, brilliant! I have to get the hang of this modulus thing.

      – user1169875
      Jan 25 '12 at 19:08






    • 7





      +1 for excellent sound effects...

      – squarecandy
      Oct 31 '13 at 3:59



















    • Ah, brilliant! I have to get the hang of this modulus thing.

      – user1169875
      Jan 25 '12 at 19:08






    • 7





      +1 for excellent sound effects...

      – squarecandy
      Oct 31 '13 at 3:59

















    Ah, brilliant! I have to get the hang of this modulus thing.

    – user1169875
    Jan 25 '12 at 19:08





    Ah, brilliant! I have to get the hang of this modulus thing.

    – user1169875
    Jan 25 '12 at 19:08




    7




    7





    +1 for excellent sound effects...

    – squarecandy
    Oct 31 '13 at 3:59





    +1 for excellent sound effects...

    – squarecandy
    Oct 31 '13 at 3:59













    13














    This code will close any extra rows:



    <table>
    <?php
    $columns = 3;
    $i = 0;
    while($row = mysql_fetch_array($result)){
    $i++;
    //if this is first value in row, create new row
    if ($i % $columns == 1) {
    echo "<tr>";
    }
    echo "<td>".$row[0]."</td>";
    //if this is last value in row, end row
    if ($i % $columns == 0) {
    echo "</tr>";
    }
    }
    //if the counter is not divisible by the number of columns, we have an open row
    $spacercells = $columns - ($i % $columns);
    if ($spacercells < $columns) {
    for ($j=1; $j<=$spacercells; $j++) {
    echo "<td></td>";
    }
    echo "</tr>";
    }
    ?>
    </table>





    share|improve this answer





















    • 1





      Thank you man! This community is absolutely the most helpful i've come across!

      – user1169875
      Jan 25 '12 at 19:40






    • 1





      Thanks man.. This is really helpful.

      – Abhishek Kumbhani
      Nov 11 '18 at 20:48
















    13














    This code will close any extra rows:



    <table>
    <?php
    $columns = 3;
    $i = 0;
    while($row = mysql_fetch_array($result)){
    $i++;
    //if this is first value in row, create new row
    if ($i % $columns == 1) {
    echo "<tr>";
    }
    echo "<td>".$row[0]."</td>";
    //if this is last value in row, end row
    if ($i % $columns == 0) {
    echo "</tr>";
    }
    }
    //if the counter is not divisible by the number of columns, we have an open row
    $spacercells = $columns - ($i % $columns);
    if ($spacercells < $columns) {
    for ($j=1; $j<=$spacercells; $j++) {
    echo "<td></td>";
    }
    echo "</tr>";
    }
    ?>
    </table>





    share|improve this answer





















    • 1





      Thank you man! This community is absolutely the most helpful i've come across!

      – user1169875
      Jan 25 '12 at 19:40






    • 1





      Thanks man.. This is really helpful.

      – Abhishek Kumbhani
      Nov 11 '18 at 20:48














    13












    13








    13







    This code will close any extra rows:



    <table>
    <?php
    $columns = 3;
    $i = 0;
    while($row = mysql_fetch_array($result)){
    $i++;
    //if this is first value in row, create new row
    if ($i % $columns == 1) {
    echo "<tr>";
    }
    echo "<td>".$row[0]."</td>";
    //if this is last value in row, end row
    if ($i % $columns == 0) {
    echo "</tr>";
    }
    }
    //if the counter is not divisible by the number of columns, we have an open row
    $spacercells = $columns - ($i % $columns);
    if ($spacercells < $columns) {
    for ($j=1; $j<=$spacercells; $j++) {
    echo "<td></td>";
    }
    echo "</tr>";
    }
    ?>
    </table>





    share|improve this answer















    This code will close any extra rows:



    <table>
    <?php
    $columns = 3;
    $i = 0;
    while($row = mysql_fetch_array($result)){
    $i++;
    //if this is first value in row, create new row
    if ($i % $columns == 1) {
    echo "<tr>";
    }
    echo "<td>".$row[0]."</td>";
    //if this is last value in row, end row
    if ($i % $columns == 0) {
    echo "</tr>";
    }
    }
    //if the counter is not divisible by the number of columns, we have an open row
    $spacercells = $columns - ($i % $columns);
    if ($spacercells < $columns) {
    for ($j=1; $j<=$spacercells; $j++) {
    echo "<td></td>";
    }
    echo "</tr>";
    }
    ?>
    </table>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 23:53

























    answered Jan 25 '12 at 19:18









    Nicholas KouvatsosNicholas Kouvatsos

    573412




    573412








    • 1





      Thank you man! This community is absolutely the most helpful i've come across!

      – user1169875
      Jan 25 '12 at 19:40






    • 1





      Thanks man.. This is really helpful.

      – Abhishek Kumbhani
      Nov 11 '18 at 20:48














    • 1





      Thank you man! This community is absolutely the most helpful i've come across!

      – user1169875
      Jan 25 '12 at 19:40






    • 1





      Thanks man.. This is really helpful.

      – Abhishek Kumbhani
      Nov 11 '18 at 20:48








    1




    1





    Thank you man! This community is absolutely the most helpful i've come across!

    – user1169875
    Jan 25 '12 at 19:40





    Thank you man! This community is absolutely the most helpful i've come across!

    – user1169875
    Jan 25 '12 at 19:40




    1




    1





    Thanks man.. This is really helpful.

    – Abhishek Kumbhani
    Nov 11 '18 at 20:48





    Thanks man.. This is really helpful.

    – Abhishek Kumbhani
    Nov 11 '18 at 20:48











    2














    I haven't tested the code, but the logic should work:



    <Table>
    <?php
    $i = 0;
    while($row = mysql_fetch_array($result)){
    if($i == 0){
    echo"<TR>";
    }
    echo"<td>".$row[0]."<TD>";
    $i++;
    if($i == 3)
    {
    $i = 0;
    echo"</tr>"
    }
    }
    if($i ==1){
    echo "<td></td><td></td></tr>";
    }
    if($i ==2)
    {
    echo "<td></td></tr>";
    }
    ?>
    <table>





    share|improve this answer




























      2














      I haven't tested the code, but the logic should work:



      <Table>
      <?php
      $i = 0;
      while($row = mysql_fetch_array($result)){
      if($i == 0){
      echo"<TR>";
      }
      echo"<td>".$row[0]."<TD>";
      $i++;
      if($i == 3)
      {
      $i = 0;
      echo"</tr>"
      }
      }
      if($i ==1){
      echo "<td></td><td></td></tr>";
      }
      if($i ==2)
      {
      echo "<td></td></tr>";
      }
      ?>
      <table>





      share|improve this answer


























        2












        2








        2







        I haven't tested the code, but the logic should work:



        <Table>
        <?php
        $i = 0;
        while($row = mysql_fetch_array($result)){
        if($i == 0){
        echo"<TR>";
        }
        echo"<td>".$row[0]."<TD>";
        $i++;
        if($i == 3)
        {
        $i = 0;
        echo"</tr>"
        }
        }
        if($i ==1){
        echo "<td></td><td></td></tr>";
        }
        if($i ==2)
        {
        echo "<td></td></tr>";
        }
        ?>
        <table>





        share|improve this answer













        I haven't tested the code, but the logic should work:



        <Table>
        <?php
        $i = 0;
        while($row = mysql_fetch_array($result)){
        if($i == 0){
        echo"<TR>";
        }
        echo"<td>".$row[0]."<TD>";
        $i++;
        if($i == 3)
        {
        $i = 0;
        echo"</tr>"
        }
        }
        if($i ==1){
        echo "<td></td><td></td></tr>";
        }
        if($i ==2)
        {
        echo "<td></td></tr>";
        }
        ?>
        <table>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 25 '12 at 19:07









        Brian GarsonBrian Garson

        1,075610




        1,075610






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9008522%2finsert-tr-after-every-third-loop%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud