0

I found a problem regarding autocomplete in codeigniter, i want to make if I fill siteid with autocomplete, in another form (sitename) will show data from database directly, but i failed, autocomplete form in siteid not shown, so another field can't worked too

Here my controller code :

    public function get_allkota() {
    $kode = $this->input->post('kode',TRUE); 
    $query = $this->db->get("tbl_site");

    $kota       =  array();
    foreach ($query as $d) {
        $kota[]     = array(
            'label' => $d->siteid,
            'nama' => $d->siteid ,
            'sitename' => $d->sitename,
        );
    }
    echo json_encode($kota);   
}

Here my jquery code :

    <script src="<?php echo base_url(); ?>assets/js/jquery.min.js" type="text/javascript"></script>
<script src="<?php echo base_url(); ?>assets/js/jquery-ui.js" type="text/javascript"></script>
<script>
 $(function () {
    $("#kode").autocomplete({  
        minLength:0,
        delay:0,
        source:'<?php echo site_url('app_admin_kabupaten/get_allkota'); ?>', 
        select:function(event, ui){
            $('#siteid').val(ui.item.siteid);
            $('#sitename').val(ui.item.sitename);
            }
        });
    });
</script>

Here my form code :

<p><input type="text" id="kode" placeholder="Fill site ID" > </p>
<p>Site ID : </br><input type="text" id="siteid"></br>
   Site Name : </br><input type="text" id="sitename"></br>

thanks

2
  • "I found a problem regarding autocomplete in codeigniter"... So what is the problem? Commented Jun 20, 2017 at 4:40
  • @DamithRuwan i was edit it, i can't run autocomplete data, if you any suggestion, please advise, thanks Commented Jun 20, 2017 at 4:43

3 Answers 3

1

The problem is in your controller code, when you do $this->db->get('table_name'), codeigniter returns query object. You need to fetch the results from it.

from documentation:

$query = $this->db->get('table_name');

foreach ($query->result() as $row)
{
        echo $row->field;
}

So change your controller code to this and it should work,

public function get_allkota() {
    $kode = $this->input->get('term'); 

    $this->db->like('siteid', $kode); 
    $query = $this->db->get("tbl_site");
    $kota       =  array();

    foreach ($query->result() as $d) {
        $kota[]     = array(
            'label' => $d->siteid,
            'nama' => $d->siteid ,
            'sitename' => $d->sitename,
        );
    }
    echo json_encode($kota);   
}
Sign up to request clarification or add additional context in comments.

5 Comments

it didn't work, i change into foreach ($query->result() as $row){echo $row->sitename;echo $row->siteid;}, may you give in full edit?
have you tried the get_allkota() function that i have included in my answer?
i think your get_allkota() same with my controller before,
no. That's what i said in my answer. you looped the $query object. you need to loop $query->results() which contains the results.
0
// js code after document is ready
// Search autocomplete
$("#swSearch").autocomplete({
    minLength: 1,
    source: function(req, add){
        $.ajax({
            url: '/search', //Controller where search is performed
            dataType: 'json',
            type: 'POST',
            data: req,
            success: function(data){
                if(data.response =='true'){
                   add(data.message);
                }
            }
        });
    }
});

// Controller search function


    $keyword = $this->input->post('term');

    $data['response'] = 'false'; //Set default response

    $query = $this->Mprofile->sw_search($keyword); //Model DB search

    if($query->num_rows() > 0){
       $data['response'] = 'true'; //Set response
       $data['message'] = array(); //Create array
       foreach($query->result() as $row){
          $data['message'][] = array('label'=> $row->friendly_name, 'value'=> $row->friendly_name); //Add a row to array
       }
    }
    echo json_encode($data);

// Simple model example

public function sw_search($keyword)
    {
        $this->db->select('id, friendly_name');
        $this->db->from('business_category');
        $this->db->where('suppress', 0);
        $this->db->like('friendly_name', $keyword);
        $this->db->order_by("friendly_name", "asc");

        $query = $this->db->get();
        foreach($query->result_array() as $row){
            //$data[$row['friendly_name']];
            $data[] = $row;
        }
        //return $data;
        return $query;
    }

Enjoy

Comments

0
In core PHP here is the code

///////////////////////Search.php////////////////

    <?php
$dbHost = 'localhost';
$dbUsername = '';
$dbPassword = '';
$dbName = '';
//connect with the database
$db = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);
//get search term
$searchTerm = $_GET['term'];
//get matched data from skills table
$query = $db->query("SELECT * FROM tablename WHERE column_name LIKE '%".$searchTerm."%' group by column_name ORDER BY column_name ASC");
while ($row = $query->fetch_assoc()) {
    $data[] = $row['column_name'];
}
//return json data
echo json_encode($data);
?>


///////////////////////////////////////////////

//////////////////Index.php///////////////////

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">

  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <script>
  $(function() {
    $( "#skills" ).autocomplete({
      source: 'search.php'
    });
  });
  </script>
</head>
<body>

<div class="ui-widget">
  <label for="skills">Skills: </label>
  <input id="skills">
</div>
</body>
</html>

/////////////////////////////////////////////////////

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.