PHP how to format <td> with DO / WHILE for 4 cells per row

I am displaying a large amount of data using a DO / WHILE loop.

I want to display the data 4 cells to a row.

attached is the current format, please advise how to modify to allow for only four cells per row... thanks.

-hellenica

                                  <table>
                                  <tr>
                                  <?php do { ?>
                                  
                                  <td>
                                  
                                  <div align="center"> 

                                    <a href="launch.congress.php?rank=<?php echo $row_rs_congress['PARENT_ID']; ?>" 
                                    class="button button-white help" 
                                    rel="#overlay "> <img src="http://news.pseka.net/uploads/img/<?php echo $row_rs_congress['PICTURE']; ?>" width="150"></a><br>
                                    <?php echo $row_rs_congress['NAME']; ?> (<?php echo $row_rs_congress['PARTY']; ?>)<br>
                                    State: <?php echo $row_rs_congress['STATE']; ?> Distict:<?php echo $row_rs_congress['DISTRICT']; ?>
                                    
                                  </div>
                                  
                                  </td>
                                  
                                  <?php } while ($row_rs_congress = mysql_fetch_assoc($rs_congress)); ?>
                                  </tr>
                                  </table>

Open in new window

marcoullisAsked:
Who is Participating?
 
nanharbisonConnect With a Mentor Commented:
How about this,
<table>
                                 
                                  <tr>
                                 
                                                  <?php
                                 
                                                  $num_rows = mysql_num_rows($rs_congress);
                                                  $i = 0;
                                                  $n = 3;
                                                 
                                                  do { ?>
                                 
                                    <?php
      
                                                                  if($i % $n == 0 && $i != 0)
                                                                        
                                                                        { ?>
                                                                              </tr><tr>
                                                                        <?PHP }
                                                                        
                                                                  $i++;
                                                      
                                                      
                                   
                                                      
                                                  ?>
                                 
                                  <td>
                                 
                                  <div align="center">

                                    <a href="launch.congress.php?rank=<?php echo $row_rs_congress['PARENT_ID']; ?>"
                                    class="button button-white help"
                                    rel="#overlay "> <img src="http://news.pseka.net/uploads/img/<?php echo $row_rs_congress['PICTURE']; ?>" width="150"></a><br>
                                    <?php echo $row_rs_congress['NAME']; ?> (<?php echo $row_rs_congress['PARTY']; ?>)<br>
                                    State: <?php echo $row_rs_congress['STATE']; ?> Distict:<?php echo $row_rs_congress['DISTRICT']; ?>
                                   
                                  </div>
                                 
                                  </td>
                                 

                                 
                                  <?php } while ($row_rs_congress = mysql_fetch_assoc($rs_congress)); ?>
                                 
                                  </tr>
                                      </table>
0
 
marcoullisAuthor Commented:
anyone?
0
 
nanharbisonCommented:
You can add a variable as a counter before the loop, like $i=1 and increment with every iteration and when it is divisible by 4, then put a new </tr><tr>
in front of the 4 rows.

As I am looking at this, a for loop seems easier to me. Get the number of rows returned first.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
marcoullisAuthor Commented:
Could you give your guidance using the code i provided please?

-hellenica
0
 
nanharbisonCommented:
$num_rows = mysql_num_rows($result);
for ($i=1; $i <= $num_rows; $i++)
{

}
0
 
nanharbisonCommented:
OOPS, my browser was not refreshing, and I thought I replied and then didn't see it. How did that happen? Hold on a minute
0
 
marcoullisAuthor Commented:
where is the division you mentioned?
0
 
nanharbisonCommented:
getting there.....
0
 
marcoullisAuthor Commented:
$num_rows = mysql_num_rows($result);
for ($i=1; $i <= $num_rows; $i++)
{

[ shouldn't there be an "if" test here? ]

}
0
 
marcoullisAuthor Commented:
:-)
0
 
nanharbisonCommented:
Okay, so forget the for loop, it was easier to set up a while loop, not a do while:
<table>
<tr>

<?PHP
$row_rs_congress = mysql_fetch_assoc($rs_congress));
$num_rows = mysql_num_rows($rs_congress);
$i=0;
while ($row_rs_congress = mysql_fetch_assoc($rs_congress))

{
      if ($i/4 == 0){
                  echo '</tr><tr>';
            }
?>
      <td>
                  <div align="center">
                <a href="launch.congress.php?rank=<?php echo $row_rs_congress['PARENT_ID']; ?>"
                 class="button button-white help" rel="#overlay "> <img src="http://news.pseka.net/uploads/img/<?php echo $row_rs_congress['PICTURE']; ?>" width="150"></a><br>
                 <?php echo $row_rs_congress['NAME']; ?> (<?php echo $row_rs_congress['PARTY']; ?>)<br>
                  State: <?php echo $row_rs_congress['STATE']; ?> Distict:<?php echo $row_rs_congress['DISTRICT']; ?>
            </div>
   
   </td>
                         
<?PHP }
       ?>  
      </tr>
</table>

I can't test this so there may be typos or other errors, but it should be close.
0
 
marcoullisAuthor Commented:
testing now...

:-)
0
 
nanharbisonCommented:
the first two rows are wrong - drop the first line and then

$num_rows = mysql_num_rows($rs_congress);

can be after the query statement, which you didn't provide
0
 
marcoullisAuthor Commented:
it didn't work either way...

i have attached another perspective.

<table>

<tr>

<?php do { ?>

<?php 

$num_rows = mysql_num_rows($rs_congress);

$i = 0;
$n = 3;

for ($i=1; $i <= $num_rows; $i++)

{
if($i % $n == 0 && $i != 0)

{
echo '</tr><tr>';
}

$i++;
}



?>

<td>

<div align="center"> 

<a href="launch.congress.php?rank=<?php echo $row_rs_congress['PARENT_ID']; ?>" 
class="button button-white help" 
rel="#overlay "> <img src="http://news.pseka.net/uploads/img/<?php echo $row_rs_congress['PICTURE']; ?>" width="150"></a><br>
<?php echo $row_rs_congress['NAME']; ?> (<?php echo $row_rs_congress['PARTY']; ?>)<br>
State: <?php echo $row_rs_congress['STATE']; ?> Distict:<?php echo $row_rs_congress['DISTRICT']; ?>

</div>

</td>



<?php } while ($row_rs_congress = mysql_fetch_assoc($rs_congress)); ?>

</tr>


</table>

Open in new window

0
 
marcoullisAuthor Commented:
but it still doesn't work...

:-(

any ideas?
0
 
marcoullisAuthor Commented:
with the new code i provided it adds a new row after each record.
0
 
nanharbisonCommented:
what exactly didn't work? Also, don't get the number of rows inside the do while loop.
0
 
nanharbisonCommented:
And, how can you have this line be line 47, when you need the contents of the row above it?

<?php } while ($row_rs_congress = mysql_fetch_assoc($rs_congress)); ?>
0
 
marcoullisAuthor Commented:
its showing one row for each record.

i want one row for each 4 rows.
0
 
nanharbisonCommented:
So how close is this to working?
0
 
nanharbisonCommented:
Can you give me a screen shot of it?
0
 
marcoullisAuthor Commented:
0
 
marcoullisAuthor Commented:
its adding a row every record, and i can't figure out why.
0
 
marcoullisAuthor Commented:
any ideas, i think we are close... this is so unnerving.
0
 
nanharbisonCommented:
hmmmm, there is nothing in the first <tr></tr> and then it's going in every row.
why are you dividing by three?
0
 
nanharbisonCommented:
can you echo $i and see what is happening with it?
0
 
marcoullisAuthor Commented:
i have four records right now to display, so for now i need to divide by the next smallest, which is 3.

i will echo $i now.

-hellenica
0
 
nanharbisonCommented:
You can't have the for loop - it is running through the whole loop with each row of the database.
0
 
marcoullisAuthor Commented:
you are the boss!!!

thanks for sticking with it.

:-)

-hellenica
0
 
nanharbisonCommented:
You are welcome - this is how the life of a coder works - a lot of "head banging against wall", try something else, finally solve it.
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.