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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
marcoullisAuthor Commented:
Could you give your guidance using the code i provided please?

-hellenica
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

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:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.