Jump to content

alternating Background


user4fun

Recommended Posts

How can i mage the back ground color of a row alternate as it pulls records form table.I am guesing it is easy because you see it every where.?while($row = mysql_fetch_array($result)) {echo '<td>< /></td>';echo "<td></td>";echo "<td><td";echo "</tr>"; Then at the start of another record the color would change to bgcolor="#F2F2F2"}echo "</table>";

Link to comment
Share on other sites

so would this be righT? because this is something i'm thinking of doing for my site

$alt = true;while($row = mysql_fetch_array($result)) {  if ($alt)    #F2F2F2  else    #FFFFFFecho "<tr bgcolor=".$alt.">"; echo '<td>FName</td>';echo "<td>Sname</td>";echo "<td>Age<td>";echo "</tr>"; }echo "</table>"; 

would this be correct and alternate the back ground colour of the table?

Link to comment
Share on other sites

The original code isn't valid, you can't just write the hex color code on a line with nothing else. Also, lines starting with # are comments, so it's commented out anyway. I would do it like this:

$alt = true;while($row = mysql_fetch_array($result)) {  if ($alt)	echo "<tr bgcolor=\"#F2F2F2\">";  else	echo "<tr bgcolor=\"#FFFFFF\">";  $alt = !$alt; //make sure to toggle $alt

Link to comment
Share on other sites

justsomeguyyour not echoing the <td> or would you do that after the final echo or after the $Alt = !$alt ?

$alt = true;while($row = mysql_fetch_array($result)){if ($alt)  <tr bgcolor=\"#F2F2F2\">";else <tr bgcolor=\"#FFFFFF\">";echo "<td>FName</td>";echo "<td>Sname</td>";echo "<td>Age<td>";echo "</tr>";$alt = !$alt;}echo "</table>"; 

so that would be correct right?

Link to comment
Share on other sites

Your missing two echo's in your code...it should be:

if ($alt)   echo "<tr bgcolor=\"#F2F2F2\">";else   echo "<tr bgcolor=\"#FFFFFF\">";

not

if ($alt)<tr bgcolor=\"#F2F2F2\">";else<tr bgcolor=\"#FFFFFF\">";

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...