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
bironeb
Forum Commoner
Posts: 59 Joined: Thu Nov 20, 2003 12:02 pm
Post
by bironeb » Wed Apr 14, 2004 6:21 pm
I am pretty new to php, and I am creating a table that the background color changes on ever other entry. I have it working, but I know there has got to be a better way. Below is my code. Please feel free to reply with any better ideas. It would be much appreciated. thanks
"WARNING: the below code may make you laugh!"
Code: Select all
<?php
//create list block of results
$record_list ="<table border="0" cellpadding="0" cellspacing="0"><tr><td width="200"><h3>NAME</h3>";
$record_list .="</td><td><h3>USER NAME</h3></td></tr>";
while ($row = mysql_fetch_array($result)) {
$count += 1;
if (($count == "1") || ($count == "3") || ($count == "5") || ($count == "7") ||
($count == "9") || ($count == "11") || ($count == "13") || ($count == "15") ||
($count == "17") || ($count == "19") || ($count == "21")) {
$color = "CCCCFF";
} else {
$color = "CCCCCC";
}
$user_id = $row['user_id'];
$user_name = $row['user_name'];
$record_list .= "<tr><td bgcolor="$color">";
$record_list .= "<a href="index.php?main=show_record&user_id=$user_id" style="text-decoration:none"><strong>$user_name</strong></a>";
$record_list .= "</td><td width="200" bgcolor="$color"><strong>$user_id</strong></td></tr>";
}
$record_list .="</table>";
?>
bironeb
Forum Commoner
Posts: 59 Joined: Thu Nov 20, 2003 12:02 pm
Post
by bironeb » Wed Apr 14, 2004 6:46 pm
Well I did a little more reading and found another way to do it. But I still think there may be a better way. Below is the new way I found I could do it. If you have a better suggestion please post. Thanks,
Code: Select all
<?php
//create list block of results
$record_list ="<table border="0" cellpadding="0" cellspacing="0"><tr><td width="200"><h3>NAME</h3>";
$record_list .="</td><td><h3>USER NAME</h3></td></tr>";
while ($row = mysql_fetch_array($result)) {
$count += 1;
$color = $count % 2;
echo "$color";
if($color == "1"){
$color = "99CCFF";
} else {
$color = "CCCCCC";
}
$user_id = $row['user_id'];
$user_name = $row['user_name'];
$record_list .= "<tr><td bgcolor="$color">";
$record_list .= "<a href="index.php?main=show_record&user_id=$user_id" style="text-decoration:none"><strong>$user_name</strong></a>";
$record_list .= "</td><td width="200" bgcolor="$color"><strong>$user_id</strong></td></tr>";
}
$record_list .="</table>";
?>
RadixDev
Forum Commoner
Posts: 66 Joined: Sun Mar 14, 2004 11:27 am
Location: U.K.
Post
by RadixDev » Wed Apr 14, 2004 6:55 pm
I kinda like it this way cos it separates HTML from PHP:-
Code: Select all
<?php
while($row = mysql_fetch_array($result) {
$count += 1;
if($count == (1,3,5,7,9,11,13,15,17,19,21) { // Are the numbers goes on like ...23,25 etc.
// Use below if it does, replace with this...
// if(($count % 2) == 1) {
$color = "CCCCFF";
} else {
$color = "CCCCCC";
}
$user_id = $row['user_id'];
$user_name = $row['user_name'];
}
?>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td width ="200"><h3>NAME</h3></td>
<td><h3>USER NAME</h3></td>
</tr>
<tr>
<td bgcolor="<?=$color ?>">
<a gref="index.php?main=show_record&user_id=<?=$user_id ?>" style="text-decoration: none;"><strong><?=$user_name ?></strong></a>
</td>
<td width="200" bgcolor="<?=$color ?>"><strong><?=$user_id ?></strong>
</td>
</tr>
</table>