PHP + Smarty + MySQL

2018/04/18 21:05
阅读数 40

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}
  • 1
    code looks fine whats the problem ? – M Khalid Junaid Jul 27 '13 at 12:10
  •  
    Only thing suspect here is the lack of a WHERE clause in your query, which appears to return all bans for all users, not just the user as implied by the UserInfo(). – Michael Berkowski Jul 27 '13 at 12:11
  •  
    @dianuj getBanLog() returns nothing, and $result = $query = $mysqli->query – bansi Jul 27 '13 at 12:14 

1 Answer

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;
}
展开阅读全文
打赏
0
0 收藏
分享
加载中
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部