webweever Posted December 4, 2007 Share Posted December 4, 2007 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>"; ?> Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 4, 2007 Share Posted December 4, 2007 Not too sure, but this should do the same trick... <?php $i = true; while($rows=mysql_fetch_array($result)){ if( $i ) { $bg_color = '#FFFFFF'; $i = false; } else { $bg_color = '#EEEEEE'; $i = true; } // rest of code PhREEEk Quote Link to comment Share on other sites More sharing options...
webweever Posted December 4, 2007 Author Share Posted December 4, 2007 I still get all the table rows the same color (eeeeee). So it's processing the true statement but to the false statement. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 4, 2007 Share Posted December 4, 2007 You need to wrap this line in PHP bgcolor="$bg_color"> in your table Quote Link to comment Share on other sites More sharing options...
webweever Posted December 4, 2007 Author Share Posted December 4, 2007 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>"; ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted December 4, 2007 Share Posted December 4, 2007 Need echo in there Quote Link to comment Share on other sites More sharing options...
revraz Posted December 4, 2007 Share Posted December 4, 2007 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>"; Quote Link to comment Share on other sites More sharing options...
webweever Posted December 4, 2007 Author Share Posted December 4, 2007 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(); ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted December 4, 2007 Share Posted December 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 4, 2007 Share Posted December 4, 2007 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 Quote Link to comment Share on other sites More sharing options...
roadshow Posted December 4, 2007 Share Posted December 4, 2007 if ($color == $color1){ $color = $color2; }ELSE{ $color = $color1; } $color1 = "#eeeeee"; $color2 = "#ffffff"; <tr <?php echo "bgcolor=\"$color\""; ?>> Quote Link to comment Share on other sites More sharing options...
revraz Posted December 4, 2007 Share Posted December 4, 2007 The code works fine without escaped quotes, he just didn't modify it to fit his layout. Quote Link to comment Share on other sites More sharing options...
webweever Posted December 4, 2007 Author Share Posted December 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 4, 2007 Share Posted December 4, 2007 <?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. Quote Link to comment Share on other sites More sharing options...
webweever Posted December 4, 2007 Author Share Posted December 4, 2007 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(); ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 4, 2007 Share Posted December 4, 2007 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 Quote Link to comment Share on other sites More sharing options...
revraz Posted December 4, 2007 Share Posted December 4, 2007 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: Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.