0

Help me please! How to transfer data from table to smarty?

Function:

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}
4

1 回答 1

4

Your getBanLog() function returns nothing, need to add a return statement. Also $result = $query = $mysqli->.. is not correct.

Try this

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;
}
于 2013-07-27T12:12:14.777 回答