Mark Alan
Mark Alan

Reputation: 455

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:

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();
}

Upvotes: 2

Views: 105

Answers (3)

Saty
Saty

Reputation: 22532

After return type everything would be vanish. You write return inside foreach loop. Loop iterate only one time and return.

Need to concatenate start tag of li with $html

also return outside of foreach loop as

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;// return here

Use num_rows(); to check query return result or not as

$num = $get_data->num_rows();

if($num>0)
{

 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;
}else{
     $html="<li>NO result found</li>";
}

    return $html;// return here

Upvotes: 2

zohaib
zohaib

Reputation: 11

try giving LIKE command the third parameter which let you place wildcard % in you desired place. I would suggest use 'after' in third parameter change this line $get_data = $this->db->like('cat_name', $keyword); to

$get_data = $this->db->like('cat_name', $keyword,'after');

Upvotes: 0

Jignesh Patel
Jignesh Patel

Reputation: 1022

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;

Upvotes: 4

Related Questions