Current location: Hot Scripts Forums » Programming Languages » PHP » Alternating Row Colors


Alternating Row Colors

Reply
  #1 (permalink)  
Old 10-16-04, 11:53 AM
MrDarko's Avatar
MrDarko MrDarko is offline
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">&nbsp;</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">&nbsp;</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.
Reply With Quote
  #2 (permalink)  
Old 10-16-04, 04:41 PM
Sabu Sabu is offline
Junior Code Guru
 
Join Date: Sep 2004
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
Well, I don't know about that
PHP Code:

$row_color = ($row_count 2) ? $color1 $color2
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
PHP Code:

echo "<tr bgcolor=\"$row_color\">"
__________________
define('TRUE', FALSE);
Reply With Quote
  #3 (permalink)  
Old 10-16-04, 04:44 PM
Acecool's Avatar
Acecool Acecool is offline
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"; ?>"
__________________
Check Acecoolco.com for PHP Tutorials, and other tuts
If you plan on contacting me, please read this: Legal Terms & Conditions
Reply With Quote
  #4 (permalink)  
Old 10-16-04, 06:44 PM
webx2000i webx2000i is offline
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:
Code:
$c = 0;
$Bc = "bgcolor=#E3EBFD"; // initial color
while ($sql_r = mysql_fetch_array($sql_q)){
	if ($Bc =="bgcolor=#E3EBFD"){ // changing colors here
		$Bc = "bgcolor=#FFFFD5";
	} else {
		$Bc = "bgcolor=#E3EBFD";
	}
	echo '
	<tr ' . $Bc . '> // assigning row color here
	<td width="26"><font face="Tahoma" size="2">' . c++ . '</font></td>
	</tr>
	';
}
I hope this might help
Reply With Quote
  #5 (permalink)  
Old 10-16-04, 06:51 PM
MrDarko's Avatar
MrDarko MrDarko is offline
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.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Random Row in Databse? steveo PHP 2 09-11-04 04:40 AM
strings in a mysql row Aesis PHP 8 08-18-04 02:33 AM
Delete row from mysql error. bigkid PHP 1 08-05-04 09:36 PM
Alternating Row Colors digitalje5u5 PHP 3 05-27-04 11:55 PM
Update more than one row davids PHP 10 05-07-04 04:57 PM


All times are GMT -5. The time now is 04:36 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.