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;
    }
?>

Re: latest comments: an issue

In the previous thread I asked if you were using the second script for PHP 5.
You said you were but you have used the first one.
Use this one.
http://cutephp.com/forum/index.php?s=&...ost&p=49288
Modify the code to suit your server.

<?php

//set your absolute path to cutenews here
$mPath = "home*****public_htmlcutenews";
$reverse = false; //false = newest comments first.
//number of latest comments to show
if(!isset($comments_number)) $comments_number = 4;
//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';
//can use: {url}, {name}, {date}, {comment}, {ip}, {mail}, {newsid}, {comid}, {title}
$ctemplate = ' {date}  by {name} - {comment}
';

$latest_comments = array();

$all_comments = file("$mPath/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)
{
$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/cdata/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/cdata/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);
?>

3 (edited by 2012-11-11 13:36:13)

Re: latest comments: an issue

Yes, now it shows the last 4 comments, but it works just about the post belonging to the categories which comments are shown (in my case: category number 9). Furthermore, it let me show in the main inlcude (to the center) only the posts belong to the category number 9, and not all active news. Why this? Look here http://www.pasqualemarinelli.com/indexb.php

Re: latest comments: an issue

can you help me, please?

Re: latest comments: an issue

I have tested the code on my server and there are no problems with categories being displayed correctly in a main include. The latest comments code displays the last X comments regardless of category. So the problem may lie in your main include.

Re: latest comments: an issue

I have tested the code on my server and there are no problems with categories being displayed correctly in a main include. The latest comments code displays the last X comments regardless of category. So the problem may lie in your main include.


You've right! I fixed the issue just adding $category = "1,2,3,4,9" in my main include... thank you very much