how do I echo the # of comments from database??

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

rhender
New php-forum User
New php-forum User
Posts: 13
Joined: Thu Dec 01, 2011 11:36 am

how do I echo the # of comments from database??

Postby rhender » Mon Mar 25, 2013 9:07 am

So, I have created a blog for myself, which allows comments.

I want to display the # of comments on the web page like so....

Comments(9)

>>>> and then the nine comments follow...

My question is this. In this example, there are 9 comments to be displayed.

How do I use PHP to echo the "9", or number of comments that are in the database?

I know this is probably a very beginner question, but I can't find an answer anywhere.

Does that make sense well enough?????

User avatar
Strider64
New php-forum User
New php-forum User
Posts: 70
Joined: Sat Mar 23, 2013 8:24 am

Re: how do I echo the # of comments from database??

Postby Strider64 » Mon Mar 25, 2013 1:19 pm

If I'm following what you are saying. The following is a very basic way you could do it, maybe created in a while loop? There are other things you can do with this code, but I think it's a good building block.

Code: Select all

<?php $menu .=  "<a href=\"index.php?comments=" .$page_no . "\ >Comment No. " . $page_no . "</a>"; ?>


Then you use echo to call the menu ---> echo $menu and then use $_GET to process the content $id = $_GET('comments'); Just remember the encapsulate your variables? 8)

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: how do I echo the # of comments from database??

Postby seandisanti » Mon Mar 25, 2013 1:51 pm

It completely depends on the way that you're interacting with your database. assuming you're using PDO (which you should be), executing a query that returns the comments will also set your statement's rowCount() equal to the number of records returned.

Code: Select all


<?php
function getComments($parent_id){
    
$db Database::getInstance();
        
$sql "SELECT * from comments WHERE parent_id=" $db->quote($parent_id);
        
$result=$db->query($sql);
        if (!
$result || $result->rowCount==0){
            return 
false//just returns falso on error or no comments.  you can figure out how you want to handle those two possibilities
        
}
        return 
$result;
}
$comments getComments($this->id);
if (!
$comments){
    die(
'drat something is amiss');
}
echo 
'Showing all ' $comments->rowCount() . ' comments for this post';
while (
$acomment=$comments->fetch(PDO::FETCH_ASSOC)){
    echo 
commentDecoratorFunction($acomment);

rhender
New php-forum User
New php-forum User
Posts: 13
Joined: Thu Dec 01, 2011 11:36 am

Re: how do I echo the # of comments from database??

Postby rhender » Wed Mar 27, 2013 1:53 pm

Thank you for the replies. I will have to look over them now, to see what all is going on. I will respond tomorrow most likely.

Thanks to both of you!


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot] and 4 guests