Udders
Udders

Reputation: 6976

Forming a query with active record

I am trying to form a query using codeigniter's active record, the search is based on a comma seperated list that user can input.

I know how to get the search criteria out as sepererate values :

$criteria = explode($this->input->post('search_criteria');

What I do not know is how I can create a query in my model that creates a enough where comparisons based on the number of criteria.

So for example if the user were to search for PHP, HTML, Project Management

I would need a query that looks like this,

SELECT * FROM candidates WHERE candidate_job_tags LIKE %PHP% OR WHERE candidate_job_tags LIKE %HTML% OR WHERE candidate_job_tags LIKE %Project Management%

Can this be done?

Upvotes: 1

Views: 128

Answers (1)

Colin Brock
Colin Brock

Reputation: 21575

You can use the Active Record class' or_like() method to build that portion of your query:

// Assuming your array looks like something like this
$criteria = array('PHP', 'HTML', 'Project Management');

foreach($criteria as $key=>$val){
    $this->db->or_like('candidate_job_tags', $val);
}

$query = $this->db->get('candidates');

would produce a query that looks like this:

SELECT * FROM (`categories`) WHERE `category_title` LIKE '%PHP%' OR `category_title` LIKE '%HTML%' OR `category_title` LIKE '%Project Management%'

Upvotes: 2

Related Questions