0

Just want to return a variable from model to controller and then check if it's value is 0 or 1 but it does not work. I use Codeigniter. Could you please check my code and help me to find my mistake.

Here is my model:

public function did_get_status($id){        

        $this->db->select('*');
        $this->db->from('members'); 
        $this->db->where('id',$id);     
        $query = $this->db->get();

        if ($query->num_rows() == 1) {  

    $row = $query->row();
    return array(
    'member_status' => $row->member_status, 
    );
        }
        else{
         return false;
        }   

   } 

Here is my controller:

$status_info = $this->model_users->did_get_status($id);
$status = $status_info['member_status'];        
                if ($status === 0) {
                    //
                } 
                else if ($status === 1) {
                    //
                }                   
            }
1
  • If you are looking for a boolean false then don't check for 0 check for boolean false (false === $status) and you don't need an "if else if" just an "if else". Ignore the fact in PHP that false can equal 0, and your programming life will be easier. Commented Oct 29, 2014 at 14:33

3 Answers 3

3

I think first check by printing the value that you are getting from function. like Here is my controller

$status_info = $this->model_users->did_get_status($id);
    var_dump($status_info);

if this print it as an array then match value with this type

$status = $status_info['member_status'];        
            if ($status == 0) {
                //
            } 
            else if ($status == 1) {
                //
            }                   
        }

replace "===" with "==" because returing type may not be some. like array return 1,0 as string and 1,o is intiger here.

Sign up to request clarification or add additional context in comments.

1 Comment

'===' will check for type of variable/object. '==' will match the values.
1

The obvious points to consider:

Is there only one match for this ID in the database?

Have you attempted to use double equals rather than triple equals?

Comments

1

This is all trivially debuggable.

$status = $status_info['member_status'];

You cannot do this, as the function can return false. You don't do error handling.

if ($status === 0)

It's probably a different data type, probably a string. You do strict comparison.

Use ==. Or, even better, fix it in the model: (int) value.

Comments

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.