luqita
luqita

Reputation: 4087

CodeIgniter: Adding to where clause

I have the following condition in ExpressionEngine (which is using CodeIgniter framework):

if ($group_id !== '')
    {
        $this->db->where("members.group_id", $group_id);
    }

I´d like to add an extra condition to that where (an AND), how can I do this? Do I add a new $this->db->where('my_field', $my_value)? Or how?

Upvotes: 0

Views: 1795

Answers (4)

GautamD31
GautamD31

Reputation: 28763

Yah.....u can try this:

if ($group_id !== '')
{
    $this->db->where("members.group_id", $group_id);
    $this->db->where('my_field', $my_value);
}

u can also use:

$this->db->or_where('my_field',$my_value);

Upvotes: 0

Exploit
Exploit

Reputation: 6386

What i do is write out the query such as:

$data = array('name' => $_POST['name'], 'lname' => $_POST['lname']);
$sql = "select table where name = ? and lname = ?";
$this->db->query($sql, $data);

the reason why i prefer to write it out is because once you end up writing very complex queries with many table joins it becomes really difficult to read active record queries.

Upvotes: 0

Igor Parra
Igor Parra

Reputation: 10348

// if (is_numeric($group_id) && $group_id > 0)
if ($group_id)
{
    $this->db->where("members.group_id", $group_id);
    $this->db->where('my_field', $my_value);
    // $this->db->where('my_field >=', $my_value);
    // $this->db->where('my_field <', $my_value);
}

Upvotes: 0

Farzher
Farzher

Reputation: 14593

You guessed it.

if ($group_id !== '')
{
    $this->db->where("members.group_id", $group_id);
    $this->db->where('my_field', $my_value);
}

Check Codeigniter documentation http://codeigniter.com/user_guide/database/active_record.html It's good.

Upvotes: 2

Related Questions