Alternating Row Color

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alix
Forum Commoner
Posts: 42
Joined: Thu Nov 18, 2004 8:41 am

Alternating Row Color

Post by alix »

I searched the forum and wasnt able to find anything to really help me, however, im sure this question has been asked many times.

Im trying to just pull data from a db and put it into a table, with each row having alternating colors.. its so much easier on the eyes isnt it? 8). I made up some sample code that might help. It pulls all the users from a db, pulls info from there.. then goes to the animals table and selects every animal in there with that username set as its owner.. then counts them... Its a very usless script but i just want to know how it was done so i created something to work with.

Here is my code (sorry for the sloppyness):

Code: Select all

<?
include("db/db.php");
$HTTP_POST_VARS;
echo "<table width=\"700\" height=\"85\"  cellpadding=\"0\" border=\"1\" cellspacing=\"0\">";
echo "<tr><td>Name(username)</td><td>fish</td><td>Dogs</td><td>Total animals</td></tr>";
$get_users= mysql_query("SELECT * FROM my_users ORDER BY name ASC");
while($rows = mysql_fetch_row($get_users))
{
	
	while($users = mysql_fetch_assoc($get_users))
	{
		
		//now print the results:
		$loginname= $users['loginname'];
		
		$result = mysql_query("SELECT * FROM my_users WHERE loginname = \"$loginname\" ");
		while($myrow = mysql_fetch_assoc($result))
		{
			
			//now print the results:
			$id= $myrow['id'];
			$loginname= $myrow['loginname'];
			$name = $myrow['name'];
		}
		
		//get hits and view
		$result = mysql_query("SELECT * FROM my_animals WHERE owner = '$loginname' AND animal = 'fish'");
		$number_of_fish = mysql_num_rows($result);
		$get_sub = mysql_query("SELECT * FROM my_animals WHERE owner = '$loginname' AND animal = 'dog' ");
		$number_of_dogs = mysql_num_rows($get_sub);
		//print results.
		$total_animals = $number_of_fish+$number_of_dogs;
		echo "<tr align=center ><td>". $name . "(". $loginname .") </td><td>  " .$number_of_fish. "</td><td>". $number_of_dogs . " </td><td>". $total_animals ."</td></tr>";
		
	}
};
echo "</table>";

 ?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

alix
Forum Commoner
Posts: 42
Joined: Thu Nov 18, 2004 8:41 am

Post by alix »

Thanks, it worked.

This is what worked for me the easiest..
Swede78 wrote:Here's another simple way of doing it, no math involved.

Code: Select all

<?php $RowBG = 'DDDDDD';  // this goes before the loop and is the 2nd color in the rotation ?>
<?php do {  // use whatever type of of loop you want ?>
<?php if( $RowBG == 'FFFFFF' ) { $RowBG = 'DDDDDD'; } else { $RowBG = 'FFFFFF'; }  // this goes inside the loop ?>
<tr> 
<td bgcolor="#<?php echo $RowBG; ?>">Bla bla bla</td>
</tr>
<?php } while ($row_Results = mysql_fetch_assoc($Results)); ?>
:D
Post Reply