weixin_33713350 2016-01-18 15:54 采纳率: 0%
浏览 30

Codeigniter Php自动完成

I am trying to achieve autocomplete search and I added all necessary jQuery but it still doesn't work.

Here is my View :

<script type="text/javascript">
$(document).ready(function(){
   $('#dseancename').autocomplete({
       source:"<?php echo site_url('AddSeance/search/?'); ?>"

   });
});

And my Controller:

function search() {
  $this->load->model('SalonModel');
if(isset($_GET['term'])){
$result = $this->SalonModel->search($_GET['term']);
if(count($result) >0){
    foreach ($result as $pr)
        $arr_result[] = $pr->name;
    echo json_encode($arr_result);
        }
}}

And my Model :

 function search($name) {

    $this->db->like('name',$name,'both');
    return $this->db->get('seance')->result();//table name seance
}

I tried to make it an array and it works. But I can't make it with database values. I searched on the internet and find someone who already did it but it doesn't work to.

  • 写回答

1条回答 默认 最新

  • weixin_33704591 2016-01-18 16:05
    关注

    In your modelview you can make a search.

     public function search($name)
    {    
    $query = $this->db->query("SELECT * FROM tablename WHERE row LIKE ('$name%')");
    if($query->num_rows > 0)
        {
        foreach ($query->result_array() as $row)
              {
             $row_set[] = htmlentities(stripslashes($row['tablename']));
              }
             echo json_encode($row_set); 
        }
     }
    
    评论

报告相同问题?