2013-07-27 18 views
0

請幫助我!如何將數據從表格傳輸到smarty?PHP + Smarty + MySQL

功能:

public function getBanLog() { 
    global $mysqli; 
    $result = $query = $mysqli->query("SELECT * FROM `bans`") or die($mysqli->error); 
    $rows = array(); 
    while($row = $result->fetch_array(MYSQLI_ASSOC)) { 
     $rows[] = $row; 
    } 
} 

的index.php:

$user = new UserInfo(); 
$smarty = new Smarty(); 

$smarty->assign("userInfo", $user); 
$smarty->assign('ban', $user->getBanLog()); 
$smarty->display('template/ban.tpl'); 

ban.tpl:

{foreach from=$ban item=row} 
    <td>{$row.id}</td> 
    <td>{$row.banned}</td> 
    <td>{$row.admin}</td> 
    <td>{$row.reason}</td> 
{/foreach} 
+1

代碼看起來不錯什麼問題呢? –

+0

這裏唯一值得懷疑的是查詢中缺少'WHERE'子句,它似乎會爲所有用戶返回所有禁令,而不僅僅是UserInfo()所隱含的用戶。 –

+0

@dianuj'getBanLog()'不返回任何內容,並且'$ result = $ query = $ mysqli-> query' – bansi

回答

4

getBanLog()函數返回什麼,需要添加一個return語句。另外$result = $query = $mysqli->..是不正確的。

試試這個

public function getBanLog() { 
    global $mysqli; 
    $result = $mysqli->query("SELECT * FROM `bans`") or die($mysqli->error); 
    $rows = array(); 
    while($row = $result->fetch_array(MYSQLI_ASSOC)) { 
     $rows[] = $row; 
    } 
    return $rows; 
}