Forums

Getting number of comments? [SOLVED with Code Sugestion]

heldrida 09 Dec, 2008
hi,

I've got a question, in blog view (Section / Category listing), how can I retrieve the
number of comments available in a article item ?

I want to do something like:

Written by Author in 10/12/2008 | Comments ( XX )

Where XX is the number of comments in the current article.

Thanks for your atention!
Max_admin 09 Dec, 2008
Hi heldrida,

if you want to change the text of the current link then you can do this in the language file, is this what you need ?

Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
heldrida 09 Dec, 2008
hi,
thanks for your atention.

No. What I need is diferent. I would like to, get the number of comments for that particular article, listed in blog view. I mean
when its displaying items by Section or by Category.

In joomla I can easily retrieve the Author name, and when it was created, etc. But how to get the number of available comments?
Is that easy?

I want to put that, in the Blog view, like

Written by AUTHOR_NAME on DATE_WHEN_WAS_CREATED | Comments ( NUMBER_OF_COMMENTS )

Being, AUTHOR_NAME already solved;

DATE_WHEN_WAS_CREATED already solved;

NUMBER_OF_COMMENTS dont know how to get that.

Thanks
Max_admin 09 Dec, 2008
Hi, did you enable the "view number of comments in blog view" setting in comments parameters ?

regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
heldrida 09 Dec, 2008
hey😉

Yeah, thats a way to have it working, but, I've done a lot of costumization and a little scripting on my templates,
so showing number of comments work a lil bit diferently from what I want🙂

So, imagining it doesnt existe (the show number of comments in blog view)

Is there a way to grab/get the number of comments? a command ?

Thanks
Max_admin 09 Dec, 2008
ok, try to check plugins/content/chronocomments.php then, it has the necessary code, its not 1 line but easy to get!

Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
heldrida 10 Dec, 2008
Hi,
thanks for your sugestion, but I didnt understood how to use it,
so I made a simple script for that:
<!--Begin Nr of Comments  -->

<!-- <a> Start/begin, link to article with chrono comments  -->
<?php echo '<a class="readOnDica" href="'.JRoute::_(ContentHelperRoute::getArticleRoute($this->item->slug, $this->item->catslug, $this->item->sectionid)).'">'; ?> 
					
<?php

<!-- Get article id -->
$articleid = $this->item->id;

<!--Get needed info from the database tables jos-chrono_comments and jos_content -->
$query = mysql_query("SELECT count( `jos_chrono_comments`.`pageid` ) , `jos_content`.`id`
FROM `jos_chrono_comments` , `jos_content`
WHERE `jos_chrono_comments`.`pageid` = `jos_content`.`id`
AND
`jos_content`.`id` = '".$articleid."'
GROUP BY `jos_content`.`id`;");

<!-- count number of comments available and apply value to variable -->
$nrComments = count(`pageid`);
 
<!-- if any result available in the query  give nr comments (X) -->
if($row = mysql_fetch_array($query)){
echo "Comments (".$row[$nrComments].")";
}

<!--If theres no result in the query then give zero comments  -->
else {
echo "Comments (0)";
}
?>

<!-- <a> Start/begin, link to article with chrono comments  -->
<?php echo "</a>";	?>

<!-- End Nr of Comments  -->
In case someone else is needing this, fell free to use it!
Max_admin 10 Dec, 2008
Thanks for sharing this🙂

regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 10 Dec, 2008
Hi helrida,

I think the html comments you added may stop this script from working correctly. Here's an amended version for other readers who want to try the script.
<?php 
// Begin Nr of Comments
// Start/begin, link to article with chrono comments
echo '<a class="readOnDica" href="'.JRoute::_(ContentHelperRoute::getArticleRoute($this->item->slug, $this->item->catslug, $this->item->sectionid)).'">';

// Get article ID
$articleid = $this->item->id;

// Get needed info from the database tables jos-chrono_comments and jos_content
$db =& JFactory::getDBO();
$query = "
    SELECT count( `jos_chrono_comments`.`pageid` ) , `jos_content`.`id`
        FROM `jos_chrono_comments` , `jos_content`
        WHERE `jos_chrono_comments`.`pageid` = `jos_content`.`id`
            AND `jos_content`.`id` = '".$articleid."'
        GROUP BY `jos_content`.`id`;";
$db-> setQuery($query);

// count number of comments available and apply value to variable
$nrComments = count(`pageid`);
if ( $row = $db->loadAssoc($query) ) {
    // if any result available in the query  give nr comments (X)
    echo "Comments (".$row[$nrComments].")";
} else {
    // If there's no result in the query then give zero comments  -->
    echo "Comments (0)";
}
// Close link to nr of Comments
echo "</a>";   
// -- End Nr of Comments  --
?>
NB I've also changed over to Joomla DB code. Not tested and may need de-bugging.

Bob

>[/code]
heldrida 10 Dec, 2008
Thanks for your atention people!
I'm allways learning Grey =) Nice sugestion

Chrono Comments, has lots of potential! Really
loving it =)

- I've got another questions, I guess its better, to
open a new thread. Its about Displaying the comments
after DisplayContent, I mean, I want to keep the
signature of the article just after its text/content;
and then, I would like to have comments.

right now, I've got chrono, being displayed just after
the text/content of the article.
When I refear to DisplayContent, I mean:

$this->article->event->afterDisplayContent;

Here's the thread (for future readers, its allways best to open a new thread right?):
http://www.chronoengine.com/forums/index.php?option=com_chronoforums&cont=posts&f=2&t=11889
samatt 19 Jun, 2009
Hi,

Nice script, I just can't get it to quite work when inserted into com_content/section/blog_item.php.

It's not returning the number of comments, I just get comments().

It is detecting empty articles as it should, they return comments(0).

My code has pretty well just been cut and paste from the script above..



    <?php
// Begin Nr of Comments
// Start/begin, link to article with chrono comments
echo '<a class="readOnDica" href="'.JRoute::_(ContentHelperRoute::getArticleRoute($this->item->slug, $this->item->catslug, $this->item->sectionid)).'">';

// Get article ID
$articleid = $this->item->id;

// Get needed info from the database tables jos-chrono_comments and jos_content
$db =& JFactory::getDBO();
$query = "
    SELECT count( `jos_chrono_comments`.`pageid` ) , `jos_content`.`id`
        FROM `jos_chrono_comments` , `jos_content`
        WHERE `jos_chrono_comments`.`pageid` = `jos_content`.`id`
            AND `jos_content`.`id` = '".$articleid."'
        GROUP BY `jos_content`.`id`;";
$db-> setQuery($query);

// count number of comments available and apply value to variable
$nrComments = count(`pageid`);
if ( $row = $db->loadAssoc($query) ) {
    // if any result available in the query  give nr comments (X)
    echo "Comments (".$row[$nrComments].")";
} else {
    // If there's no result in the query then give zero comments  -->
    echo "Comments (0)";
}
// Close link to nr of Comments
echo "</a>";   
// -- End Nr of Comments  --
?>

    <?php } ?>



[attachment=0]comment.gif[/attachment]

Just running on localhost via WAMP at the moment. Any help would be great.

Kind regards,

Matt
GreyHead 19 Jun, 2009
Hi samatt,

I'm not familiar with the ChronoComments code but his line looks wrong to me
    echo "Comments (".$row[$nrComments].")";
I think that $row should be an array with two entries $row[?] and $row['id'] - I don't know what the index for the COUNT entry would be, I suggest that you echo $row out and take a look.

Bob
whisted 26 Oct, 2009
This solved my problem:

change this line:
echo "Comments (".$row[$nrComments].")";


to this:
echo "Comments (".$row["count( `jos_chrono_comments`.`pageid` )"].")";
nml375 26 Oct, 2009
A suggestion in general when using the COUNT() (or other) function in queries, give the column an alias, and use that alias to index your result:
...SELECT count( `jos_chrono_comments`.`pageid` ) AS comments,
 ...
echo "Comments (" . $row['comments'] . ")";
...


/Fredrik
TK1990 27 Feb, 2010
hi,

im just a newbie at this, so where exactly must i write this in?

best regards,
theo
fardoshi 26 Jun, 2010
Thanks,

Very Useful

Cheers,

Brian
asthmaz2 25 Jul, 2010
I'm a newb and I appreciate everyone's responses. Thanks especially to /Fredrik --> you saved me quite a bit of time!
davebentley 02 Oct, 2010
Forgive this poor newbie's inexperience (I prefer that to naivety!). I can appreciate the coding for showing specific author comment counts but where is this best put to use. I mean...what benefit does it provide when compared to the comment count already available?
This topic is locked and no more replies can be posted.