从智能模板中获取价值 [英] Getting value in smarty template
本文介绍了从智能模板中获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有名为testfun.php
的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');
?>
我有名为testfunction.tpl
的TPL文件。我在此文件中获得输出
<body>
<ul>
{$id}
:
{$name}
</ul>
</body>
</html>
当我运行testfun.php
时,我得到以下输出:
16 : dg
但我希望输出如下:
1:d
d:g
我应该做什么?
推荐答案
将数据存储在数组中。因此您必须在您的TPL中执行for循环。
在您的第三方物流中是这样的:
<ul>
{foreach from=$your_data item=item_value key=key}
<li> { $key } : {$item_value}</li>
{/foreach}
</ul>
在您的php中,将其设置为:
require 'Smarty/libs/Smarty.class.php';
$smarty = new Smarty;
$your_row= array();
$sel=mysql_query("select * from form");
while($row=mysql_fetch_array($sel))
{
$your_row[]= $row;
}
$smarty->assign('your_data',$your_row);
$smarty->display('testfunction.tpl');
?>
有关更详细的信息,请查看此页面:
http://www.smarty.net/docsv2/en/language.function.foreach
希望这对你有帮助。
这篇关于从智能模板中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文