怎么使用smarty连接数据库,和输出
我在php页面这样写的代码:$query="SELECT*FROMmemberwhereuid=$uid";$result=mysql_query($query)ordi...
我在php页面这样写的代码:
$query = "SELECT * FROM member where uid=$uid";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['xingming'] . "<br/>";
} ;
这段代码可以换成smarty形式的吗 展开
$query = "SELECT * FROM member where uid=$uid";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['xingming'] . "<br/>";
} ;
这段代码可以换成smarty形式的吗 展开
1个回答
展开全部
要将型启陆php代码写成函数并且保存成function.xingmings.php放在plugins目录里
<?php
function smarty_function_xingmings($params,&$smarty){
$uid = $params['uid']; //旁空取参数 uid
$query = "SELECT * FROM member where uid=$uid";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$xingmingArr=array();
while($row = mysql_fetch_array($result))
{
$xingmingArr[] = $row['xingming'];
}
//将数组返回给模板
$smarty->assign($thexingmings,$xingmingArr);
}
?>
模板里的调用卜顷代码
{xingmings val="thexingmings" uid="26"}
{section name=i loop=$thexingmings}
姓名列表:{$thexingmings[i].xingming}<br>
{/section}
<?php
function smarty_function_xingmings($params,&$smarty){
$uid = $params['uid']; //旁空取参数 uid
$query = "SELECT * FROM member where uid=$uid";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$xingmingArr=array();
while($row = mysql_fetch_array($result))
{
$xingmingArr[] = $row['xingming'];
}
//将数组返回给模板
$smarty->assign($thexingmings,$xingmingArr);
}
?>
模板里的调用卜顷代码
{xingmings val="thexingmings" uid="26"}
{section name=i loop=$thexingmings}
姓名列表:{$thexingmings[i].xingming}<br>
{/section}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询