Connecting Tech Pros Worldwide Help | Site Map

How do you have different Tables in a while loop?

Newbie
 
Join Date: Aug 2009
Posts: 6
#1: Aug 10 '09
Sup guys.
Okay, straight to the point: I'm trying to perform a while loop with some data from my database and it's working fine appart from the way the data is displayed. I need the data to be displayed in a different "<td>" as in the ID would be different, so that the structure of the table would look like this:
______________
| TD 1 - Data 1 |
| TD 2 - Data 2 |
| TD 1 - Data 3 |
| TD 2 - Data 4 |
| TD 1 - Data 5 |
¬¬¬¬¬¬¬¬¬¬¬

If that makes any sense to anyone lol. I'm quite new to PHP and Mysql, but am okay with HTML and CSS, so if my PHP terms are wrong forgive me. My code for the table so far is as follows:


Quote:
<table id="navigation" title="Navigation" border="0" width="190" align="right" cellspacing="0">
<td rowspan="16" id="general_Sidebar" align="right">Hi</td>


<td id="general_Grey_Header" class="general_Grey_Header">Newest Users<br></td>
</tr>
<?

$dbres = mysql_query("SELECT `signup`,`login` FROM `users` WHERE `level`!='-1' AND `level`!='0' AND `game_id`='1' ORDER BY `signup` DESC LIMIT 10");

while($user = mysql_fetch_assoc($dbres)){

?>
<tr><td id="general_Light_Grey" class="general_Light_Grey"><b><?php echo($user[login]); ?></b></td>
</tr>

<tr><td id="general_Grey" class="general_Grey"><b><?php echo($user[login]); ?></b></td>
</tr>
<?
}
?>
</table>

This is for a Newest User feature that displays the last ten usernames that signed up. So far I can either make it display each username twice but have the right layout, or disply each name once and have to wrong layout. I need it to display the right layout and display each username only once. If that also makes any sense lol.

If more info is required just ask, thanks for your time and help :D
MrMancunian's Avatar
Expert
 
Join Date: Jul 2008
Location: Utrecht, The Netherlands
Posts: 274
#2: Aug 10 '09

re: How do you have different Tables in a while loop?


Hi, this can be done in a simple way. First, you count the number of rows using
Expand|Select|Wrap|Line Numbers
  1. $numberofrows = mysql_num_rows($dbres);
Next, you create a for...next loop. This is what you use instead of the while-loop you created.

Expand|Select|Wrap|Line Numbers
  1. for($i = 0; $i < $numberofrows; $i++) 
  2. {
  3.   $user = mysql_fetch_array($dbres);
  4. }
Now, you check if $i has a remainder when you divide it by 2. This is done with %. If there is a remainder you use one color. If there isn't a remainder, you use the other. So like this:
Expand|Select|Wrap|Line Numbers
  1. if($i % 2)
  2. {
  3.   echo "<tr><td id="general_Light_Grey" class="general_Light_Grey"><b><?php echo($user[login]); ?></b></td></tr>";
  4. else
  5. {
  6. echo "<tr><td id="general_Grey" class="general_Grey"><b><?php echo($user[login]); ?></b></td></tr>";
  7. }
Hope this makes sense to you :-)

Steven
Newbie
 
Join Date: Aug 2009
Posts: 6
#3: Aug 10 '09

re: How do you have different Tables in a while loop?


I hate to be a pain in the ass, but I put the code in, and now on the list there is only one name. The code now reads:

Quote:
<?

$dbres = mysql_query("SELECT `signup`,`login` FROM `gebruiker` WHERE `level`!='-1' AND `level`!='0' AND `game_id`='1' ORDER BY `signup` ASC LIMIT 10");
$numberofrows = mysql_num_rows($dbres);

for($i = 0; $i < $numberofrows; $i++)
{
$user = mysql_fetch_array($dbres);
}



if($i % 2) { ?>
<tr><td id="general_Light_Grey" class="general_Light_Grey"><b><?php echo($user[login]); ?></b></td></tr>

<? }else{ ?>

<tr><td id="general_Grey" class="general_Grey"><b><?php echo($user[login]); ?></b></td></tr>
<?
}
?>
I've probably put things in the wrong place but what am I doing wrong?
Cheers Mancun for helping :D
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#4: Aug 10 '09

re: How do you have different Tables in a while loop?


you could look for a Javascript that makes alternating table row colours (one of the frameworks should have it). or you could alter the class names by position number.
Newbie
 
Join Date: Aug 2009
Posts: 6
#5: Aug 10 '09

re: How do you have different Tables in a while loop?


Thanks for all for your help, but I edited Mancun's little script and got it working, thanks loads guys :D
MrMancunian's Avatar
Expert
 
Join Date: Jul 2008
Location: Utrecht, The Netherlands
Posts: 274
#6: Aug 10 '09

re: How do you have different Tables in a while loop?


You do need to put the code inside the for-loop. Like this:

Expand|Select|Wrap|Line Numbers
  1. <?
  2. $dbres = mysql_query("SELECT `signup`,`login` FROM `gebruiker` WHERE `level`!='-1' AND `level`!='0' AND `game_id`='1' ORDER BY `signup` ASC LIMIT 10");
  3. $numberofrows = mysql_num_rows($dbres); 
  4.  
  5. for($i = 0; $i < $numberofrows; $i++) 
  6. {
  7. $user = mysql_fetch_array($dbres); 
  8.  
  9. if($i % 2) { ?>
  10. <tr><td id="general_Light_Grey" class="general_Light_Grey"><b><?php echo($user[login]); ?></b></td></tr>
  11.  
  12. <? }else{ ?>
  13.  
  14. <tr><td id="general_Grey" class="general_Grey"><b><?php echo($user[login]); ?></b></td></tr>
  15. <?
  16. ?> 
  17. }
Steven
Newbie
 
Join Date: Aug 2009
Posts: 6
#7: Aug 10 '09

re: How do you have different Tables in a while loop?


Thanks loads Mancun :D
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#8: Aug 10 '09

re: How do you have different Tables in a while loop?


Quote:

Originally Posted by MrMancunian View Post

Expand|Select|Wrap|Line Numbers
  1. for($i = 0; $i < $numberofrows; $i++) 
  2. {
  3.   $user = mysql_fetch_array($dbres);
  4. }

I don't get that. You're using a loop here because?

Anyway, here's how I would do it:

Expand|Select|Wrap|Line Numbers
  1. // Your query
  2. $query = mysql_query(...);
  3.  
  4. // Keep looping until mysql_fetch_array() returns false
  5. for ($i = 0; mysql_fetch_array($query); ++$i) {
  6.     if ($i % 2 == 0) {
  7.         // Color it one way
  8.     } else {
  9.         // Color it another way
  10.     }
  11. }
  12.  
Also, HTML IDs are supposed to be used once and only once. Use classes instead.

Expand|Select|Wrap|Line Numbers
  1. <!-- incorrect -->
  2. <div id="some_id" /><div id="some_id" />
  3. <!-- correct -->
  4. <div class="some_class" id="some_id" /><div class="some_class" id="some_other_id" />
  5.  
Mark.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#9: Aug 10 '09

re: How do you have different Tables in a while loop?


side note: you must not use duplicate ID names (in HTML), they’re meant to be unique*. that may cause trouble when using Javascript with it.

* XHTML parsers will break on that (DTD violation)

****… markus got the better of me
MrMancunian's Avatar
Expert
 
Join Date: Jul 2008
Location: Utrecht, The Netherlands
Posts: 274
#10: Aug 10 '09

re: How do you have different Tables in a while loop?


Quote:

Originally Posted by Markus View Post

I don't get that. You're using a loop here because?

I forgot to remove the last bracket in that piece of code.

44penfold, use Markus' code, it's way more efficient :-)

Steven
Newbie
 
Join Date: Aug 2009
Posts: 6
#11: Aug 10 '09

re: How do you have different Tables in a while loop?


Righto, got it all done :D - Thanks guys, works like a charm :) In future I'll make sure I use classes instead of IDs :D
Reply

Tags
loop, mysql, php, tables