Solved

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

Posted on 2012-04-06
30
427 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
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)

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now