AnkitJ
AnkitJ

Reputation: 356

How to compare array value with column in codeigniter database query

I am using codeigniter with php. I want to get values from database table by comparing a column. The problem is the value coming from html code to compare with column is checkbox value and array. I want something like it, HTML

<input type="checkbox" name="car[]" value="Audi">Audi<br>
<input type="checkbox" name="car[]" value="BMW">BMW<br>
<input type="checkbox" name="car[]" value="Ford">Ford<br>

and code in codeigniter model is $this->db->where('car_name ==', $car[]); $this->db->get('cars');

Upvotes: 0

Views: 2626

Answers (2)

Deenadhayalan Manoharan
Deenadhayalan Manoharan

Reputation: 5444

Try this...

Generates a WHERE field IN ('item', 'item') SQL query

$names = array('Frank', 'Todd', 'James');
$this->db->where_in('username', $names);
// Produces: WHERE username IN ('Frank', 'Todd', 'James')

Ref:https://ellislab.com/codeigniter/user-guide/database/active_record.html

Upvotes: 1

Sougata Bose
Sougata Bose

Reputation: 31739

Use where_in -

$this->db->where_in('car_name', $car);

Upvotes: 3

Related Questions