?
Solved

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

Posted on 2012-04-06
30
Medium Priority
?
436 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 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