Solved

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

Posted on 2012-04-06
30
430 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 500 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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
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).

808 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