while循环不会增加

时间:2011-04-11 18:24:04

标签: php arrays for-loop while-loop

有一个奇怪的问题我的循环没有像显示第一个类别那样递增而不是像这样的其余部分:

class temp 
{
var $file;

function new_list($forum_list)
{
    foreach($forum_list as $key => $value)
    {
        $this->file = str_replace('{'.$key.'}', $value, $this->file);
    }
    return $this->file;
}

function display()
{
    echo $this->file;
}
}

 $temp = new temp();

// mysql query

while ($row_cat = mysql_fetch_array($cat)){
    $temp->new_list(array(
        'CAT_TITLE' => $row_cat['title'],
        'CAT_DESCRIPTION' => $row_cat['description']
    ));

    $cid = $row_cat['id'];

    // mysql query

    while ($row_forum = mysql_fetch_array($forum)){
        $temp->new_list(array(
            'FORUM_TITLE' => $row_forum['title'],
            'FORUM_DESCRIPTION' => $row_forum['description']
        ));
    }
}
$temp->display();

现在我已经制作了一个模板引擎,并且它像往常一样使用数组,但只显示其描述和子类别及其描述的类别只有一次这是第一个条目,为什么这样我做错了什么?

编辑:我已经添加了它捕获文件的模板引擎代码并使用file_get_contents方法,然后将键转换为值,当我使用它时没有引擎它工作正常,但是引擎只显示第一列。

1 个答案:

答案 0 :(得分:0)

尝试简化它以找到错误。我没有看到您发布的内容存在问题,但我怀疑您尚未发布的代码逻辑中存在错误。试试这个:

<-- mysql query -->

while ($row_cat = mysql_fetch_array($cat)){
echo $row_cat['title']."<br/>";

$cid = $row_cat['id'];

<-- mysql query -->

while ($row_forum = mysql_fetch_array($forum)){
"-".$row_forum['title']."<br/>";
}
}

更新:

是的:这是你的问题:$this->file = ...每次迭代都会覆盖$ file。你需要连接结果(将字符串与。一起添加。),如下所示:

$this->file = $this->file . str_replace('{'.$key.'}', $value, $this->file);
相关问题