简体   繁体   English

php在smarty tpl中显示所有数据库记录

[英]php displaying all db records in smarty tpl

I want to create an array and assign it to smarty template to display all of my news. 我想创建一个数组并将其分配给smarty模板以显示所有新闻。 But the problem is it is only picking up the first row of data in the table, how can I fix this? 但是问题在于,它仅获取表中的第一行数据,如何解决此问题? It is only showing the first ever row and ther eis 4 rows in the database 它仅显示数据库中的第一行和第四行

I am using mysqli for db driver... 我正在使用mysqli作为数据库驱动程序...

$newsArray = array();
        $row = autoLoader::getLibrary('database')->query("SELECT * FROM `cms_news`")->fetch_array();
            $newsArray[] = $row;

        $this->smarty->assign('website_news', $newsArray);

Then I display it using this code in the tpl 然后我在tpl中使用此代码显示它

{section name=nr loop=$website_news} 
   {$website_news[nr].id} 
   {$website_news[nr].title} 
   {$website_news[nr].shortstory}<br /> 
{sectionelse} 
   <h1>No news found!</h1> 
{/section} 
$newsArray = autoLoader::getLibrary('database')->query("SELECT * FROM `cms_news`")->fetch_all(MYSQLI_BOTH);
$this->smarty->assign('website_news', $newsArray);

Your template seems to be ok. 您的模板似乎还可以。 The problem is with your date not being fully loaded as an array. 问题在于您的日期未完全作为数组加载。 This is a php (controller) side error. 这是php(控制器)方面的错误。 You can verify if your print_r($newsArray); 您可以验证您的print_r($newsArray); contains full data. 包含完整数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM