模板引擎的Mysql结果循环

时间:2014-06-27 06:30:09

标签: php template-engine

我想要做的是使用非常简单的模板引擎循环所有结果。 问题是脚本多次执行相同的结果.. 现在,数据库中有3个测试工作(Test_1,Test_2,Test_3), 所以基本上结果现在看起来像这样:

Test_3测试localhost

Test_3测试localhost

Test_3测试localhost

$result = $pDatabase->query($query) or die('Query failed: ' . mysql_error());
    while ($row = mysql_fetch_array($result)) {
        //Loop Template (row)
        $works_row = new Template("works_row.tpl"); 
            //Changing all from $row[] to {}
            $rows[]=$row;
            $works_row->set("category",$row['category']);
            $works_row->set("name",$row['name']);
            $works_row->set("link",$row['link']);
    }
    foreach ($rows as $row) {
    $works_templates[] = $works_row;
    }
    $works_contents = Template::merge($works_templates);
    mysql_free_result($result);
    //Content part that calls all works
    $works_list  = new Template("works_block.tpl"); 
    $works_list->set("works_rows", $works_contents);

我真的不明白为什么它不能正常工作

1 个答案:

答案 0 :(得分:1)

试试这个,它有效

    $result = $pDatabase->query($query) or die('Query failed: ' . mysql_error());
    while ($row = mysql_fetch_array($result)) {
        //Loop Template (row)
        $works_row = new Template("works_row.tpl"); 
            //Changing all from $row[] to {}
            $rows[]=$row;
            $works_row->set("category",$row['category']);
            $works_row->set("name",$row['name']);
            $works_row->set("link",$row['link']);
            $works_templates[] = $works_row;
    }

    $works_contents = Template::merge($works_templates);
    mysql_free_result($result);
    //Content part that calls all works
    $works_list  = new Template("works_block.tpl"); 
    $works_list->set("works_rows", $works_contents);