?
Solved

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

Posted on 2012-04-06
30
Medium Priority
?
443 Views
Last Modified: 2012-06-22
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

0
Comment
Question by:marcoullis
  • 15
  • 15
30 Comments
 

Author Comment

by:marcoullis
ID: 37816630
anyone?
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 37816688
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
 

Author Comment

by:marcoullis
ID: 37816694
Could you give your guidance using the code i provided please?

-hellenica
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
LVL 17

Expert Comment

by:nanharbison
ID: 37816700
$num_rows = mysql_num_rows($result);
for ($i=1; $i <= $num_rows; $i++)
{

}
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 37816703
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
 

Author Comment

by:marcoullis
ID: 37816705
where is the division you mentioned?
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 37816727
getting there.....
0
 

Author Comment

by:marcoullis
ID: 37816730
$num_rows = mysql_num_rows($result);
for ($i=1; $i <= $num_rows; $i++)
{

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

}
0
 

Author Comment

by:marcoullis
ID: 37816732
:-)
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 37816780
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
 

Author Comment

by:marcoullis
ID: 37816786
testing now...

:-)
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 37816788
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
 

Author Comment

by:marcoullis
ID: 37816836
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
 

Author Comment

by:marcoullis
ID: 37816853
but it still doesn't work...

:-(

any ideas?
0
 

Author Comment

by:marcoullis
ID: 37816857
with the new code i provided it adds a new row after each record.
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 37816858
what exactly didn't work? Also, don't get the number of rows inside the do while loop.
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 37816863
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
 

Author Comment

by:marcoullis
ID: 37816865
its showing one row for each record.

i want one row for each 4 rows.
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 37816867
So how close is this to working?
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 37816871
Can you give me a screen shot of it?
0
 

Author Comment

by:marcoullis
ID: 37816883
0
 

Author Comment

by:marcoullis
ID: 37816888
its adding a row every record, and i can't figure out why.
0
 

Author Comment

by:marcoullis
ID: 37816919
any ideas, i think we are close... this is so unnerving.
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 37816927
hmmmm, there is nothing in the first <tr></tr> and then it's going in every row.
why are you dividing by three?
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 37816930
can you echo $i and see what is happening with it?
0
 

Author Comment

by:marcoullis
ID: 37816950
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
 
LVL 17

Accepted Solution

by:
nanharbison earned 2000 total points
ID: 37816961
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
 
LVL 17

Expert Comment

by:nanharbison
ID: 37816969
You can't have the for loop - it is running through the whole loop with each row of the database.
0
 

Author Closing Comment

by:marcoullis
ID: 37816994
you are the boss!!!

thanks for sticking with it.

:-)

-hellenica
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 37817051
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

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we’ll look at how to deploy ProxySQL.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses
Course of the Month15 days, 20 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question