在codeigniter视图中使用foreach生成选择选项

时间:2014-09-05 10:30:14

标签: php codeigniter

我使用codeigniter生成一些html选项,但我只得到一个结果,第一个结果来自表。

这是我的控制器

public function edit_event($id = 0){
    $id = $this->uri->segment(3);
    $current_id = $this->ion_auth->get_user_id();
    $data['data'] = $this->as->the_event_edit($id);
    $data['groups'] = $this->as->the_groups_list($current_id);
    $this->load->view('editevent',$data);
    }

这是我的模特

public function the_groups_list($current_id){
    $query = $this->db->get_where('all_groups', array('group_owner' => $current_id));
    foreach ($query->result() as $row)
    {
    $data = array(
               'group_title' => $row->group_title,
               'group_name' => $row->group_name,
               'group_owner' => $row->group_owner
          );  
          return $data;
    }
    }

这是另一个模型

public function as_event_edit($id){
    $query = $this->db->get_where('all_scheduled_messages', array('id' => $id));
    foreach ($query->result() as $row)
    {
    $data = array(
               'as_title' => $row->as_title,
               'as_event_name' => $row->as_event_name,
               'as_owner' => $row->as_owner,
               'as_type' => $row->as_type,
               'as_target_dataset' => $row->as_target_dataset,
               'as_timestamp' => $row->as_timestamp,
               'as_time' => $row->as_time,
               'as_day' => $row->as_day
          );  
          return $data;
    }
    }

然后我在视图中使用$groups['group_title']并且只显示第一个组标题,即使我有来自其他行的4个组标题。

我如何返回并传递一个数组,然后我可以将其传递给视图,以便使用foreach迭代并显示组标题?。

2 个答案:

答案 0 :(得分:0)

将模型从$data =更改为$data[] =以插入每一行而不只是一行。

在你的视图循环中,如下所示:

foreach ($data as $group) {
    echo "Title" . $groups['group_title'];
}

RTM:Adding Dynamic Data to the View

在控制器中:将$ data ['data']重命名为$ data ['event']

$data['event'] = $this->as->the_event_edit($id);
$data['groups'] = $this->as->the_groups_list($current_id);
$this->load->view('editevent',$data);

在您看来:

foreach ($event as $items) {
    var_dump($items);
}

foreach ($groups as $group) {
    var_dump($group);
}

答案 1 :(得分:0)

在你的模型中,你不是在创建一个多维数组。您需要使用计数器添加密钥:

$data = array();
$i=0;
foreach ($query->result() as $row){

  $data[$i]['as_title'] = $row->as_title;
  $data[$i]['as_event_name'] = $row->as_event_name;
  $data[$i]['as_owner'] = $row->as_owner;
  $data[$i]['as_type'] = $row->as_type;
  $data[$i]['as_target_dataset'] = $row->as_target_dataset;
  $data[$i]['as_timestamp'] = $row->as_timestamp;
  $data[$i]['as_time'] = $row->as_time;
  $data[$i]['as_day'] = $row->as_day;

  $i++;

);  
return $data;

或使用传入数组的键

$data = array();
foreach ($query->result() as $id => $row){

  $data[$id]['as_title'] = $row->as_title;
  $data[$id]['as_event_name'] = $row->as_event_name;
  $data[$id]['as_owner'] = $row->as_owner;
  $data[$id]['as_type'] = $row->as_type;
  $data[$id]['as_target_dataset'] = $row->as_target_dataset;
  $data[$id]['as_timestamp'] = $row->as_timestamp;
  $data[$id]['as_time'] = $row->as_time;
  $data[$id]['as_day'] = $row->as_day;

);  
return $data;