英文:
How to combine 'foreach' and value in codeigniter?
问题
我想使用CodeIgniter 3从数据库在我的网站上查看数据。在这个网站上查看我的数据有两种方法。第一种方法是不使用循环,我使用了以下代码:
<?php $s = $sched_stud->row();?>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<th scope="row">Full Name</th>
<td><?php echo $s->name;?></td>
</tr>
<tr>
<th scope="row">Level Enrolled</th>
<td><?php echo $s->level;?></td>
</tr>
</tbody>
</table>
第二种方法是使用foreach,这是我的代码:
<tbody>
<?php
$a = 1;
foreach ($sched_stud as $key) {
?>
<tr>
<th scope="row"><?php echo $a; $a++; ?>.</th>
<td>Tue, 8 Jan 2019</td>
<td>17.00 - 18.30</td>
<td><?php echo $key->room;?></td>
<td>Mrs. Adinda</td>
<td>Upcoming</td>
</tr>
<?php } ?>
</tbody>
但是出现了错误,提示:
Undefined property: mysqli::$room
我该如何修复这个错误?
英文:
I want to view data from database in my website using CodeIgniter 3. There are 2 types of ways to view my data in this website. First is without looping, I used this code :
<?php $s=$sched_stud->row();?>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<th scope="row">Full Name</th>
<td><?php echo $s->name;?></td>
</tr>
<tr>
<th scope="row">Level Enrolled</th>
<td><?php echo $s->level;?></td>
</tr>
</tbody>
</table>
and the second one is using foreach, this is my code :
<tbody>
<?php
$a=1;
foreach ($sched_stud as $key) { ?>
<tr>
<th scope="row"><?php echo $a; $a++; ?>.</th>
<td>Tue, 8 Jan 2019</td>
<td>17.00 - 18.30</td>
<td><?php echo $key->room;?></td>
<td>Mrs. Adinda</td>
<td>Upcoming</td>
</tr>
<?php } ?>
</tbody>
but there are errors saying :
Undefined property: mysqli::$room
how can i fix this?
答案1
得分: 0
在第一种方式中,您只获取一条记录,使用 row()
方法。
所以在您的第二种方式中,您需要使用 result()
方法来获取多条记录。
<tbody>
<?php
$a = 1;
foreach ($sched_stud->result() as $key) { ?>
<tr>
<th scope="row"><?php echo $a; $a++; ?>.</th>
<td>Tue, 8 Jan 2019</td>
<td>17.00 - 18.30</td>
<td><?php echo $key->room; ?></td>
<td>Mrs. Adinda</td>
<td>Upcoming</td>
</tr>
<?php } ?>
</tbody>
英文:
In first way you are getting only one record with row()
SO in your second way for loop you need result()
to get multiple records
<tbody>
<?php
$a=1;
foreach ($sched_stud->result() as $key) { ?>
<tr>
<th scope="row"><?php echo $a; $a++; ?>.</th>
<td>Tue, 8 Jan 2019</td>
<td>17.00 - 18.30</td>
<td><?php echo $key->room;?></td>
<td>Mrs. Adinda</td>
<td>Upcoming</td>
</tr>
<?php } ?>
</tbody>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论