Topic: latest comments: an issue
Why my latest comments script, in CN 1.5, shows 5 times the same comment, but not the last 5 comments? You con see it here, to the right side, downward http://www.pasqualemarinelli.com/indexb.php
This is my script:
<?php
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Configuration
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
//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://www.pasqualemarinelli.com/indexb.php';
//more help on the format: http://www.php.net/manual/en/function.date.php
$date_format = 'd/m/Y';
//In your template you can use: {url}, {name}, {date}, {comment}, {ip}, {mail}, {newsid}, {comid}
$template = '<font face="Arial">[b]{name} [{date}] scrive:[/b] {comment} [url={][u]Vai al post[/u][/url]
</font>';
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
END - END - END - END - END - END / don't edit below /
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
$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("b/cdata/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 = $template;
$output = str_replace("{url}", $site_url."?subaction=showcomments&id={newsid}#{comid}", $output);
$output = str_replace("{name}", $comment['name'], $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;
}
?>