I am using php, ajax and mysql to make an autocomplete function, when the user starts to enter the name of their school I use ajax to send the value they entered into a text field to a php function. that function then uses a like %string% to find colleges that match what they entered. I have a couple of issues tho.
For example I have Cal Poly (California polytech) in my database as:
California Polytechnic State University-San Luis Obispo
But usually the user would enter in California Polytech, they would just enter in Cal poly or the phrase for their school name.
but when i type in "Cal Poly" no results show up, i have to type in california to get the cal poly school to show up.
This is just an example and I need it for other schools as well.
Is there any way to improve my database query to make it return results that are even slightly similar without returning things that aren't similar?
Here is my code so far:
Jquery:
$(".input-college").keyup(function(){
var d = $('#search-college-form').serialize();
var v = $(this).val();
if (v.length > 4) {
$.ajax({
url: '/colleges/search_college_names',
data: d,
type: "POST",
success:function(data){
$(".input-d-down ul").html(data);
}
});
}
});
PHP
colleges controller
public function search_college_names() {
$search = $_POST['college-search'];
$data['college_list'] = $this->College_Model->search_colleges($search);
$this->load->view('inputs_views/search-college-dropdown', $data);
}
colleges model
public function search_colleges($search) {
$query = $this->db->select()->from('collegenames')->like("names", $search)->get();
return $query->result_array();
}
please let me know, I am kinda new to search function using like%%
Thanks
One way to go about this is to split the search string by spaces and then AND
them in the query.
For instance, the string "Cal poly"
would result in a query such as SELECT * FROM table WHERE name LIKE "%Cal%" AND name LIKE "%poly%"
.
If you need this to be a really fast and smart search you should look into Solr, but that's probably serious overkill for your problem.