Reputation:
i have written a method and model for keyword search. But unfortunately I am getting the output which are matched with my keyword and also which does not have any match with the keyword I typed.
Here is My Code:
controller.php :
function suggestions()
{
$this->load->model('Booksmodel');
$term = $this->input->post('term',TRUE);
$rows = $this->Booksmodel->GetAutocomplete(array('keyword' => $term));
$json_array = array();
foreach ($rows as $row)
array_push($json_array, $row->book_title);
array_push($json_array, $row->auth_firstname);
array_push($json_array, $row->isbn);
echo json_encode($json_array);
}
Please help me.
Upvotes: 1
Views: 1882
Reputation: 5411
Try this (Added another parameter, none, to 'like'
function ):
function GetAutocomplete($options = array())
{
$this->load->database();
$this->db->limit('10');
$this->db->select('book_title,auth_firstname,isbn,auth_lastname,publisher_name');
$this->db->like('book_title', $options['keyword'], 'none');
$this->db->or_like('auth_firstname', $options['keyword'], 'none');
$this->db->or_like('isbn', $options['keyword'], 'none');
$this->db->or_like('auth_lastname', $options['keyword'], 'none');
$this->db->or_like('publisher_name', $options['keyword'], 'none');
$query = $this->db->get('bookdetails');
return $query->result();
}
'none' will prevent it from using wildcard (%
) in like function.
Upvotes: 1