Jump to content

[SOLVED] Alternating table row colors problem


webweever

Recommended Posts

Can anyone tell me why the code below gives me a table with eeeeee background and not alternating between ffffff and eeeeee?

 

<?php
$i=0;

while($rows=mysql_fetch_array($result)){
if($i % 2)
{
$bg_color = '#FFFFFF';
}
else
{
$bg_color = '#EEEEEE';
}


$name = $row["name"];
$url = $row["url"];
$cat = $row["cat"];
?>
<tr>
<td align="center" width="30" bgcolor="$bg_color"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="$bg_color"><font face='verdana'><font size='1'><a target="_blank" href="<?php echo $rows['url']; ?>"><?php echo $rows['name']; ?></a> 
<td width="50" bgcolor="$bg_color"><font face='verdana' ><font size='1'><? echo $rows['cat']; ?></td>
</td>
</tr>

<?
$i++;
}


echo "</table>";

?>

Link to comment
Share on other sites

My code is below, now I get all grey background for each row.

 

<?php
$i = true;

while($rows=mysql_fetch_array($result)){
if( $i ) {
    $bg_color = '#FFFFFF';
    $i = false;
} else {
    $bg_color = '#EEEEEE';
    $i = true;
}


$name = $row["name"];
$url = $row["url"];
$cat = $row["cat"];
?>
<tr>
<td align="center" width="30" bgcolor="<?php $bg_color ?>"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="<?php $bg_color ?>"><font face='verdana'><font size='1'><a target="_blank" href="<?php echo $rows['url']; ?>"><?php echo $rows['name']; ?></a> 
<td width="50" bgcolor="<?php $bg_color ?>"><font face='verdana' ><font size='1'><? echo $rows['cat']; ?></td>
</td>
</tr>

<?
$i++;
}


echo "</table>";

?>

Link to comment
Share on other sites

This is what I did

 

$color = "#ffffff";
$color1 = "#ffffff";
$color2 = "#99ffff";
while ($row = mysql_fetch_row($result)) {
if ($color == $color1){
$color = $color2;
}ELSE{
$color = $color1;
}
$id=$row[0];
echo "<tr bgcolor=$color>";

Link to comment
Share on other sites

With the code above i don't get the complete table output. I must not be understanding you correctly.

 

Code:

 

$sql="SELECT * FROM $tbl_name ORDER BY cat ASC";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

?>
<table width="500" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td><form name="form1" method="post" action="">
<table width="500" border="1" cellpadding="2" cellspacing="0" bgcolor="#CCCCCC" align="center">
<tr>
<td bgcolor="#FFFFFF" width="30"><font face='verdana'><font size='2'><strong>Delete</strong></td>
<td bgcolor="#FFFFFF"><font face='verdana'><font size='2'><strong>URL</strong></td>
<td bgcolor="#FFFFFF" width="50"><font face='verdana'><font size='2'><strong>Category</strong></td>
</tr>
<?php
$color = "#ffffff";
$color1 = "#ffffff";
$color2 = "#99ffff";
while ($row = mysql_fetch_row($result)) {
if ($color == $color1){
$color = $color2;
}ELSE{
$color = $color1;
}
$id=$row[0];
echo "<tr bgcolor=$color>";
}
$name = $row["name"];
$url = $row["url"];
$cat = $row["cat"];
?>
<tr>
<td align="center" width="30" bgcolor="<?php $bg_color ?>"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="<?php $bg_color ?>"><font face='verdana'><font size='1'><a target="_blank" href="<?php echo $rows['url']; ?>"><?php echo $rows['name']; ?></a> 
<td width="50" bgcolor="<?php $bg_color ?>"><font face='verdana' ><font size='1'><? echo $rows['cat']; ?></td>
</td>
</tr>

<?
$i++;
//}


echo "</table>";

?>
<?php
//}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?
// Check if delete button active, start this 
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php 
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}
}
mysql_close();
?>

Link to comment
Share on other sites

I only showed you a snippet of my code for my project.  You would have to echo all the $row[]s to get the full output.

 

It was more of an example on how to switch the colors instead of to just copy and paste into your code.

Link to comment
Share on other sites

You need to ECHO the values

<td align="center" width="30" bgcolor="<?php ech $bg_color ?>"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="<?php echo $bg_color ?>"><font face='verdana'><font size='1'><a target="_blank" href="<?php echo $rows['url']; ?>"><?php echo $rows['name']; ?></a> 
<td width="50" bgcolor="<?php echo $bg_color ?>"><font face='verdana' ><font size='1'><?php echo $rows['cat']; ?></td>

 

Ken

Link to comment
Share on other sites

I can only get this thing to "work", and by work I mean no errors with this code:

 

<?php

while ($row = mysql_fetch_row($result)) {
if ($color == $color1){
$color = $color2;
}ELSE{
$color = $color1;
}

$color1 = "#eeeeee";
$color2 = "#ffffff";

echo "<tr bgcolor=$color>";
echo "<td>Name</td>";
echo "<td>Url</td>";
echo "<td>Cat</td>";
echo "</tr>";
}

?>

 

This only gives the row a background color.

Link to comment
Share on other sites

<?php
$i = true;

while($rows=mysql_fetch_array($result)){
if( $i ) {
    $bg_color = '#FFFFFF';
    $i = false;
} else {
    $bg_color = '#EEEEEE';
    $i = true;
}


$name = $row["name"];
$url = $row["url"];
$cat = $row["cat"];
?>
<tr bgcolor ="<?php echo $bg_color; ?>"
<td align="center" width="30"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td><font face='verdana'><font size='1'><a target="_blank" href="<?php echo $rows['url']; ?>"><?php echo $rows['name']; ?></a> 
<td width="50"><font face='verdana' ><font size='1'><? echo $rows['cat']; ?></td>
</td>
</tr>

<?php
}


echo "</table>";

?>

 

This is wrong in your code also

 

<input name="checkbox[]"

 

Looks like you are trying to use a PHP array in HTML.

Link to comment
Share on other sites

I'm not even using all the HTML. I'm trying to rebuild the whole table to get this alternating color thing to work.

 

<?php

while ($row = mysql_fetch_row($result)) {
if ($color == $color1){
$color = $color2;
}ELSE{
$color = $color1;
}

$color1 = "#eeeeee";
$color2 = "#ffffff";

echo "<tr bgcolor=$color>";
echo "<td>Name</td>";
echo "<td>Url</td>";
echo "<td>Cat</td>";
echo "</tr>";
}

?>
<?
$i++;
//}


echo "</table>";

?>
<?php
//}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?
// Check if delete button active, start this 
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php 
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}
}
mysql_close();
?>

Link to comment
Share on other sites

Let's go back to your original code and modify that to work:

<?php
$bg_color = '';
while($rows=mysql_fetch_assoc($result)){
    $bg_color = ($bg_color == 'FFFFFF')?'EEEEEE':'FFFFFF'; // if it's "ffffff" change it to "eeeeee", else make it "ffffff"
?>
<tr>
<td align="center" width="30" bgcolor="#<?php echo $bg_color ?>"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#<?php echo $bg_color ?>"><font face='verdana'><font size='1'><a target="_blank" href="<?php echo $rows['url']; ?>"><?php echo $rows['name']; ?></a> 
<td width="50" bgcolor="<?php echo $bg_color ?>"><font face='verdana' ><font size='1'><? echo $rows['cat']; ?></td>
</td>
</tr>

<?php
}
echo "</table>";

?>

 

I would change the above code to use a style attribute on the <tr> tag instead and stay in PHP:

<?php
$bg_color = '';
$tmp = array();
while($rows=mysql_fetch_assoc($result)){
    $bg_color = ($bg_color == 'FFFFFF')?'EEEEEE':'FFFFFF'; // if it's "ffffff" change it to "eeeeee", else make it "ffffff"
    $tmp[] = '<tr style="font-family:verdana;font-size:80%;background-color:#' . $bg_color . '>';
    $tmp[] = '<td align="center" width="30"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="' . $rows['id'] . '"></td>';
    $tmp[] = '<td><a target="_blank" href="' . $rows['url'] . '">' . $rows['name'] . '</a></td> ';
    $tmp[] = '<td width="50">' . $rows['cat'] . '</td>';
    $tmp[] = '</tr>';
}
$tmp[] = '</table>';
echo implode("\n",$tmp)."\n";
?>

 

Ken

Link to comment
Share on other sites

Thats what I did in my previous post, but it appears he isn't reading what people are posting.  He also isn't looking at any of the code or he would see where the errors were fixed.

 

Let's go back to your original code and modify that to work:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.