10-16-04, 11:53 AM
Newbie Coder
Join Date: Jul 2004
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
Alternating Row Colors
I have been searching all over the internet and found
MANY snippits to alternate the colors of a table. no matter which one i use my table is just highlighted in green, i cant get any script to work. i am thinking because of how my script is written.
take a look.
PHP Code:
<?
include( "dbinfo.inc.php" );
mysql_connect ( $host , $username , $password );
@ mysql_select_db ( $database ) or die( "Unable to select database" );
$query = "SELECT * FROM contacts" ;
$result = mysql_query ( $query );
$color1 = "#CCFFCC" ;
$color2 = "#0080C0" ;
$num = mysql_numrows ( $result );
mysql_close ();
?>
<? include( "header.php" ); ?>
<?
echo "<b><center>Database Output</center></b><br><br>" ;
?>
<table width="779" height="440" border="0" cellspacing="0">
<tr>
<td width="119" height="440"> </td>
<td width="543" valign="top"><table width="550" border="0" cellpadding="0" cellspacing="0">
<tr>
<th width="77"><font face="Arial, Helvetica, sans-serif">Name</font></th>
<th width="68"><font face="Arial, Helvetica, sans-serif">Phone</font></th>
<th width="68"><font face="Arial, Helvetica, sans-serif">Mobile</font></th>
<th width="89"><font face="Arial, Helvetica, sans-serif">Fax</font></th>
<th width="50"><font face="Arial, Helvetica, sans-serif">E-mail</font></th>
<th width="71"><font face="Arial, Helvetica, sans-serif">Website</font></th>
<th width="64"><font face="Arial, Helvetica, sans-serif">Update</font></th>
<th width="56"><font face="Arial, Helvetica, sans-serif">Delete</font></th>
</tr>
<?
$row_count = 0 ;
$i = 0 ;
while ( $i < $num ) {
$id = mysql_result ( $result , $i , "id" );
$first = mysql_result ( $result , $i , "first" );
$last = mysql_result ( $result , $i , "last" );
$phone = mysql_result ( $result , $i , "phone" );
$mobile = mysql_result ( $result , $i , "mobile" );
$fax = mysql_result ( $result , $i , "fax" );
$email = mysql_result ( $result , $i , "email" );
$web = mysql_result ( $result , $i , "web" );
$row_color = ( $row_count % 2 ) ? $color1 : $color2 ;
?>
<tr>
<td bgcolor="$row_color" height="18" class="menutext1"><font face="Arial, Helvetica, sans-serif"><? echo " $first $last " ; ?> </font></td>
<td bgcolor="$row_color" class="menutext1"><font face="Arial, Helvetica, sans-serif"><? echo " $phone " ; ?> </font></td>
<td bgcolor="$row_color" class="menutext1"><font face="Arial, Helvetica, sans-serif"><? echo " $mobile " ; ?> </font></td>
<td bgcolor="$row_color" class="menutext1"><font face="Arial, Helvetica, sans-serif"><? echo " $fax " ; ?> </font></td>
<td bgcolor="$row_color" class="menutext1"><div align="center"><a href="mailto:<? echo " $email " ; ?> ">E-mail</a></div></td>
<td bgcolor="$row_color" class="menutext1"><div align="center"><a href="<? echo " $web " ; ?> ">Website</a></div></td>
<td bgcolor="$row_color" class="menutext1"><div align="center"><a href="http://www.stryksta.com/tests/update.php?id=<? echo $id ; ?> ">Update</a></div></td>
<td bgcolor="$row_color" class="menutext1"><div align="center"><a href="http://www.stryksta.com/tests/delete.php?id=<? echo $id ; ?> ">Delete</a></div></td>
</tr>
<?
$row_count ++;
++ $i ;
}
echo "</table>" ;
?>
</table></td>
<td width="111"> </td>
</tr>
</table>
it would seem that i have it all right, i have moved it around and tried to use many scripts all highlight green.
10-16-04, 04:41 PM
Junior Code Guru
Join Date: Sep 2004
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
Well, I don't know about that
business, but I do see where your problem is.
On the cells themselves, you've tried to set $row_color. But the problem is, that part of the code isn't in the php section (<? ?>) so it can't possibly treat it like a variable and just prints out "$row_color" instead.
What I would do, is remove the bgcolor for all those second set of cells, move the <tr> into the coded part, and change it to
__________________
define('TRUE', FALSE);
10-16-04, 04:44 PM
Aspiring Coder
Join Date: Nov 2003
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
bgcolor="$row_color"
to
bgcolor="<?php echo "$row_color"; ?>"
10-16-04, 06:44 PM
Newbie Coder
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
here is what i'm using in myweb page:
I hope this might help
10-16-04, 06:51 PM
Newbie Coder
Join Date: Jul 2004
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
I tried the first two solutions and they worked great thanks!, i didnt try that last one, i wasnt sure if that would conflict with my other if and while statments.
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off