将while循环放入字符串中

时间:2012-03-22 21:51:47

标签: php

我有这个代码创建一个字符串,然后在底部回显循环。问题是只显示循环的最后一个变量。我希望每次迭代都显示出来。

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 个答案:

答案 0 :(得分:1)

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>";

此时你在每个循环中重新定义$strOutput,所以只有最后一个存活到最后。

而是使用.=

 $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>";

答案 1 :(得分:0)

所以......只需在结束echo循环的}之前移动while ...

答案 2 :(得分:0)

我认为这里的真正问题是,每次为其分配新内容时,您都会覆盖$ strOutput变量。您应该在目前使用$strOutput .=的几乎所有地方使用$strOutput =