2012-08-12 66 views
0

我稱之爲PHP文件testfun.php獲取數據庫值

<?php 
$conn=mysql_connect("localhost","root","") or die("unabke to connect"); 
$db=mysql_select_db("smartyform",$conn) or die("databse error"); 

require 'Smarty/libs/Smarty.class.php'; 

$smarty = new Smarty; 

$sel=mysql_query("select * from form"); 
while($row=mysql_fetch_array($sel)) 
{ 
$id=$row[0]; 
$name=$row[1]; 
} 
$smarty->assign('id',$id); 
$smarty->assign('name',$name); 

$smarty->display('testfunction.tpl'); 
    ?> 

我已經TPL文件名爲testfunction.tpl

<body> 

<ul> 
{$id} 
: 
{$name} 
</ul> 

</body> 
</html> 

當我運行testfun.php我得到這樣的輸出:

16 : dg 

但我想輸出像:

1:d 
d:g 

我該怎麼辦?

+0

可能重複( http://stackoverflow.com/questions/11920553/getting-value-in-smarty-template) – rodneyrehm 2012-08-12 07:58:11

回答

0

您必須將數據作爲數組傳遞給smarty。所以,在PHP中,你應該做這樣的事情:

while($row=mysql_fetch_array($sel)) 
{ 
    $data[] = array(
    "id" => $row[0], 
    "name" => $row[1] 
); 
} 

$smarty->assign("data", $data); 

然後,在你的Smarty模板,你必須使用foreach列出您的項目:

{foreach $data as $item} 
    {$item.id}:{$item.name} 
{/foreach} 
[在智者的模板中獲得價值]的