【问题标题】:Smarty foreach loopSmarty foreach 循环
【发布时间】:2013-02-16 12:44:45
【问题描述】:

我对 smarty 有问题,实际上我无法循环从数据库收集的数据。

这是getreputation功能

function getreputation($username)
{
global $rDB;

$rows = $rDB->select('
    SELECT *
    FROM account_reputation
    WHERE username=?
    ORDER BY id DESC
    ',
    $username
);

foreach($rows as $row)
{
    $reputation = array();
    $reputation['user'] = $row['username'];
    $reputation['reputation'] = $row['reputation'];
    $reputation['action'] = $row['reason'];
    $reputation['date'] = $row['date'];
}

return $reputation;
}

这是我如何将此函数分配给 tpl

$smarty->assign('reputation', getreputation($page['username']));

最后这是 foreach 循环

{foreach from=$reputation item=reputation}
<tr><td width="33%"><center>
{if $reputation.action == 1}Registering an account
{elseif $reputation.action == 2}Daily visit
{elseif $reputation.action == 3}Posting a comment
{elseif $reputation.action == 4}Your comment was voted up (each upvote)
{elseif $reputation.action == 5}Submitting a screenshot
{elseif $reputation.action == 6}Submitting a guide (approved)
{elseif $reputation.action == 7}Earning a <font color="brown">Copper</font> <a href="/?website-achievements">website achievement</a>
{elseif $reputation.action == 8}Earning a Silver <a href="/?website-achievements">website achievement</a>
{elseif $reputation.action == 9}Earning a <font color="gold">Gold</font> <a href="/?website-achievements">website achievement</a>
{else}Achievement not found
{/if}
</center></td><td width="33%">
<center><font color='green'>+{$reputation.reputation}
</font></center></td><td width="33%"><center>
{$reputation.date}
</center></td></tr>
{/foreach}

我能看到的只有

所以 foreach 的输出是完全错误的 有人可以帮我解决这个问题吗?

【问题讨论】:

  • item 使用不同的变量。 {foreach from=$reputation item=rep} 并在循环中以 $rep.action 的身份访问。
  • 您是否在 Smarty 模板中的某处通过 {debug} 检查了“声誉”的值?
  • @Niko 是的,我检查了,一切都按原样加载到 tpl,foreach 中的问题
  • @MichaelBerkowski 已经尝试过了,没有结果,仍然显示图像上的结果

标签: php mysql foreach smarty


【解决方案1】:

首先,您在此处发布的 PHP 函数将永远不会返回超过一行,因为您一直在覆盖 $reputation 的值。

function getreputation($username) {
    global $rDB;
    $rows = $rDB->select('SELECT * FROM account_reputation WHERE username=? ORDER BY id DESC', $username);

    // Initialize the array that will be filled with the single rows
    $reputation = array();
    foreach($rows as $row) {
        // Create a temporal variable with a different name
        $rep = array();
        $rep['user'] = $row['username'];
        $rep['reputation'] = $row['reputation'];
        $rep['action'] = $row['reason'];
        $rep['date'] = $row['date'];

        // Add this array to the other one
        $reputation[] = $rep;
    }

    return $reputation;
}

然后,在您的 Smarty 代码中,您应该为当前项目使用不同的变量名称(如 Michael Berkowski 建议的那样):

{foreach from=$reputation item=rep}
    <tr><td width="33%"><center>
    {if $rep.action == 1}Registering an account
    {elseif $rep.action == 2}Daily visit
    etc...
    {else}Achievement not found
    {/if}
    </center></td>

    <td width="33%"><center><font color='green'>+{$rep.reputation}
    </font></center></td>
    <td width="33%"><center>{$rep.date}</center></td>
    </tr>
{/foreach}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    相关资源
    最近更新 更多