0

Below is my code

$state = $data['state'];
$this->db->select("state")->from('table')
    ->where('en_movingfrom_state', $state)
    ->where('status', 3);

I want to execute this query, if $state is set then where condition will execute.

So please help me how can i do this?

2 Answers 2

1

Try this:

$state = $data['state'];

// checking `$state has set`
if (isset($state) && (trim($state) != '')) {
    $this->db->where('en_movingfrom_state', $state)
}
$result = $this->db->select("state")->from('table')
    ->where('status', 3)
    ->get()->result_array();

// Output
print_r($result);
Sign up to request clarification or add additional context in comments.

Comments

0

you can use isset() function to check whether $state is set

$state = $data['state'];
$this->db->select("state")->from('table');
if(isset($state) && $state != ""){
   $this->db->where('en_movingfrom_state', $state);
}
$this->db->where('status', 3);
$this->db->get()->result_array();

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.