1

I'm trying to learn the MVC approach using CodeIgniter and I'm stuck with trying to display the results of the SQL query to the view. I'm using the method binding way as I've heard its secure with escaping variables when writing SQL queries.

Here's the model:

public function getuser_id()
{
    $this->db->select('id')->from('users')->where('email', $this->session->userdata('email'));
    $query = $this->db->get();
}

Here's the controller:

public function members()
{
    if ($this->session->userdata('is_logged_in'))
    {
        $data['title'] = 'Members Page';
        $this->load->model('model_users');
        $data['uid'] = $this->model_users->getuser_id();
        $this->load->view('members', $data);
    }
    else 
    {
        redirect('main/restricted');
    }
}

The view simply does echo $uid;. All I want to do is print the user ID on the screen. I believe that the issue lies in the getuser_id() function. The profiler says that the query is being executed on the page but I am not seeing a result. The data definitelythere exists in the database.

Any help would be greatly appreciated!

1
  • Instead of accessing the session directly in the model. You should access it in the controller, and pass it to the model as a parameter of the getuser_id() function. Commented Mar 22, 2013 at 4:03

1 Answer 1

2

There are a few problems with your code.

Firstly, instead of accessing the session directly in the model. You should access it in the controller, and pass the data to the model as a parameter of the getuser_id() function.

Secondly, you are making a query but not returning the data from it. To do this you will need to amend the line:

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

To be one of the following, depending on your preference:

$query = $this->db->get()->result(); //returns results as an object $query = $this->db->get()->result_array(); // returns results as an array

More info on returning results here. http://ellislab.com/codeigniter/user-guide/database/results.html

Thirdly, you dont pass any data from your model back to your controller. You need to add a return $your_data to the end of the models function.

Updated Model:

public function getuser_id($user_email)
{
    $this->db->select('id')->from('users')->where('email', $user_email);
    $query = $this->db->get()->result_array();

    return $query; // Don't forget to return your results!

}

Here's the controller:

public function members()
{
    if ($this->session->userdata('is_logged_in'))
    {
        $data['title'] = 'Members Page';
        $this->load->model('model_users');
        $user_email = $this->session->userdata('email');
        $data['uid'] = $this->model_users->getuser_id($user_email);
        $this->load->view('members', $data);
    }
    else 
    {
        redirect('main/restricted');
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hi @Jeemusu, thank you for the fast response. I've updated my code to pass the session variable as a parameter from the controller to the model. The code works with no errors but it is just not displaying the results. The database is autoloaded from the config file so it's definitely working, but just not displaying...
Actually, after including the return $query; in the model, I receive an 'Message: Array to string conversion' error
The getuser_id() function should fetch the result using get()->row() as there should only be one result, and then return the id only. return $query->id;
haha awesome @SeainMalkin! That did it, thank you for that! Also thank you to @Jeemusu for pointing me in the right direction. It's much appreciated guys. I'm just getting to grips with this from doing procedural style :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.