[eluser]rich.a.coy[/eluser]
Hello,
I'm working on a search feature and it works great as long as someone only enters one word. I have a complex SQL query that joins two tables and searches 6 columns using LIKE and also checking a "show" field for each column to remove nonpublic viewable data.
If someone types in two or more words it returns no results. I understand why but don't understand how to fix t. I assume I need to explode the search string but unsure how to actually run the query for each word. I looked in the forums and saw some examples using Active Record but my SQL statement is a little more complex and would prefer to not use Active Record on this query.
Also, where do I need to tell the results to be distinct records only?
Thanks for any help you can offer. I've only been using CI, and MVC, for about a month and these the CI community has been very friendly and helpful.
Here is my Model:
Code:
function search_results() {
$search_term = $this->input->post('searchbox');
if ($search_term != "") {
$sql = "SELECT * FROM user_profiles INNER JOIN users ON user_profiles.user_id = users.id WHERE fname LIKE '%".$this->db->escape_like_str($search_term)."%' AND show_fname = 'e'
OR user_profiles.lname LIKE '%".$this->db->escape_like_str($search_term)."%' AND show_lname = 'e'
OR user_profiles.country LIKE '%".$this->db->escape_like_str($search_term)."%' AND show_country = 'e'
OR user_profiles.state LIKE '%".$this->db->escape_like_str($search_term)."%' AND show_state = 'e'
OR user_profiles.city LIKE '%".$this->db->escape_like_str($search_term)."%' AND show_city = 'e'
OR users.username LIKE '%".$this->db->escape_like_str($search_term)."%'
";
$query = $this->db->query($sql);
return $query->result();
}
}
Thanks
Rich