数据不会在codeigniter中显示

时间:2016-02-12 18:59:12

标签: php codeigniter

第一次使用php和代码点火器,我想显示的数据不会显示在文本字段中,我是否遗漏了代码?

查看

    Username: <input type="text" class="form-control" id="username" name="username" placeholder="" disabled="disable" value="<?php echo $result['username']; ?>" > <br>
    Password: <input type="text" class="form-control" id="password" name="password" placeholder=""disabled="disable" value="<?php echo $result['password']; ?>"><br>
    Rank : <input type="text" class="form-control" id="rank" name="rank" placeholder=""disabled="disable" value="<?php echo $result['title']; ?>"><br>

模型                

    Class profile_model extends CI_Model {
     public function my_data()
   {

    $username = $this->session->userdata('$username');
    $data = array();
    $this->db->select('*');
    $this->db->from('user');
     $this->db->where('username', $username);
    $query = $this->db->get();

   //this will return multiple rows or object of arrays
   //return $query->result();
   // you need to send only single row
    return $query->row();
  }


 }

控制器                          

    public function profile() {
    // in data array key name should be same which you will pass to view    

    $this->load->model('profile_model');
    $data['result'] = $this->profile_model->my_data();        
    $this->load->view('profile_view', $data);


}

}

Image

3 个答案:

答案 0 :(得分:1)

不需要在模型函数中使用$data=array(),因为$query->row();返回对象并使用以下函数而不是您在模型中编写的函数:

public function my_data()
{
$username = $this->session->userdata('$username');    
$this->db->select('*');
$this->db->from('user');
$this->db->where('username', $username);
$query = $this->db->get();
return $query->row();
}

并在视图中编写以下代码:

value="<?php echo $result->username; ?>"

答案 1 :(得分:0)

$query->row()返回一个对象,而不是一个数组。请在您的视图中尝试此操作:

value="<?php echo $result->username; ?>"

答案 2 :(得分:0)

尝试使用row_array()

 public function my_data()
  {
     $username = $this->session->userdata('$username');
     $data = array();
     $this->db->select('*');
     $this->db->from('user');
     $this->db->where('username', $username);
     $query = $this->db->get();
     return $query->row_array();
   }