Getting value in smarty template

≡放荡痞女 提交于 2019-12-11 19:15:25

问题


I have php file called testfun.php. Which is getting values from the databse

<?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');
    ?>

I have tpl file called testfunction.tpl. I am getting output in this file

<body>

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

</body>
</html>

When I run the testfun.php I got this output:

16 : dg 

But I want output like:

1:d
d:g

What should I do ?


回答1:


Store your data in an array. So you have to do a for loop in your tpl.

It would be like this in your tpl:

 <ul>
    {foreach from=$your_data item=item_value key=key}
            <li> { $key } : {$item_value}</li>
    {/foreach}
</ul>

In your php make it this way:

     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');
 ?>

View this page for a more detailed information: http://www.smarty.net/docsv2/en/language.function.foreach

Hope that helps you.



来源:https://stackoverflow.com/questions/11920553/getting-value-in-smarty-template

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!