nielcleo Posted August 13, 2011 Share Posted August 13, 2011 Hi,im encountering conflict about alternate table TR background in loop..here's my code made with a white background: while($rows=mysql_fetch_array($result)){echo "<tr bgcolor='#fff'>";echo "<td>" . $rows['id'] . "</td>";echo "<td>";echo "<a href='view_topic.php?id="; echo $rows['id']; echo "'>" . $rows['topic'] ."</a>"; echo "<BR></td>";echo "<td align='center'>" . $rows['view'] . "</td>";echo "<td align='center'>" . $rows['reply'] . "</td>";echo "<td align='center'>" . $rows['datetime'] . "</td>";echo "</tr>";} now im planning to have an alternate background white - grey - white - grey and i put the code like this. while($rows=mysql_fetch_array($result)){ echo "<tr bgcolor='#fff'>";echo "<td>" . $rows['id'] . "</td>";echo "<td>";echo "<a href='view_topic.php?id="; echo $rows['id']; echo "'>" . $rows['topic'] ."</a>"; echo "<BR></td>";echo "<td align='center'>" . $rows['view'] . "</td>";echo "<td align='center'>" . $rows['reply'] . "</td>";echo "<td align='center'>" . $rows['datetime'] . "</td>";echo "</tr>";echo "<tr bgcolor='#eee'>";echo "<td>" . $rows['id'] . "</td>";echo "<td>";echo "<a href='view_topic.php?id="; echo $rows['id']; echo "'>" . $rows['topic'] ."</a>"; echo "<BR></td>";echo "<td align='center'>" . $rows['view'] . "</td>";echo "<td align='center'>" . $rows['reply'] . "</td>";echo "<td align='center'>" . $rows['datetime'] . "</td>";echo "</tr>";} its displayed the alternate background but the content is the same the ID 1 will display in the white background im expecting the ID 2 will be displayed on grey background but it happen the the ID 1 will display also in the grey background it will duplicate the content.. Link to comment Share on other sites More sharing options...
JamesB Posted August 13, 2011 Share Posted August 13, 2011 you could do something like this: $switcher = true;while($rows=mysql_fetch_array($result)){ $colour = $switcher ? '#fff' : '#eee'; echo "<tr bgcolor='".$colour."'>"; echo "<td>" . $rows['id'] . "</td>"; echo "<td>"; echo "<a href='view_topic.php?id="; echo $rows['id']; echo "'>" . $rows['topic'] ."</a>"; echo "<BR></td>"; echo "<td align='center'>" . $rows['view'] . "</td>"; echo "<td align='center'>" . $rows['reply'] . "</td>"; echo "<td align='center'>" . $rows['datetime'] . "</td>"; echo "</tr>"; $switcher = !$switcher;} so $switcher is a boolean variable changing between true, false, true, false, etc, and the $colour variable is set to '#fff' if the switcher is true, or '#eee' if its false.also you might wanna consider using CSS for the styling, eg. <tr style='background-color: #fff;'> or even 2 css classes.also to save confusion you might wanna change $rows to $row, as each call to mysql_fetch_array() only returns 1 row. Link to comment Share on other sites More sharing options...
nielcleo Posted August 13, 2011 Author Share Posted August 13, 2011 if im gonna use the CSS classes. what will i do to manipulate that?if i will use <tr style='background-color:#fff;'> the background will only be white.. Link to comment Share on other sites More sharing options...
JamesB Posted August 13, 2011 Share Posted August 13, 2011 well to use inline css, in my last post you would change: echo "<tr bgcolor='".$colour."'>"; to echo "<tr style='background-color: ".$colour.";'>"; to use css classes, the first 2 lines in the loop could be: $cssClass = $switcher ? 'row_col_1' : 'row_col_2'; echo "<tr class='".$cssClass."'>"; then to go with that, in an external .css file, or a html <style> tag: .row_col_1 { background-color: #fff; }.row_col_2 { background-color: #eee; } Link to comment Share on other sites More sharing options...
nielcleo Posted August 13, 2011 Author Share Posted August 13, 2011 ohh that's great its working thanks a lot Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.