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() as $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 is your model, specifically your query.
Your SELECT is set to retrieve the following: 'id', 'Name', 'Password', 'Email' but in reality (according to your code), you only need Name.
You are creating an unnecessary array. As you may or may not know, $query->result() is a Codeigniter function that returns an array of objects. Therefore, you do not need to iterate over it and create another array. All you need to do is return those results, and let your controller do the iteration using the -> operator to obtain you object data.
With all that said, these are the errors I would deal with in your current model method. I used comments to explain:
public function sign_in()
{
if (isset($_POST)) { //POST info should be set in the controller, not in the model
$this->load->library('session'); //Why do you need this??
$Email = $this->input->post('Email'); ///POST info should be set in the controller, not in the model
$Password = $this->input->post('Password');//POST info should be set in the controller, not in the model
$this->db->select('id', 'Name', 'Password', 'Email'); // why do you require all these, if you are only returning the 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 already only be one account that matches?
$query = $this->db->get();
//the code below is iterating for no purpose.
//If the reason why youre doing this iteration is to obtain arrays rather than arrays of objects,
//then use $this->db->result_array() instead
//also, the conditional is not necessary as it will already return false (0) if none found.
if ($query->num_rows() > 0) {
$data = array();
foreach ($query->result() as $row) {
$data[] = array(
'Name' => $row->Name
);
}
return $data;
} else {
return false;
}
}
}
I would rewrite your code like 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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With