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

+3


source to share


3 answers


The problem is with your model, specifically your request.

  • Yours is SELECT

    set to receive the following: 'id', 'Name', 'Password', 'Email'

    but actually (as per your code) you only need Name

    .

  • You are creating an unnecessary array. As you may or may not know, $query->result()

    this is a Codeigniter function that returns an array of objects. So you don't have to iterate over it and create another array. All you have to do is return those results and have your controller iterate with a statement ->

    to get the object's data.

With all that said, these are the errors I would run into 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;

      

+2


source


Try changing the if statement in your model to this:

if ($query->num_rows() > 0) {
    $data = array('name' = $query->result()->row->Name);
    return $data;
} else {
    ...
}

      



The problem is with the way you are assigning a value to your array $data

. I've simplified the logic in the if statement for you; since you only return one row through limit(1)

in your active write request you don't need it foreach

.

0


source


Try the below code below. Also, on the controller, you used $ data, which I think might have been confused, so he changed it to userigninfo codeigniter and you didn't set any variable on the controller for the name

Model

public function sign_in()
{
if (isset($_POST)) {

    $this->load->library('session');

    $email = $this->input->post('email');
    $password = $this->input->post('password');

    // Check is the same on table in database case sensitive I think
    $this->db->select('id', 'name', 'email');
    $this->db->from('users');
    $this->db->where('email', $email);

    // I would not use MD5 Not Secure Any More
    $this->db->where('password', md5($password));

    $query = $this->db->get();

    if ($query->num_rows() > 0) {
        return $query->row_array();
    } else {
        return false;
    }
}

      

Controller

public function index() {
$this->load->model('Login');

$user_info = $this->Login->sign_in();

if ($user_info) {
    $data['id'] = $user_info['id'];
    $data['name'] = $user_info['name'];
    $data['email'] = $user_info['email'];

    $this->load->view('index', $data);
    echo 'success';
    print_r($user_info);

} else {

    $this->load->view('index');

}

}   

      

In view

<?php echo $name;?>

      

I'm also not sure if you are using any form validation from codeigniter when submitting the form

http://www.codeigniter.com/userguide2/libraries/form_validation.html

0


source







All Articles