Help - Search - Members - Calendar
Full Version: [resolved] BAB: Commenting question
Codegrrl.com Forums > Script Help > Tutorial Help
Ramsha
I wanted to show the name of my commenters on that particular entry, but since I'm hopeless at any and all PHP, I decided asking for some help would be more than beneficial.

CODE
    <?php
$sql = "SELECT * FROM php_blog_comments WHERE entry='$id' ORDER BY timestamp";
$result3 = mysql_query ($sql) or print ("Can't select comments from table php_blog_comments.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result3))
{
printf("<p>Thanks for commenting: <a href=\"%s\">%s</a></p>", stripslashes($row['url']), stripslashes($row['name']));
} ?>


I tried that, only it doesn't work. It doesn't print the commenters' names or URLs or anything. Just the "Thanks for commenting:" part.
Amelie
Try this:

CODE
<?php
$getcomments = mysql_query ("SELECT * FROM php_blog_comments WHERE entry='$id' ORDER BY timestamp") or print ("Can't select comments from table php_blog_comments.<br /> . mysql_error());
if (mysql_num_rows($getcomments) > 0) {
    echo '<p>Thanks for commenting:';
    while($commenters = mysql_fetch_array($getcomments)) {
        printf("<a href=\"%s\">%s</a>, ", stripslashes($commenters['url']), stripslashes($commenters['name']));
    }
    echo '</p>';
} ?>


I don't know if this will fix the problem, but you had the "thanks for commenting" part in the while loop, so that would have caused your commenters list to look like:

Thanks for commenting: name
Thanks for commenting: name2
Thanks for commenting: name3

I changed it so that it would be:

Thanks for commenting: name, name2, etc.

Also, I renamed the variables from $row to $commenters and $result3 to $getcomments since there other variables with those names ($row, $result3) and that might be conflicting. smile.gif
Ramsha
Gotcha! Works perfectly, thanks Amelie!

So, the while loop is for the recurring information, right?
Amelie
Yep, that's right. The while loop basically does whatever's in the {}s over and over until a condition no longer exists - in this case the condition is that there is a result from the database to display. When there are no more results, the loop ends. smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.