1

Please Help.. im using CODEIGNITER I want to print the value of my userlevel and email using echo.. to know if im getting the right data from the database.. then suddenly this error keep bugging me. beginner in CODEIGNITER need some help here.. thanks a lot!

in my controller:

if ($this->is_validated($rules))
{
        $where  = array('email' => $this->input->post('txt_email'));
        $data['query'] = $this->database_model->select_userlevel('userlevel', $where);
        $user = $data['query'];
        foreach($user->result() as $row)
        {
           echo $row->email;
           echo $row->userlevel;
        }
}

in my model:

public function select_userlevel($table, $where) 
{
    $this->db->where($where);
    $query =  $this->db->get($table);
    if ($query->num_rows() > 0) 
    {
        return true;
    }
    else
    {
        echo 'no value';
        return false;
    }
}
0

2 Answers 2

2

Instead of return true you need to return your query in model

Model

public function select_userlevel($table, $where) 
{
    $this->db->where($where);
    $query =  $this->db->get($table);
if ($query->num_rows() > 0) 
{
    return  $query;// return query here
}
else
{

    return false;
}

Controller

$user = $this->database_model->select_userlevel('userlevel', $where);
     foreach($user->result() as $row)
        {
       // your code here
      }

UPDATED FOR better solution and proper use of MVC.

Instead of return query you need to return data from your model

MODEL

public function select_userlevel($table, $where)
{
$this->db->where($where);
$query = $this->db->get($table);
if ($query->num_rows() > 0)
{
return $query->result(); // return your result
}
else
{

return false;
}
}

CONTROLLER

$user = $this->database_model->select_userlevel('userlevel', $where);
foreach ($user as $row) {
    // your code here
}
Sign up to request clarification or add additional context in comments.

5 Comments

agreed. The problem is that you are never returning the results to your controller.
@CodeGodie agree with you . This is bad use of MVC . I am only pointing the wrong thing in OP code
hi @Saty, can you explain why this is bad use of MVC?
Fatching the result in controller is bad use .
Gotcha. yes I agree. the controller should only receive the results.
0

$user is not an active record object, so it does not have a reference to the result method.

All of this logic belongs in the Model and not the Controller.

Controller

public function doSomething()
{
   if( !$this->is_validated($rules) ) 
       return;

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

   $users = $this->database_model->select_userlevel($email);

   // for debugging
   var_dump( $users )
   die();
}

Model

public function select_userlevel($email)
{
    $query = $this->db->select()
                      ->where('email', $email)
                      ->get('userlevel');

    return ( $query->num_rows() > 0) ? $query->result() : false;
}

2 Comments

How is this different than @Saty 's answer?
It's not, he was just faster at typing the answer :)

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.