Jump to content

Display Total Comment Count


Nicole
 Share

Recommended Posts

Hello again everyone,

I'm creating a template with an easy-to-use front-end adminbar (basically says: "Edit Page", "New Page", "Add News", "Add Product") and to make it more "user-friendly" it has the editor avatar and name as well as the options for "Profile, Help and Logout". I wanted to use PW's Comments Fieldtype and found that it's possible to display comment count here: http://processwire.c...rns-odd-number/ and I also found that it's possible to check comments since the last visit here: http://processwire.c...nce-last-visit/

I am now wondering *if* it's possible to display the total comment *count* from all pages containing the 'comments' field. Basically like this:

"Welcome Back admin, there are {$comment-count} new comments."

and that would redirect to the Comments Manager module.

I've looked around the forum but didn't find anything similar, and I'm not really good with PHP so I might've overlooked a function.

Thanks in advance.

EDIT: Can I use something like: $count = count($comment) ?

Link to comment
Share on other sites

To get all pending comments you'd have to loop through all pages that can have comments and have more than 0 comments. Then find the pending comments (status=0) and count them.

$total = 0;
foreach($pages->find("comments.count>0") as $p){
   $total += $p->comments->find("status=0")->count();
}
echo "$total comments pending";
  • Like 2
Link to comment
Share on other sites

We haven't accounted for a site-wide count in the API. But you could still do it fairly easily with an SQL query:

$result = $db->query("SELECT COUNT(*) FROM field_comments"); 
list($numTotal) = $result->fetch_row(); 
echo "<p>There are $numTotal total comments</p>";

$result = $db->query("SELECT COUNT(*) FROM field_comments WHERE status=0"); 
list($numPending) = $result->fetch_row(); 
echo "<p>There are $numPending comments waiting approval</p>";

$yesterday = strtotime("yesterday"); 
$result = $db->query("SELECT COUNT(*) FROM field_comments WHERE created>=$yesterday"); 
list($numNew) = $result->fetch_row(); 
echo "<p>There are $numNew new comments posted since yesterday</p>"; 
  • Like 4
Link to comment
Share on other sites

To get all pending comments you'd have to loop through all pages that can have comments and have more than 0 comments. Then find the pending comments (status=0) and count them.

$total = 0;
foreach($pages->find("comments.count>0") as $p){
$total += $p->comments->find("status=0")->count();
}
echo "$total comments pending";

Thank you very much for this code Soma, I imagined it would have something to do with finding the pages, then getting the fields, but I had no idea about the 'status' and how to find the pending ones. Thanks again.

We haven't accounted for a site-wide count in the API. But you could still do it fairly easily with an SQL query:

$result = $db->query("SELECT COUNT(*) FROM field_comments");
list($numTotal) = $result->fetch_row();
echo "<p>There are $numTotal total comments</p>";

$result = $db->query("SELECT COUNT(*) FROM field_comments WHERE status=0");
list($numPending) = $result->fetch_row();
echo "<p>There are $numPending comments waiting approval</p>";

$yesterday = strtotime("yesterday");
$result = $db->query("SELECT COUNT(*) FROM field_comments WHERE created>=$yesterday");
list($numNew) = $result->fetch_row();
echo "<p>There are $numNew new comments posted since yesterday</p>";

Ryan, thank you so much... I would've never been able to sort out this part and surprisingly, I can make sense of it. Thanks for the query snippets and for ProcessWire. :D

I'll test and post back to tell you how it worked.

  • Like 1
Link to comment
Share on other sites

@Nicole. The code Ryan provided are examples that archive what my code does, but using direct SQL queries with $db var.

$result = $db->query("SELECT COUNT(*) FROM field_comments WHERE status=0");
list($numPending) = $result->fetch_row();
echo "<p>There are $numPending comments waiting approval</p>";

This does the same as my code example. It's just much more efficient to do it this way with direct SQL query. "field_comments" would be the "comments" field. Each field you create has a table with it's name in the database.

Ryan just gave those examples because there's no implementation in the API that would do this ATM, so my code is kinda an not so effective version of it as it has to loop through all pages that have comments. This is no problem with smaller sites, but the more pages with comments there are the slower it would get. While it should work well up to couple hundreds of pages, Ryan's method would scale up to hundred thousands pages easily before getting any noticeable impact.

  • Like 2
Link to comment
Share on other sites

  • 1 year later...
  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...