2012-08-12 70 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} 
[在智者的模板中获得价值]的