开发者

while loop does not increment

开发者 https://www.devze.com 2023-02-23 12:15 出处:网络
There is a weird problem my while loop is not 开发者_Python百科incrementing like its displaying the first category but not the rest like this:

There is a weird problem my while loop is not 开发者_Python百科incrementing like its displaying the first category but not the rest like this:

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();

now i have made a template engine and it uses array as usual but only displays the category its description and the subcategory and its description only once that is the first entry why is that so have i made anything wrong?

EDIT: I have added the template engine code it catches the file and use the file_get_contents method and then converts the keys to values and when i use it without the engine its works fine but with the engine it only shows the first column.


Try to simplify it to find the error. I dont see a problem with what youve posted but I suspect theres an error in the logic of the code you havent posted. Try this:

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

UPDATE:

Yup: Heres your problem: $this->file = ... is overwriting $file each iteration. You need to concat the result (add the strings together with a . ) something like this:

$this->file = $this->file . str_replace('{'.$key.'}', $value, $this->file);
0

精彩评论

暂无评论...
验证码 换一张
取 消