drummer392
drummer392

Reputation: 473

Put while loop into a string

I have this code that makes a string and then echos the loop out at the bottom. The problem is only the last variable that was looped through will be displayed. I want every iteration displayed.

elseif ($row['type'] == 7) {
    $blog_table = $row['blog_table'];
    $getblog = mysql_query("SELECT * FROM `$blog_table`");

    while ($blog = mysql_fetch_assoc($getblog)) 
    {
        $rowID = $blog['postID'];
        $matches = mysql_query("SELECT * FROM content WHERE `id` = '$rowID' ORDER BY `id` DESC");
        while ($post = mysql_fetch_assoc($matches)) 
        {
            if (mysql_num_rows($matches) > 0)
            {
            $strOutput = "<div class=\"controlPanel\"><a onClick=\"edit_{$post['id']}();return false\"><div class=\"controlPanel_edit\"></div>&nbsp;</a></div><p class=blogDate>{$post['date']}</p><span class=h1>{$post['title']}<p></p></span><p></p>{$post['maintext']}<p></p>";
            $postID = $post['id'];
            $getcomments = mysql_query("SELECT * FROM content WHERE `postID` = '$postID' ORDER BY `id` DESC");
            $num_comments = mysql_num_rows($getcomments); 
            $strOutput = $strOutput."<p class=\"blogPublisher\">Posted by {$post['publisher']}  |  Comments ({$num_comments})</p>";

            $strOutput = $strOutput."<div class=\"heriyah_blog_comments_content\">";
            while ($comments = mysql_fetch_assoc($getcomments)) 
            {
            $strOutput = $strOutput."<span class=\"blogCommentPublisher\"><b>{$comments['publisher']}</b>  |  {$comments['date']}<a onClick=delete_{$row['id']}();return false><img src=admin/system/images/delete.jpg class=click_btn></a></span><p></p>{$comments['maintext']}<p></p>";
            }
            $strOutput = $strOutput."</div>";
            $strOutput = $strOutput."<a class=click_btn onClick=comment_{$post['id']}();return false>Post a Comment</a><hr></hr>";
            }
            $strOutput = $strOutput;
        }
    }
    echo "$('#{$row['div']}.heriyah').append('<div class=\"heriyah_sortable\" id=\"sort_{$row['id']}\"><div class=\"controlPanel\"><a onClick=\"blog_{$row['blogID']}();return false\"><div class=\"controlPanel_settings\"></div></a><a onClick=\"delete_{$row['id']}();return false\"><div class=\"controlPanel_delete\"></div></a><div class=\"controlPanel_move\"></div></div><p>&nbsp;</p>{$strOutput}</div>');\n";
}

Upvotes: 1

Views: 2395

Answers (3)

Ryan Kempt
Ryan Kempt

Reputation: 4209

I think the real problem here is that you are overwriting your $strOutput variable every time you assign something new to it. You should be using $strOutput .= in almost every place you currently have been using $strOutput =.

Upvotes: 0

ahmetunal
ahmetunal

Reputation: 3960

if (mysql_num_rows($matches) > 0)
        {
            $strOutput = "<div class=\"controlPanel\"><a onClick=\"edit_{$post['id']}();return false\"><div class=\"controlPanel_edit\"></div>&nbsp;</a></div><p class=blogDate>{$post['date']}</p><span class=h1>{$post['title']}<p></p></span><p></p>{$post['maintext']}<p></p>";

at this point you redefine $strOutput at each loop, so only the last one survives to the end.

Instead use .=

 $strOutput .= "<div class=\"controlPanel\"><a onClick=\"edit_{$post['id']}();return false\"><div class=\"controlPanel_edit\"></div>&nbsp;</a></div><p class=blogDate>{$post['date']}</p><span class=h1>{$post['title']}<p></p></span><p></p>{$post['maintext']}<p></p>";

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

So... Just move the echo in before the } that ends the while loop...

Upvotes: 0

Related Questions