2013-02-16 131 views
0

我有smarty的問題,實際上我無法從數據庫收集循環數據。Smarty foreach循環

這是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'])); 

而且finaly這是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} 

所有我能看到是 Main site output of foreach

因此,foreach的輸出是完全錯誤的 有人可以幫助我解決這個問題嗎?

+0

使用不同的變量爲'項目'。 '{foreach from = $ reputation item = rep}'並在循環中作爲'$ rep.action'訪問。 – 2013-02-16 12:48:39

+0

您是否通過Smarty模板中的某個地方的{debug}檢查了「信譽」的價值? – Niko 2013-02-16 12:49:05

+0

@Niko是的,我檢查,一切都加載到tpl,因爲它應該在foreach中的問題 – 2013-02-16 12:54:47

回答

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代碼,你應該使用不同的變量名稱爲當前項目(由邁克爾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}