1 (edited by 2008-09-09 10:35:09)

Topic: [HACK] Most Recent Comments

Name: Latest Comments List
Author: BadDog
Updated by: Flexer
CuteNews Compatibility: 1.3.6 - * (no incompatibility reported yet)
Description: This will list the latest x number of comments made. This doesn't take post order in account, only the posting date of all comments.
Requirements: This original version has some problems when used with php5. So only use this when you have php 4 or lower.
Download: get the file here
Instructions:
Make a file with the following code in it. Don't forget to change the variables at the beginning of the file.
<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'><?php
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Update of Georgi Avramov's latest_comments.php by BadDog
Made more campatiable for CN 1.4 and also to include
title of the news for which the comment belongs to make it
usable in a sidebar.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
//absolute path to cutenews
$mPath = "C:/httpd/htdocs/cutenews";

//number of latest comments to show
if(!isset($comments_number)) $comments_number = 5;

//URL to where you include your news, example: http://site.com/news.php
$site_url = 'http://127.0.0.1/index.php';

//more help on the format: http://www.php.net/manual/en/function.date.php
$date_format = 'd M y';

//can use: {url}, {name}, {date}, {comment}, {ip}, {mail}, {newsid}, {comid}, {title}
$ctemplate = ' {title}  by {name}
';


    $latest_comments = array_fill(1, $comments_number, array('newsid'=>'0','comid'=>'0','name'=>'','mail'=>'','ip'=>'','comment'=>''));

    function check_latest_comment($comment_line, $newsid){
        global $latest_comments;
        $tmp_latest_comments = $latest_comments;
        $comment_arr = explode("|", $comment_line);
        foreach($latest_comments as $key=>$latest_comment){

            if($latest_comment['comid'] < $comment_arr[0]){

                $previous = $latest_comments[$key];
                foreach($latest_comments as $my_key=>$latest_comment_arr){
                    if($key < $my_key){
                     $current = $latest_comments[$my_key];
                     $latest_comments[$my_key] = $previous;
                     $previous = $current;
                    }
                }

                $latest_comments[$key] = array(
                                       'newsid'    => $newsid,
                                       'comid'    => $comment_arr[0],
                                       'name'    => $comment_arr[1],
                                       'mail'    => $comment_arr[2],
                                       'ip'    => $comment_arr[3],
                                       'comment'=> $comment_arr[4],
                          );
            }
        }
    }

   $all_comments = file("$mPath/data/comments.txt");
    foreach($all_comments as $comment_line)
    {

  $comment_line_arr = explode("|>|", $comment_line);
        $newsid = $comment_line_arr[0];
        $comment_arr = explode("||", $comment_line_arr[1]);
  foreach($comment_arr as $single_comment)
  {
            check_latest_comment($single_comment, $newsid);
  }
    }

    foreach($latest_comments as $comment){
  $output = $ctemplate;

          if ($comment['name']){

          $title = get_title($comment['newsid'], $mPath); //make sure path is passed to prevent open errors
 
     $output = str_replace("{url}", $site_url."?subaction=showcomments&id={newsid}#{comid}", $output);
     $output = str_replace("{name}", $comment['name'], $output);
     $output = str_replace("{title}", $title, $output);
     $output = str_replace("{mail}", $comment['mail'], $output);
     $output = str_replace("{ip}", $comment['ip'], $output);
     $output = str_replace("{comment}", $comment['comment'], $output);
     $output = str_replace("{date}", date($date_format, $comment['comid']), $output);
     $output = str_replace("{newsid}", $comment['newsid'], $output);
     $output = str_replace("{comid}", $comment['comid'], $output);
          echo $output . "\n";
          }
}

function get_title($id, $mPath){
   
       $all_news = file("$mPath/data/news.txt");
       foreach ($all_news as $news) {
       $news_arr = explode("|", $news);
    if ($news_arr[0] == "$id") {
       $title = $news_arr[2];
       }
       }
     $title = ereg_replace("<[^>]*>","",$title); // incase there is html tags
     $title = str_stop($title, 40); // keep the title length under 40 chars, edit to taste
     return $title;
}

function str_stop($string, $max_length){
     if (strlen($string) > $max_length){
         $string = substr($string, 0, $max_length);
         $pos = strrpos($string, " ");
     if ($pos === false) {
          return substr($string, 0, $max_length)."...";
        }
          return substr($string, 0, $pos)."...";
        }else{
          return $string;
        }
}
?></div>

Re: [HACK] Most Recent Comments

Search the forum.. there is a addon around.. its called latest comments or somethisng like that

Re: [HACK] Most Recent Comments

Jetski,Aug 29 2005, 03:26 AM wrote:

Search the forum.. there is a addon around.. its called latest comments or somethisng like that
[right][snapback]49163[/snapback][/right]


There is a latest_comments.php that is broken and in need of a overhaul for 1.4....I'll see if I can fix it, but if PHP regex is non C it will take me some figuring.

Re: [HACK] Most Recent Comments

OK, fixed latest_comments.php to work decent and fixed two flaws I found in it....NOW I just need to get a title returned so i can include it in the output as it is now it only shows the name. Soooooo I am thinking that I *could* open news.txt with the article ID that is saved in comments.txt to get a title for the comment and then trim it so longgggggggggg ones don't wrap to many lines since it will be in a sidebar......

Anyone want to spare me searching the code to find the php code to read the news.txt to match the article ID please!!! I just need a quick snippet/function to open it and extract the title.


Re: [HACK] Most Recent Comments


Something like this?

<?php
$all_news = file("news.txt");
foreach ($all_news as $news) {
     $news_arr = explode("|", $news);
     if ($news_arr[0] == "ID") {
     $title = $news_arr[2];
     }
}
?>


6 (edited by 2008-08-27 13:49:52)

Re: [HACK] Most Recent Comments

Name: Latest Comments List
Author: BadDog
Updated by: FUNimations
CuteNews Compatibility: 1.3.6 - * (no incompatibility reported yet)
Description: This will list the latest x number of comments made. This doesn't take post order in account, only the posting date of all comments.
Requirements: This is a rebuild to work with php5. This version might work in lower php versions aswell. Though this is unconfirmed.
Discussion Topic: the topic
Instructions:
Make a file with the following code in it.
<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'><?php

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Update of Georgi Avramov's latest_comments.php by BadDog
Made more campatiable for CN 1.4 and also to include
title of the news for which the comment belongs to make it
usable in a sidebar.

UPDATE AGAIN BY FUNimations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
//absolute path to cutenews
$mPath = "C:\wamp\www\cutenews";
$reverse = false; //false = newest comments first.
require_once( $mPath."/data/config.php" );
require_once( $mPath."/inc/functions.inc.php" );
//number of latest comments to show
if(!isset($comments_number)) $comments_number = 3;

//URL to where you include your news, example: http://site.com/news.php
$site_url = 'http://localhost/cutenews/';

//more help on the format: http://www.php.net/manual/en/function.date.php
$date_format = 'd M y';

//can use: {url}, {name}, {date}, {comment}, {ip}, {mail}, {newsid}, {comid}, {title}
$ctemplate = ' {title}  by {name} - {comment}
';


$latest_comments = array();

$all_comments = file("$mPath/data/comments.txt");

foreach($all_comments as $comment_line)
{
    $comment_line_arr = explode("|>|", $comment_line);
    $newsid = $comment_line_arr[0];
    $comment_arr = explode("||", $comment_line_arr[1]);
    foreach($comment_arr as $single_comment)
    {
        $single_comment_arr = explode("|",$single_comment);
        $latest_comments[$single_comment_arr[0]] = array_merge( array( $newsid ), $single_comment_arr );
       
    }
}
if($reverse)
ksort($latest_comments);
else
krsort($latest_comments);

$t=0;
foreach($latest_comments as $comment){
$output = $ctemplate;

if ($comment[2]){

$title = get_title($comment[0], $mPath); //make sure path is passed to prevent open errors
$category = get_cat($comment[0], $mPath); //make sure path is passed to prevent open errors

$output = str_replace("{url}", $site_url."?subaction=showcomments&id={newsid}&ucat={category}#{comid}", $output);
$output = str_replace("{name}", $comment[2], $output);
$output = str_replace("{title}", $title, $output);
$output = str_replace("{mail}", $comment[3], $output);
$output = str_replace("{ip}", $comment[4], $output);
$output = str_replace("{comment}", $comment[5], $output);
$output = str_replace("{date}", date($date_format, $comment[1]), $output);
$output = str_replace("{newsid}", $comment[0], $output);
$output = str_replace("{comid}", $comment[1], $output);
$output = str_replace("{category}", $category, $output);
$output = replace_comment("show", $output);

echo $output . "\n";
$t++;
}
if($t == $comments_number) break;
}

function get_title($id, $mPath){

$all_news = file("$mPath/data/news.txt");
foreach ($all_news as $news) {
$news_arr = explode("|", $news);
if ($news_arr[0] == "$id") {
$title = $news_arr[2];
}
}
$title = ereg_replace("<[^>]*>","",$title); // incase there is html tags
$title = str_stop($title, 40); // keep the title length under 40 chars, edit to taste
return $title;
}

function get_cat($id, $mPath){

$all_news = file("$mPath/data/news.txt");
foreach ($all_news as $news) {
$news_arr = explode("|", $news);
if ($news_arr[0] == "$id") {
return $news_arr[6];
}
}
return $title;
}

function str_stop($string, $max_length){
if (strlen($string) > $max_length){
$string = substr($string, 0, $max_length);
$pos = strrpos($string, " ");
if ($pos === false) {
return substr($string, 0, $max_length)."...";
}
return substr($string, 0, $pos)."...";
}else{
return $string;
}
}
unset($reverse);
?></div>