这是我的代码
$this -> db -> select('*');
$this -> db -> from('events');
$this -> db -> where('user_id',$session_data['id']);
$query = $this -> db -> get();
$result = $query->result();
if($result)
{
$events_array = array();
foreach($result as $row)
{
$events_array = array(
'id' => $row->id,
'title' => $row->title,
'ev_img_name' => $row->ev_img_name,
);
$this->session->set_userdata('events',$events_array);
}
$session_data = $this->session->userdata('events');
$id = $this->session->userdata('id');
$data['id'] = $id;
$data['id'] = $session_data['id'];
$title = $this->session->userdata('title');
$data['title'] = $title;
$data['title'] = $session_data['title'];
$ev_img_name = $this->session->userdata('ev_img_name');
$data['ev_img_name'] = $ev_img_name;
$data['ev_img_name'] = $session_data['ev_img_name'];
它只获取表的第一行,但我需要获得具有相同user_id的所有行。我怎样才能做到这一点。提前谢谢。
答案 0 :(得分:0)
在foreach中,您将值重新分配给同一个数组。 尝试使用
$events_array = array();
foreach($result as $row)
{
$events_array[] = array(
'id' => $row->id,
'title' => $row->title,
'ev_img_name' => $row->ev_img_name,
);
}
$this->session->set_userdata('events',$events_array);
这样,您将获得$events_array
中返回结果的所有行。
现在在您的视图中,您可以执行此操作
<?php foreach($events_array as $event) { ?>
<div class="event">
Title: <?= $event['title'] ?>
Image: <br>
<img src="<?= $event['ev_img_name'] ?>">
</div>
<?php } ?>
答案 1 :(得分:0)
试用此代码, 您可以直接从codeigniter sql查询获取数据格式的数据(字段数),然后直接将该数组设置为session,如下所示,
__keys__