tags:

views:

21

answers:

3

In the comment leaving system on part of my website there is a weird blank entry that always remains above the latest real entry. I've looked in the database directly and there isn't a blank entry. Yet, on the top of all my comments, there is always an empty box that is formatted the same way as the rest of the comments. Where is this coming from and how do I get rid of it? Here is the php:

$query = "SELECT * FROM catharsis";
$result = mysql_query($query);
$num = mysql_numrows($result);

mysql_close();
echo "<h4><center>Let it out.</center></h4>";


echo '<ul class="comments">';

for ($i = 0; $i < $num; $i++) {
    $name = mysql_result($result,$num - $i,"message");
    echo "<li>$name</li>";
    echo '<br>';
}
echo '</ul>';

relevant css:

ul.comments {
list-style-type: none;
position: relative;
right: 2.5em;

}

ul.comments li {
list-style-type: none;
background-color: c0dbff;
border-style: solid;
border-width: 1px;
border-color: black;
padding: .4em;
}
+1  A: 

Can you confirm that if you echo $num it is zero?

You don't want to be printing a ul when you don't have any li entries anyway so you'd want something like this:

if ($num > 0)
{
    echo '<ul class="comments">'; 

    for ($i = 0; $i < $num; $i++) { 
        $name = mysql_result($result,$num - $i,"message"); 
        echo "<li>$name</li>"; 
        echo '<br>'; 
    } 
    echo '</ul>'; 
 }
Graphain
+2  A: 

I believe it comes from the first time this line is executed:

$name = mysql_result($result,$num - $i,"message");

Since your results are indexed from 0 to $num-1 you won't have an entry with the index $num-0. Change that line to read

$name = mysql_result($result,$num - $i -1,"message");

or start $i at 1 and let it run up to <= $num

Josiah
+1 - I was writing this!
laurent-rpnet
I used the start $i at 1 and run up to <= solution and it worked. Thank you very much!
Jimmy
Glad to have helped :)
Josiah
+3  A: 
$query = 'SELECT message FROM catharsis ORDER BY id DESC';
$result = mysql_query($query);
mysql_close();
echo '<h4><center>Let it out.</center></h4>';


echo '<ul class="comments">';

while($t = mysql_fetch_array($result)){
    echo '<li>'.$t[0].'</li><br>';
}
echo '</ul>';
Robus