...

View Full Version : Display comment tables in different colors



guvenck
12-27-2006, 11:11 PM
I am preparing a comment system. Have no trouble with displaying comments.




$classes = array("blue","red","green","orange","yellow");

include("db.php");
$query = "SELECT ID,title,body,date FROM comments ORDER BY date DESC";
$result = mysql_query($query) or die(mysql_error());
$num_comments = mysql_num_rows($result);
if($num_comments == 0) {
echo '<p>No comments made yet.</p>';
} else {
echo '<p>' . $num_comments . ' comments made.</p>';
$i = 0;
while ($row = mysql_fetch_array($result)) {
echo '<table id="comments" class="' . $classes[$i] .'" cellspacing="0">';
echo '<tr>';
echo '<td valign="top">';
echo '<span>' . $row['title'] . '</span>';
echo '<hr>';
echo '<p>' . $row['body'] . '</p>';
echo '</td>';
echo '</tr>';
echo '</table>';
echo '<br>';
$i++;
}
}



and external CSS file:




.blue {
background-color: blue;
}

.red {
background-color: red;
}

.green {
background-color: green;
}

.orange {
background-color: orange;
}

.yellow {
background-color: yellow;
}



I want the comment tables displayed in different colors and start over again when end of array is reached. In my case, display blue after yellow again.

Any ideas?

whizard
12-27-2006, 11:16 PM
put this at the beginning of your while loop:



if($i > count($classes))
{
$i = '0';
}


Dan

guvenck
12-27-2006, 11:40 PM
This approach seems to work, may not be the best way though.
I display the comments according to date so when a new comment is added, all comments change color again and single comments do not keep their color. Hope you get what I mean.




if($classes[$i] == end($classes)) {
$i = 0;
}



Final code:



$classes = array("blue","red","green","orange","yellow");

include("db.php");
$query = "SELECT ID,title,body,date FROM comments ORDER BY date DESC";
$result = mysql_query($query) or die(mysql_error());
$num_comments = mysql_num_rows($result);
if($num_comments == 0) {
echo '<p>No comments made yet.</p>';
} else {
echo '<p>' . $num_comments . ' comments made.</p>';
$i = 0;
while ($row = mysql_fetch_array($result)) {
echo '<table id="comments" class="' . $classes[$i] .'" cellspacing="0">';
echo '<tr>';
echo '<td valign="top">';
echo '<span>' . $row['title'] . '</span>';
echo '<hr>';
echo '<p>' . $row['body'] . '</p>';
echo '</td>';
echo '</tr>';
echo '</table>';
echo '<br>';
$i++;
if($classes[$i] == end($classes)) {
$i = 0;
}
}
}

whizard
12-27-2006, 11:47 PM
This approach seems to work, may not be the best way though.
I display the comments according to date so when a new comment is added, all comments change color again and single comments do not keep their color. Hope you get what I mean.




if($classes[$i] == end($classes)) {
$i = 0;
}



I don't really see a difference between my suggestion and your final code. If you got your thing to work, that's good!

No matter which version of code you use, the coloring system is completely separate from the comments themselves, and it will will always color the first one blue, the second one red, and so on.

Dan

guvenck
12-28-2006, 12:05 AM
Thanks for the help anyway. About the colors, you know, it never ends, we always want more :) For exp, I now try how to make the tables corners look round :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum