显示来自DB的数据,聪明

时间:2016-07-01 09:20:17

标签: php mysql smarty

im Smarty newbee。试图从数据库中显示一些数据,但浏览器页面中没有显示任何数据。我试图在php文件中显示查询,一切正常,但我不能在Smarty中显示。我的代码在这里: New.php

<?php
include 'Smarty/libs/Smarty.class.php';
$link = mysqli_connect ('localhost','xxxx','xxxx','xxxx');

$sql = "SELECT * FROM `s_news`";
$result = mysqli_query($link,$sql);

while($row = mysqli_fetch_assoc($result)) {
$data[] = $row; # $data is the array created for use in the Smarty template. 
$smarty->assign('data', $data); 
$smarty->display('text.tpl'); 
}
?>

Text.tpl文件:

{foreach from=$cat1 item=item}
<table cellpadding="2" cellspacing="0"> 
<tr> 
<td>Username:</td> 
</tr> 
{foreach from=$data item=item key=key} 
<tr> 
<td>{$item.date}</td> 
<tr> 
{/foreach} 
</table> 
{/foreach}

谢谢!

1 个答案:

答案 0 :(得分:1)

您正在分配并尝试在while循环中显示模板。在收集完所有数据后,在外面进行

while($row = mysqli_fetch_assoc($result)){
$data[] = $row; # $data is the array created for use in the Smarty template. 
}
$smarty->assign('data', $data); 
$smarty->display('text.tpl');