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>