Loop not outputting all data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
olspazzy
Forum Newbie
Posts: 5
Joined: Tue Mar 25, 2008 12:43 am

Loop not outputting all data

Post by olspazzy »

Does anyone see a reason why the "show news comments" section of this code is only displaying the newest comment for each post?

$comments_list_commentbit is being called from within the template for "news_newsbit_commentslink". Note that some classes and variables may not appear to be defined as this is only a portion of the script.

Code: Select all

if ($action == '')
{
    define('ISPRINTABLEPAGE', false);
    define('WILLTRUNCATE', true);
    define('ISRSS', false);
    $templatesused = 'news_newsbit, news_newsbit_commentslink, news_avatarbit, news_newsbit_readmorelink, comments_list_commentbit, comments_list_commentbit_removecomment';
    unp_cacheTemplates($templatesused);
    $getnews = $DB->query("SELECT * FROM `unp_news` ORDER BY `date` DESC LIMIT $newslimit");
    while ($news = $DB->fetch_array($getnews))
    {
        $catid = $news['catid'];
        $category = $categorycache["$catid"];
        $newsid = $news['newsid'];
        $subject = $news['subject'];
        $newstext = $news['news'];
        $poster = $news['poster'];
        $posterid = $news['posterid'];
        $date = $news['date'];
        $postdate = unp_date($dateformat, $date);
        $posttime = unp_date($timeformat, $date);
        $avatar = unp_checkAvatar($posterid);
        // begin Show News Comments
        $getcomments = $DB->query("SELECT * FROM `unp_comments` WHERE newsid='$newsid'");
        if ($DB->num_rows($getcomments) > 0)
        {
            while ($comments = $DB->fetch_array($getcomments))
            {
                // grab and fix up comments
                $c_id = $comments['id'];
                $c_title = htmlspecialchars(stripslashes($comments['title']));
                $c_name = htmlspecialchars(stripslashes($comments['name']));
                $c_email = htmlspecialchars(stripslashes($comments['email']));
                $c_date = unp_date($dateformat, $comments['date']);
                $c_time = unp_date($timeformat, $comments['date']);
                $c_text = nl2br(htmlspecialchars(stripslashes($comments['comments'])));
                $c_ipaddress = $comments['ipaddress'];
                $c_proxy = $comments['proxy'];
                $c_text = $n->unp_doSmilies($c_text);
                if ($isloggedin == 1)
                {
                    eval('$removecommentlink = "'.unp_printTemplate('comments_list_commentbit_removecomment').'";');
                }
                else
                {
                    $removecommentlink = '';
                }
                eval('$comments_list_commentbit = "'.unp_printTemplate('comments_list_commentbit').'";');
            }
        }
        else
        {
            $comments_list_commentbit = '';
        }
        // end Show News Comments
        if (!$avatar)
        {
            $useravatar = '';
        }
        else
        {
            eval('$useravatar = "'.unp_printTemplate('news_avatarbit').'";');
        }
        if ($commentsallowance == '1')
        {
            $comments = $news['comments'];
            eval('$commentsinfo = "'.unp_printTemplate('news_newsbit_commentslink').'";');
        }
        else
        {
            $commentsinfo = ' ';
        }
        $comments = $news['comments'];
        //$newstext = $n->unp_doNewsTrim($newstext); // Move to unp_doNewsFormat
        $newstext = $n->unp_doNewsFormat($newstext);
        $subject = $n->unp_doSubjectFormat($subject);
        // NewsBit
        eval('$news_newsbit = "'.unp_printTemplate('news_newsbit').'";');
        unp_echoTemplate($news_newsbit);
                
        // NewsBit
        echo "\n\n";
    }
    unset($news);
}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Loop not outputting all data

Post by Christopher »

Looks like it should be fetching all comments "WHERE newsid='$newsid'". Are there multiple comments? Are the DB classes being used correctly?
(#10850)
olspazzy
Forum Newbie
Posts: 5
Joined: Tue Mar 25, 2008 12:43 am

Re: Loop not outputting all data

Post by olspazzy »

Yes, the db classes are correct, yes, there are multiple posts, all of which have either zero or multiple comments. Here's the progress I've made:

Changing:

Code: Select all

eval('$comments_list_commentbit = "'.unp_printTemplate('comments_list_commentbit').'";');
to:

Code: Select all

eval('$comments_list_commentbit .= "'.unp_printTemplate('comments_list_commentbit').'";');
(note the ".") will output all the comments for each news post, but it will also include the comments of the post above it. Why might this be? This notice is also passed at the top of the script output so long as the first post has 1 or more comments:

Code: Select all

Notice: Undefined variable: comments_list_commentbit in /home/x/x/x.com/news/news.php(149) : eval()'d code on line 8
Post Reply