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!
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
<?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);
?>
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:
<?
//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++;
}
?>