Val Do
Val Do

Reputation: 2695

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

Upvotes: 3

Views: 408

Answers (3)

CodeGodie
CodeGodie

Reputation: 12132

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;

Upvotes: 2

user4419336
user4419336

Reputation:

Try code below not tested. Also on controller you used $data I think might of been confused so changed it to user_info codeigniter and you did not set any variable on the controller for 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');

}

}   

On View

<?php echo $name;?>

I am not sure also if your using any form validation from codeigniter when submitting form

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

Upvotes: 0

Daniel Waghorn
Daniel Waghorn

Reputation: 2985

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 stems from how you're assigning the value to your $data array. I simplified the logic in the if statement for you too; since you're only returning a single row via the limit(1) in your active record query you don't need the foreach.

Upvotes: 0

Related Questions