Mark Alan Mark Alan - 6 months ago 8
SQL Question

not getting all rows from the database

I have developed an autocomplete function in codeigniter but the problem is that if we type two letters, it matches 2 or more than 2 characters but it is only showing 1 record I don't know why is this happening. Can anyone help me out.

I have 3 records that matches criteria like this:


  • Vehicles

  • Heavy road Vehicles

  • Small Vehicles



When I type Ve, it shows me Design & Development and when I type in Beh, it shows me only Vehicle as it should show all three. All 3 records contain name vehicles, can any one let me know where I am doing a mistake

Here is my php model Get_data.php

public function result($keyword) {
$get_data = $this->db->like('cat_name', $keyword);
$get_data = $this->db->order_by("cat_id", "ASC");
$get_data = $this->db->get('categories');

foreach($get_data->result() as $data):
$sport = str_replace($keyword, "<b>" . $keyword . "</b>" , $data->cat_name);
$html = '<li onclick="set_item(\''.str_replace("'", "\'", $data->cat_name).'\')">';
$html .= $sport;
$html .= "</li>";
return $html;
endforeach;
}


My php controller

public function getData() {
$keyword = $this->input->post('keyword');
$html = $this->get_data->result($keyword);
echo $html;
}


Here is the ajax query I used

function autocomp() {
var min_length = 0;
var keyword = $("#skills").val();

if(keyword.length > min_length) {
$.ajax({
type : "POST",
data : {keyword: keyword},
dataType : 'text',
url : "http://localhost:90/tufuturo/home/getData",
success : function(data) {
$("#resultkeyword").show();
if(data == false) {
$("#resultkeyword").html("<li>No Results Found</li>");
} else {
$("#resultkeyword").html(data);
}
}
});
} else {
$("#resultkeyword").hide();
}
}

function set_item(item) {
$("#skills").val(item);
$("#resultkeyword").html("<li><b>"+item+"</b></li>");
$("#resultkeyword").hide();
}

Answer

Update foreach loop only

$html = '';
foreach($get_data->result() as $data):
            $sport = str_replace($keyword, "<b>" . $keyword . "</b>" , $data->cat_name);
            $html  .= '<li onclick="set_item(\''.str_replace("'", "\'", $data->cat_name).'\')">';
            $html .= $sport;
            $html .= "</li>";
            
        endforeach;
return $html;

Comments