Jump to content
Nicole Bardales

Display Total Comment Count

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) ?

Share this post


Link to post
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

Share this post


Link to post
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 3

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

I'm happy to announce both codes (Soma's and Ryan's) work.

@Ryan, I don't know why, but counting the comments worked even without the database query (using only Soma's code)

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

@Soma, thanks for that wonderful explanation. I was wondering why both methods worked, this makes a lot of sense. I'll be using the SQL Query version, then. :D

Share this post


Link to post
Share on other sites

Hey,

Is there anyway you can do this for just one page?

i.e: "How many comments are for Page A?"

Thanks.

Share this post


Link to post
Share on other sites

Actually i found the answer :)

$num = count($Page->comments); 
"<p>Page has $num Comments</p>"
  • Like 4

Share this post


Link to post
Share on other sites

...just a quick edit :) :

		<?php 
      
		$num = count($page->comments); 
                echo "<p>Page has $num Comments</p>";
      
		?>
  • Like 1

Share this post


Link to post
Share on other sites

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By sambadave
      Hi there
      Short version of question
      Let's say I have a page in the admin that contains a field... Is it possible to output the content from that field on another page in the admin? Almost like a reference.
      Longer version of question (with example)
      A house builder with multiple (60+) developments. They want to be able to create notices/messages that can be added to one or many developments. Handy for things like regional covid lockdowns or temporary office closures due to bad weather.
      My approach for the admin editing options:
      Add each message to each development
      Pros: You edit the message on the development page in context
      Cons: Very time consuming and repetitive if the same message needs to be applied to 60+ developments
        Control all the messages from one admin page and say which development each message should be applied to
      Pros: Easier to add/remove messages to more than one development at a time. Control all messages from one place.
      Cons: Content is not added on development page, which is where typical admin users may expect to find it I went for option 2 due to flexibility, and created a page within the admin for global development notices. This contains a repeater with:
      Field for message to display Checkbox list of all developments. The user can select which ones to apply each message to It's working really well but the only thing is that if the user goes to a specific development in the admin, the relevant messages aren't displayed in context (as they aren't edited on that page and instead on the global development notices page)... which may cause confusion when a new staff member / content admin tries to edit the text but there is no field when they go to the development admin page where they expect to see it...
      Solution???
      I don't require the message(s) to also be editable on the development page, but I wondered if there was a nice way to show it/them somehow. I feel like I am missing something really simple as I'm sure ProcessWire will have a nice way of achieving this, or maybe there are field settings that allow this kind of thing to happen?
      Any ideas on approaches or similar experiences would be much appreciated, even if it is just a much simpler example with the content from one field being shown on another admin page to get the ball rolling.
      Thanks in advance for any advice :)
    • By franciccio-ITALIANO
      Hi everyone.
      I've created 12 templates that are the same but each with an extra bit of html code. 
      The piece of code is as follows:
      <div> <div class="box-pf"> <i class="fa fa-map-pin fa-2x fa-red faa-pulse animated"></i> <a href=""> <span class="uk-text-middle"><i>Sonchus oleraceus</i> 'Grespino degli Orti'</span></b> </a> </div> </div> On the third line we read "fa-red."
      I created 12 similar templates.
      The first template has only one box with fa-red, the last template has 12 boxes with icons of 12 different colors.
      So. is there any way to have only 1 template and add, if I want and when I want, a small or big, same or different piece of html code?
       
    • By humanafterall
      Hi,
      I would like to set an admin template to 'https only' as recommended in the Processwire security docs.
      However if I do this it forces this setting locally too, resulting in https://localhost requests which result in an error page.
      Is there a simple way round this? Setting https for templates in the config?
      Thanks!
    • By DanielKit
      Hi. I'm currently stuck at the login page in my project. Once I enter my admin username and my password and press login, nothing happens. The page just reloads. However, the URL changes from http://myipaddress/processwire to http://myipaddress/processwire/?login=1. I've checked all of my server settings, and to my knowledge, all seems to be fine there. I don't know where to go from here.. Thanks in advance!
    • By modifiedcontent
      Small annoyance: I get a horizontal scrollbar in UIkit admin area - or actually Admin Theme Boss based on Uikit 3. I tried to fix it with CSS, but had trouble isolating/targeting it and don't want to mess with module or core files. I think this issue has been reported before. Is there a recommended fix? 
×
×
  • Create New...