Codeigniter 从数据库填充表

发布于 2024-11-07 05:03:53 字数 534 浏览 0 评论 0原文

我想使用 但我无法让它工作。请检查我的代码:

controller
$data['query'] = $this->db->query('SELECT * FROM users');
$this->load->view('users_view',$data);

View
<?php foreach ($query->result_array() as $row)
{ ;?>
<tr>
<td><?php echo $row['usrID'];?></td>
<td><?php echo $row['usrName'];?></td>
<td><?php echo $row['usrPassword'];?></td>
</tr>
<?php } ?>
<?php endforeach; ?>

I want to populate all the data from my database to a table using <?php foreach; ?> but I can't get it to work. Please check my code:

controller
$data['query'] = $this->db->query('SELECT * FROM users');
$this->load->view('users_view',$data);

View
<?php foreach ($query->result_array() as $row)
{ ;?>
<tr>
<td><?php echo $row['usrID'];?></td>
<td><?php echo $row['usrName'];?></td>
<td><?php echo $row['usrPassword'];?></td>
</tr>
<?php } ?>
<?php endforeach; ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不即不离 2024-11-14 05:03:53

我会推荐视图中 foreach 的替代语法...

看起来您应该有一些语法错误。

<?php foreach ($query->result_array() as $row): ?>
    <tr>
        <td><?php echo $row['usrID'];?></td>
        <td><?php echo $row['usrName'];?></td>
        <td><?php echo $row['usrPassword'];?></td>
    </tr>
<?php endforeach; ?>

编辑 - 指出您的语法问题。

<?php foreach ($query->result_array() as $row)
{ ;?> //<---- semicolon?
<tr>
<td><?php echo $row['usrID'];?></td>
<td><?php echo $row['usrName'];?></td>
<td><?php echo $row['usrPassword'];?></td>
</tr>
<?php } ?>
<?php endforeach; ?> //<--- no need for this when using regular foreach

第一个分号就像编写这样的 foreach...

foreach ($array as $ele) { ; //<--this would throw an error just like it should above.
    //do stuff
}

除了问题点之外的意见

您应该在控制器中处理数据并简单地发送一个数组要在视图中显示的数据,而不是整个查询对象。

所以...

控制器

$query = $this->db->query('SELECT * FROM users');
$data['users'] = $query->result_array();
$this->load->view('users_view',$data);

I would reccommend the alternative syntax for your foreach in the view...

It looks like you should be having some syntax errors the way you have it.

<?php foreach ($query->result_array() as $row): ?>
    <tr>
        <td><?php echo $row['usrID'];?></td>
        <td><?php echo $row['usrName'];?></td>
        <td><?php echo $row['usrPassword'];?></td>
    </tr>
<?php endforeach; ?>

EDIT - to point out your syntax issues.

<?php foreach ($query->result_array() as $row)
{ ;?> //<---- semicolon?
<tr>
<td><?php echo $row['usrID'];?></td>
<td><?php echo $row['usrName'];?></td>
<td><?php echo $row['usrPassword'];?></td>
</tr>
<?php } ?>
<?php endforeach; ?> //<--- no need for this when using regular foreach

This first semicolon would be like writing a foreach like this...

foreach ($array as $ele) { ; //<--this would throw an error just like it should above.
    //do stuff
}

Opinion aside from the point of the question

You should be processing your data in the controller and simply sending an array of data to be displayed in the view, not the whole query object.

so...

Controller

$query = $this->db->query('SELECT * FROM users');
$data['users'] = $query->result_array();
$this->load->view('users_view',$data);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文