主类别数组用子类别表示

时间:2011-10-02 17:00:11

标签: php arrays foreach while-loop template-engine

include "includes/config.php";
class template{
    var $page;
    var $built;

    public function start(){
        $this->page = "{TITLE}<BR />{S_TITLE}<BR />";
    }

    public function reset(){
        $this->built = $this->page;
    }

    public function set($data){
        $this->reset();
        foreach($data as $key => $val){
            $this->built = str_replace('{'.$key.'}', $val, $this->built);
        }
        echo $this->built;
    }

    public function show(){
        echo $this->built;
    }
}

$temp = new template();

$temp->start();

$query_cat = "SELECT * FROM category";
$cat = mysql_query($query_cat, $config) or die(mysql_error());
while($row_cat = mysql_fetch_assoc($cat)){

$query_subcat = "SELECT * FROM subcat WHERE cid={$row_cat['id']}";
$subcat = mysql_query($query_forum, $config) or die(mysql_error());
while($row_subcat = mysql_fetch_assoc($subcat)){

$temp->set(array('TITLE' => $row_cat['title'], 
                'S_TITLE' => $row_subcat['title']));

}
}

$temp->show();

好吧,我的问题是主要类别是像

这样的子类别一次又一次地重复自我

CAT1 subcat1.1 CAT1 subcat1.2 CAT2 subcat2.1 CAT2 subcat2.2

我想要它 CAT1 subcat1.1 subcat1.2 CAT2 subcat2.1 subcat2.2

有人可以告诉我我错在哪里吗?

0 个答案:

没有答案