Recent Comments Add-on

I finally got my recent comments in last night after hunting around the EE database. I simply used the same one I created for pMachine Pro, with modifications to the table and field names. This does require adding a new Template, whether in a new group or in the weblog group.

Disclaimation Proclamation: I can’t guarantee this will work on your site, but I’m willing to bet that if you asked me I might be able to help you. :)


Basically, because I knew I would be adding my own functions in, the templating system works out great. I created a new Template Group called “addons”. You can call it anything you wish, as long you refer to it correctly. In the “addons” template group, I edited the index and put in the following code:

<?php

// Get the first # words in a given string

// $content = the string to parse.

// $count = the number of words to return. defaults to 10.

function getfirstwords($content,$count=”10″)

{

$array = explode(” “,$content);

for($x=0;$x<=$count-1;$x++) {

$shortdesc.=$array[$x].” “;

}

$shortdesc=rtrim($shortdesc).”…”;

return $shortdesc;

}

// Recent Comments

// $count = How many comments do you wish to retrieve? Defaults to the last 5 comments made.

function recent_comments($count=”5″)

{

$results=”";

$dbusername=”dbusername”;

$dbpassword=”dbpassword”;

$dbname=”expressionengine”;

$db_yoshi=mysql_connect(“localhost”,$dbusername,$dbpassword);

mysql_select_db($dbname,$db_yoshi);

$recent=mysql_query(“select entry_id,name,comment from exp_comments where weblog_id=1 order by comment_date desc limit 0,$count”,$db_yoshi) or die(mysql_error());

while (list($entry_id,$name,$comment)=mysql_fetch_row($recent))

{

list($title)=mysql_fetch_row(mysql_query(“select title from exp_weblog_titles where entry_id=$entry_id”));

$linkurl=”/index.php/weblog/comments/”.$entry_id.”/”;

$summary=getfirstwords($comment);

$results.=”

“.$title.” – “.$name.” – “.$summary.”

“;

}

mysql_close($db_yoshi);

return $results;

}

?>

Please be sure that you check “YES” to “Allow PHP in this Template” in your preferences, or nothing will work, and you will get a borken page. Please not that you must set this in ALL your pages that will display this script. i.e. odds are you’ll need to set it on your index. Now that the function was saved, you need to embed the template into the page that will be displaying it. In this case, the front page, so you will add to the top of the weblog/index template this statement:

embed=addons/index

Again, please make sure the weblog/index template preferences are set to parse PHP. Otherwise you will get a broken page.

Now, in your weblog/index template, figure out where you want to have the most Recent Comments displayed, and put this line in:

<?php echo recent_comments(); ?>

NOTES: (things you need to know)

This function assumes that you’re doing this for the “first” weblog. If you want to display it for a different weblog, then you must provide the weblog id in line 24. You’ll see where it says:

weblog_id=1

change that to the appropriate weblog id. You can find that by going into your CP and going to Admin => SQL Manager => Manage Database Tables, then click on “Browse” for exp_weblogs (at the bottom).

You can change the formatting to how you like to have it on your weblog by changing line 30. Just change divs to whatever you would like.

Random Posts

 

10 Responses to “Recent Comments Add-on”

  1. Chris Curtis says:

    Very nicely done, Yoshi.  My one suggestion right now would be to remove the line numbers from your code since having them there makes doing a copy-paste really difficult for people.

    Other than that, great job!

  2. yoshi says:

    yeah I had thought the same thing! I’m just weird like that, I like to have an easy reference. I’ll just get rid of them now heheh

  3. Sue says:

    Another suggestion is to take your code, put it in a .txt file on the site, and refer people there. That way, you don’t have to worry about smart quotes, line numbers, etc.

  4. yoshi says:

    Yeah, i’m trying figure out the best way. must be the formatting i’m using when i post, i can’t put in the { signs when posting…

  5. yoshi says:

    I’ve changed the recent comments script a little bit. I’ll post the change in a few. :)

  6. Humblebub says:

    Hi Yoshi:  Probably to much coffee and not enough sleep thrashing through the new EE toy :) – but – I am unsure what changes I need to make in the function to make it the recent comments work here.

    Obviously the $db_username and $db_password needs to be changed, but what else.  I am rendering a blank page and am sure it is something I have missed.

  7. yoshi says:

    Hey Humblebob, check out the revised one here and download the revised_function.txt file to use as your function. you should be able to just change out the dbusername and password and put it into your template and get it going. Just make sure you are allowing PHP in that template and any other template that embeds that form.

  8. Humblebub says:

    Thanks Yoshi – still no joy.  I am still getting a blank page – http://paradise.islandmusings.net/index.php/addons/index

    May be time for me to walk away and think about it for a while as I am probably missing something obvious which will make be blush.

  9. yoshi says:

    Hmm, there’s gotta be an error in the code, like missing a semi-colon, or anything. Can you possibly e-mail me your code? or better, if you have aol instant messenger, message me at psychodaisy2002. :cowtiny:

  10. Humblebub says:

    It is on the way via e-mail.  I don’t have aol im.  Thanks again.