Page 1 of 1

alternate row colors

Posted: Thu Jul 22, 2010 10:06 am
by norwichchris
hi,
I'm new to PHP and want to implement an alternating row colors for my table. I discovered that this is possible and attempted to use a script online but it does not work and it only showed the table but not the information. I have no idea why it would do that as all of my database connections are the same: see below

Code: Select all

  <?php
//Database connection, this must remain at the top, to allow functions below to run;
$hostname = "AAA"; //host on awardspace
$database = "BBB"; //database
$username = 'CCC'; //username note same as db, may want to change
$password = 'DDD'; //password for db
$tbl_name="tblevents"; // Table name 
//Actual database connection;
mysql_connect("$hostname", "$username", "$password")or die("cannot connect");
mysql_select_db("$database")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
	$color="1";
	
  	echo '<table width="800" border="2" align="center" cellpadding="2" cellspacing="1">';
  	while($row = mysql_fetch_array($result)){
	// If $color==1 table row color = #FFC600
		if($color==1){
		echo "<tr bgcolor='#FFC600'>
		<td>".$rows['tblEvent']."</td><td>".$rows['tblDate']."</td><td>".$rows['tblContact']."</td>
		</tr>";
		// Set $color==2, for switching to other color
		$color="2";
		}

		// When $color not equal 1, use this table row color
		else {
		echo "<tr bgcolor='#C6FF00'>
		<td>".$rows['tblEvent']."</td><td>".$rows['tblDate']."</td><td>".$rows['tblContact']."</td>
		</tr>";
// Set $color back to 1
$color="1";
}

}
    echo "</table>";
  	mysql_close($con);
?> &nbsp;
I know it is possble to do this but im not sure how here is th link to the site i found the information i wanted on:

http://www.phpeasystep.com/phptu/4.html

any help would be much appreciated

Re: alternate row colors

Posted: Thu Jul 22, 2010 11:16 am
by TipPro
First, do not post your database credentials on a public forum.

Second, if a table is showing up without any rows it is most likely because your database is not returning any rows.

Check for errors after you try to execute your query.

PS: Look into using the PHP MySQL Improved Extension (http://php.net/manual/en/book.mysqli.php)

Re: alternate row colors

Posted: Thu Jul 22, 2010 12:46 pm
by pickle
The simplest solution would be something like this:

Code: Select all

while(...)
{
  $bgcolor = ($bgcolor == 'FFC600') ? 'C6FF00' : 'FFC600';
  echo "<tr bgcolor='#$bgcolor'>
                <td>".$rows['tblEvent']."</td><td>".$rows['tblDate']."</td><td>".$rows['tblContact']."</td>
                </tr>";

Re: alternate row colors

Posted: Thu Jul 22, 2010 1:37 pm
by Benjamin
I like stripping but I don't like the extra code. Here's a one-liner I recently used:

Code: Select all

<tr class="<?php echo $zebra = (isset($zebra) && $zebra == 'even') ? 'odd' : 'even'; ?>">

Re: alternate row colors

Posted: Fri Jul 23, 2010 3:32 am
by Shasha Munzara
I usually use this method:

Code: Select all

<?
 //initialize the row counter
 $rowctr=0; 

 while(...)
 {
  //next use the modulo ("%") symbol to determine whether the row counter is odd or even & assign alternating colors
  ($rowctr%2==0)? $bgcol="#FFFFFF" : $bgcol="#E6E6E6";
 
  echo "<tr bgcolor='$bgcol' ..>..</tr>";

  //finally increment our row counter    
  $rowctr++;
 }
?>

Re: alternate row colors

Posted: Sat Aug 14, 2010 7:16 am
by norwichchris
ok thx guys I got it working successfully.
I didn't realise that i had mispelt one of the variables.

I may consdier taking some of your ideas for some later PHP projects.