tags:

views:

57

answers:

4

I'm working on some code for my blog that displays comments. This is the code that outputs comments from the database:

$i = 0;
while ($i < $num) {
$name = mysql_result($result,$num - $i,"name");
$dream = mysql_result($result,$num - $i,"dream");


echo "$name<br>$dream<br>";

$i++;
}

I'm currently using CSS to apply a style to the entire div in which this bit of php resides. This lets me format all the comments posted, but I want to format each comment individually though uniformly. For example, I can use CSS to put a border around all the comments, but what I want is the same border around each comment to separate the comments. Any ideas? Help is appreciated.

+3  A: 

Put each comment into a div

echo "<div class='comment'>$name<br>$dream</div>";

or, semantically nicer, wrap everything into an <ul> and have each comment be a <li>:

echo "<li>$name<br>$dream</li>";

and address each li like so:

ul.comments li { border: ..... }
Pekka
+1 for the `ul` idea. Although I think the `ul` and `li` opening and closing tags need fixing.
Mike
@Mike they did indeed, thanks. Fixed :)
Pekka
I like the ul idea, but is there any way to get rid of the bullet points it creates? My page is looking amateur because the placement and coloring of the comments makes clear that they are comments so the bullet points seem unnecessary and aesthetically unpleasing.
Malcolm
@Malcolm `list-style-type: none; margin: 0px; padding: 0px` (adjust margin and padding as you wish)
Pekka
@Pekka Thanks a lot!
Malcolm
+1  A: 
echo '<div class="comment">' . "$name<br/>$dream<br/></div>";

Then in your CSS you can use

.comment {
    /* CSS rules for comments */
}

And I like Pekka's idea:

<ul class="comments">
    <?php
    $i = 0;
    while ($i < $num) 
    {
        $name = mysql_result($result,$num - $i,"name");
        $dream = mysql_result($result,$num - $i,"dream");
        ?>
        <li>
            <ul>
                <li><?php echo $name; ?></li>
                <li><?php echo $dream; ?></li>
            </ul>
        </li>
        <?php
        ++$i;
    }
    ?>
</ul>

And then in your CSS (and you can add even more classes for each name, dream, etc)

ul.comments {
    /* CSS rulse for a nice background etc for the whole group of comments */
}
ul.comments ul {
    /* CSS rules for each comment */
}
ul.comments ul li {
    /* CSS rules for each subsection of a comment
       You could make separate classes for these too */
}
Peter Ajtai
+1  A: 

Just use divs and classes.

For instance, output something like:

<div class="comment">
  <div class="author">John</div>
  <div class="commentBody">This is a comment!</div>
</div>

In this way you can style separately the whole comment (class comment), the author name (class author) and the comment itself (class commentBody)

You could also use a <li> instead of <div>s.

nico
One could also wrap a `<h1>` around the author, and a `<p>` around the comment body. It wouldn't be necessary to put a class on every element then, because the path is e.g. `div.comments > div > h1`
Pekka
@Pekka: `<h1>` to `<h6>` are designed to create headings, the author's name is definitely not an heading in this case.
nico
+1  A: 

As a suggestion, you could zebra-stripe the comments instead of placing a border around each of them. Your PHP would need to add a conditional using a modulus(2), to add a css class even/odd. And your CSS would look like this:

ul.comments li.even {
    background-color:white;
}

ul.comments li.odd {
    background-color:lightBlue;
}
Robert Hui