【问题标题】:How to prepare a mysql-query to loop in smarty section?如何准备一个 mysql-query 在 smarty 部分循环?
【发布时间】:2012-10-04 04:47:46
【问题描述】:

我正在尝试准备一个数组以在 Smarty {section} 循环中使用它。

我开发了一个简单的 PHP 脚本,它给出了我想要的结果:

$userlist = mysql_query("SELECT user_username FROM table_users");

while($name = mysql_fetch_assoc($userlist)){
 echo $name['user_username'].", ";
}

现在我不想在 php 中“回显”结果,而是将 while-part 移动到我已经创建的 tpl 文件中

$smarty->assign('users', $users);
$smarty->display('userlist.tpl');

现在在 .tpl 文件中,我有

{section name="userlist" loop="$users"} 
name: {$users[userlist].user_username} <br /> 
{/section}

现在我想知道,我需要如何准备我的 $users 数组以将其分配给 smarty 循环部分。我已经尝试了很多,但是当数组来自 MySQL 数据库时,它无法正常工作,...

有人可以帮忙吗? 最好的问候。

【问题讨论】:

  • 也许是在 PHP 中的部分。它将被合并。
  • 感谢@JohnConde 的提示

标签: php mysql loops smarty2


【解决方案1】:

试试

{foreach $users as $user}
name: {$user.user_username} <br /> 
{/foreach}

无论如何,正如@John Conde 所说,MySQL_ 函数已被弃用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-08
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    相关资源
    最近更新 更多