Jump to content

Tables Background


nielcleo
 Share

Recommended Posts

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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...