1

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";
}
3
  • Move your echo into up into the while loop? Commented Mar 22, 2012 at 21:54
  • because you are redefining your $strOutput in every iteration Commented Mar 22, 2012 at 21:54
  • I would just move the echo inside the loop, but I wanted that <div> to surround the entire loop. Commented Mar 22, 2012 at 22:08

3 Answers 3

2
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>";
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, I learn something new everytime I come here. Really appreciate that. Who knew a period could have so much power.
0

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

Comments

0

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 =.

2 Comments

turns out I only needed it on the first $strOutput. I'm gonna apply this method to my practices.
Yes, however, where you are using $strOutput = $strOutput.'something'; you could just be using $strOutput .= 'something'; as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.