php - How to send data from database from model to controller codeigneter -
model
public function sign_in() { if (isset($_post)) { $this->load->library('session'); $email = $this->input->post('email'); $password = $this->input->post('password'); $this->db->select('id', 'name', 'password', 'email'); $this->db->from('users'); $this->db->where('email', $email); $this->db->where('password', md5($password)); $this->db->limit(1); $query = $this->db->get(); if ($query->num_rows() > 0) { $data = array(); foreach ($query->result() $row) { $data[] = array( 'name' => $row->name ); } return $data; } else { return false; } } }
//controller
public function index() { $this->load->model('login'); $data = $this->login->sign_in(); if ($data) { $this->load->view('index', $data); echo 'success'; print_r($data); } else { $this->load->view('index'); } }
// result
the problem here model, query.
your
select
set retrieve following:'id', 'name', 'password', 'email'
in reality (according code), needname
.you creating unnecessary array. may or may not know,
$query->result()
codeigniter function returns array of objects. therefore, not need iterate on , create array. need return results, , let controller iteration using->
operator obtain object data.
with said, these errors deal in current model method. used comments explain:
public function sign_in() { if (isset($_post)) { //post info should set in controller, not in model $this->load->library('session'); //why need this?? $email = $this->input->post('email'); ///post info should set in controller, not in model $password = $this->input->post('password');//post info should set in controller, not in model $this->db->select('id', 'name', 'password', 'email'); // why require these, if returning name ? $this->db->from('users'); $this->db->where('email', $email); $this->db->where('password', md5($password)); $this->db->limit(1); // why limit, if there should 1 account matches? $query = $this->db->get(); //the code below iterating no purpose. //if reason why youre doing iteration obtain arrays rather arrays of objects, //then use $this->db->result_array() instead //also, conditional not necessary return false (0) if none found. if ($query->num_rows() > 0) { $data = array(); foreach ($query->result() $row) { $data[] = array( 'name' => $row->name ); } return $data; } else { return false; } } }
i rewrite code this:
model:
public function sign_in($email, $password) { $this->db->select('name'); $this->db->from('users'); $this->db->where('email', $email); $this->db->where('password', md5($password)); $query = $this->db->get(); return $query->row(); } }
controller:
public function index() { $data = array(); if(isset($_post)){ $this->load->model('login'); $email = $this->input->post('email'); $password = $this->input->post('password'); $result = $this->login->sign_in($email, $password); if ($result) { $data["user_info"] = $result; } } $this->load->view('index', $data); }
view:
print_r($user_info); //or echo $user_info->name;
Comments
Post a Comment