Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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() 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

enter image description here

like image 594
Val Do Avatar asked Mar 01 '26 11:03

Val Do


1 Answers

The problem here is your model, specifically your query.

  1. Your SELECT is set to retrieve the following: 'id', 'Name', 'Password', 'Email' but in reality (according to your code), you only need Name.

  2. 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;
like image 162
CodeGodie Avatar answered Mar 04 '26 03:03

CodeGodie