2017-06-20 219 views
0

我发现一个关于codeigniter中的自动完成的问题,我想如果我填写siteid与自动完成,在另一种形式(sitename)将直接显示数据库中的数据,但我失败,自动完成表单在SITEID没有显示,那么另一个字段不能工作太CodeIgniter中的自动完成MySQL与JQuery的自动完成

这里我控制器代码:

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

这里我jQuery代码:

<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> 

这里我的表单代码:

<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> 

感谢

+0

“我发现关于笨自动完成的问题” ..那么问题是什么? –

+0

@DamithRuwan我是编辑它,我无法运行自动完成数据,如果您有任何建议,请告知,谢谢 – karisma

回答

1

的问题是在你的控制器代码,当你做$this->db->get('table_name'),笨返回查询对象。你需要从中获取结果。

从文档:

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

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

因此改变控制器代码这一点,它应该工作,

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); 
} 
+0

它没有工作,我改变为foreach($查询 - >结果()作为$行){回声$ row-> sitename; echo $ row-> siteid;},你可以给全编辑吗? – karisma

+0

您是否尝试过我的答案中包含的'get_allkota()'函数? –

+0

我认为你的get_allkota()与我之前的控制器相同, – karisma

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 
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> 

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