未定义的索引codeigniter

时间:2014-05-20 05:48:27

标签: codeigniter

我在codeigniter

中遇到了结果数组的问题

模型是

 function ReadMessages($UID)
 {
    $query = $this->db->get('ls_messages3');
    return $query->result_array();
 }

控制器:

function messages()
{
  $data['user_info']['UnreadMessagesCount']=$this->pages_model->MessagesCount($this->session->userdata('user_id'));
  $this->load->model("articles_model");
  $this->load->model("login_model");
  $this->load->model("cabinet_model");
  $this->load->model("user_model");
  $this->load->library("form_validation");
  $data['pages'] = $this->pages_model->get_pages();
  $data['pages_info'] = $this->login_model->get_info("pm");
  $data['categories'] = $this->pages_model->get_cat();
  $data['user'] = $this->session->userdata('user');
  $data['user_info']['user_id']=$this->session->userdata('user_id');
  $data['user_info']['user_category']=$this->session->userdata('user_cat');
  $data['user_info']['avatar'] = $this->session->userdata('avatar');
  $data['error']='';
  $name="pm";
  $data['single_user'] = $this->login_model->user_info($data['user']);
  $data['mess'] = $this->user_model->ReadMessages($data['user_info']['user_id']);
  $this->template->page($data,$name);

}

并查看:

<div id="body-in">
             <div id="profile_content">
                <?=var_dump($mess);?>
                <?=$mess['id'];?>

             </div>         
           </div>

结果数组位于$ data [&#39; mess&#39;]变量中。在视图中我使用var_dump函数到这个变量,我得到正常答案,如下所示:

array(1) { [0]=> array(7) { ["id"]=> string(1) "1" ["user_id"]=> string(1) "1" ["lstext"]=> string(95) " Hello! This is the Message! " ["isread"]=> string(5) "FALSE" ["date"]=> string(10) "2014-05-18" ["touser_id"]=> string(1) "2" ["time"]=> string(5) "16:06" } }

但是当我试图从这个数组中获取单个值时,我有错误&#34;消息:未定义索引&#34;在这个页面上

1 个答案:

答案 0 :(得分:0)

将模型代码更改为

 function ReadMessages()
 {
    $query = $this->db->get('ls_messages3');
    return $query->row_array();
 }

并在控制器中

$this->user_model->ReadMessages($data['user_info']['user_id']);

$this->user_model->ReadMessages();

在视图中使用<?php echo $mess[0]['id']; ?>