user4fun Posted July 6, 2007 Share Posted July 6, 2007 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 More sharing options...
justsomeguy Posted July 6, 2007 Share Posted July 6, 2007 $alt = true;while (...){ if ($alt) //color 1 else //color 2 $alt = !$alt;} Link to comment Share on other sites More sharing options...
morrisjohnny Posted July 11, 2007 Share Posted July 11, 2007 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 More sharing options...
shlomk'e Posted July 11, 2007 Share Posted July 11, 2007 Yes morris its good, omg justsomeguy you are genius... Link to comment Share on other sites More sharing options...
justsomeguy Posted July 11, 2007 Share Posted July 11, 2007 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 More sharing options...
morrisjohnny Posted July 11, 2007 Share Posted July 11, 2007 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 More sharing options...
justsomeguy Posted July 11, 2007 Share Posted July 11, 2007 Right. The code I posted just started the tr, everything after that is up to you. Link to comment Share on other sites More sharing options...
Mr_CHISOL Posted July 12, 2007 Share Posted July 12, 2007 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.