2013-06-27 20 views
2

我在我的網站上有一個表單,可以在其中提交一隻貓。該表單包含諸如「姓名」和「性別」等輸入,但我只是試圖讓自動完成功能與「姓名」字段配合使用。這裏是我的jQuery的樣子:使用CodeIgniter自動完成數據庫(Active Record)

$(document).ready(function() { 
$("#tags").autocomplete({ 
source: '/Anish/auto_cat' 
}); 
}); 

這裏是我的模型是什麼樣子:

public function auto_cat($search_term) { 
    $this->db->like('name', $search_term); 
    $response = $this->db->get('anish_cats')->result_array(); 
    // var_dump($response);die; 
    return $response; 
    } 
} 

這裏是我的控制器:

public function auto_cat(){ 
    $search_term = $this->input->get('term'); 
    $cats = $this->Anish_m->auto_cat($search_term); 
} 

,這裏是我的看法:

<head> 
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" /> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> 
<link rel="stylesheet" href="/resources/demos/style.css" /> 
</head> 

<h1>Anish's Page</h1> 
<form action="/Anish/create" method="POST"> 
    <div class="ui-widget"> 
    <label for="tags">Name</label><input id="tags" type="text" name="name"> 
    </div> 
    <div> 
    <label>Age</label><input type="text" name="age"> 
    </div> 
    <div> 
    <label>Gender</label><input type="text" name="gender"> 
    </div> 
    <div> 
    <label>Species</label><input type="text" name="species"> 
    </div> 
    <div> 
    <label>Eye Color</label><input type="text" name="eye_color"> 
    </div> 
    <div> 
    <label>Color</label><input type="text" name="color"> 
    </div> 
    <div> 
    <label>Description</label><input type="text" name="description"> 
    </div> 
    <div> 
    <label>marital status</label><input type="text" name="marital_status"> 
    </div> 
    <br> 
    <button type="submit" class="btn btn-block btn-primary span1">Add cat</button> 
</form> 
<br/><br/><br/><br/> 
    <table class="table table-striped table-bordered table-hover"> 
    <thead> 
     <tr> 
     <th>Name</th> 
     <th>Gender</th> 
     <th>Age</th> 
     <th>Species</th> 
     <th>Eye Color</th> 
     <th>Color</th> 
     <th>Description</th> 
     <th>Marital Status</th> 
     <th>Edit</th> 
     <th>Delete</th> 
     </tr> 
    </thead> 
    <tbody> 
     <?php foreach ($cats as $cat):?> 
     <tr> 
      <td> 
      <?php echo ($cat['name']);?><br/> 
      </td> 
      <td> 
      <?php echo ($cat['gender']);?><br/> 
      </td> 
      <td> 
      <?php echo ($cat['age']);?><br/> 
      </td> 
      <td> 
      <?php echo ($cat['species']);?><br/> 
      </td> 
      <td> 
      <?php echo ($cat['eye_color']);?><br/> 
      </td> 
      <td> 
      <?php echo ($cat['color']);?><br/> 
      </td> 
      <td> 
      <?php echo ($cat['description']);?><br/> 
      </td> 
      <td> 
      <?php echo ($cat['marital_status']);?><br/> 
      </td> 
      <td> 
      <form action="/Anish/edit" method="post"> 
      <input type="hidden" value="<?php echo ($cat['id']);?>" name="Anish_id_edit"> 
      <button class="btn btn-block btn-info">Edit</button> 
      </form> 
     </td> 
     <td> 
      <form action="/Anish/delete" method="post"> 
      <input type="hidden" value="<?php echo ($cat['id']);?>" name="Anish_id"> 
      <button class="btn btn-block btn-danger">Delete</button> 
      </form> 
     </td> 
     </tr> 
     <?php endforeach;?> 
    </tbody> 
    </table> 

我被卡住了。在我的控制檯,我能看到這個輸出,當我鍵入字母「A」,如果我取消在我的模型中的var_dump:

array(4) { 
    [0]=> 
    array(9) { 
    ["id"]=> 
    string(2) "13" 
    ["name"]=> 
    string(5) "Anish" 
    ["gender"]=> 
    string(4) "Male" 
    ["age"]=> 
    string(2) "20" 
    ["species"]=> 
    string(3) "Cat" 
    ["eye_color"]=> 
    string(5) "Brown" 
    ["color"]=> 
    string(5) "Black" 
    ["description"]=> 
    string(7) "Awesome" 
    ["marital_status"]=> 
    string(1) "0" 
    } 
    [1]=> 
    array(9) { 
    ["id"]=> 
    string(2) "16" 
    ["name"]=> 
    string(5) "Anish" 
    ["gender"]=> 
    string(2) "fe" 
    ["age"]=> 
    string(2) "23" 
    ["species"]=> 
    string(2) "fe" 
    ["eye_color"]=> 
    string(2) "fe" 
    ["color"]=> 
    string(2) "fe" 
    ["description"]=> 
    string(2) "fe" 
    ["marital_status"]=> 
    string(1) "1" 
    } 
    [2]=> 
    array(9) { 
    ["id"]=> 
    string(2) "17" 
    ["name"]=> 
    string(1) "a" 
    ["gender"]=> 
    string(1) "a" 
    ["age"]=> 
    string(1) "4" 
    ["species"]=> 
    string(1) "a" 
    ["eye_color"]=> 
    string(1) "a" 
    ["color"]=> 
    string(1) "a" 
    ["description"]=> 
    string(1) "a" 
    ["marital_status"]=> 
    string(1) "0" 
    } 
    [3]=> 
    array(9) { 
    ["id"]=> 
    string(2) "18" 
    ["name"]=> 
    string(4) "Matt" 
    ["gender"]=> 
    string(6) "Female" 
    ["age"]=> 
    string(2) "80" 
    ["species"]=> 
    string(6) "ferret" 
    ["eye_color"]=> 
    string(4) "blue" 
    ["color"]=> 
    string(4) "pink" 
    ["description"]=> 
    string(5) "Chill" 
    ["marital_status"]=> 
    string(1) "0" 
    } 
} 

這是我的表的圖像: This is an image of my table:

我感謝所有的幫助。

回答

2

試試這個,如果有幫助:

<?php 
    #controller function 
    public function auto_cat(){ 
     print_r ($this->model->search_auto_cat($_REQUEST['term'])); 
    } 

    #model function 
    function search_auto_cat($term){ 
     $data = array(); 
     $rs  = $this->db->select('name as label, name as value, id', false)->or_like('name', $term)->or_like('last_name', $term)->or_like('id', $term)->limit(20)->get('anish_cats'); 
     //print_r($this->db->last_query()); 
     if($rs->num_rows() > 0){ 
      $temp = $rs->result_array(); 
     }else{ 
      $temp = array(); 
      $temp[0]['label'] = "No results found"; 
      $temp[0]['value'] = ""; 
      $temp[0]['id']  = "0"; 

     } 
     $data = json_encode($temp); 
     return $data; 
    } 
?> 
1

這意味着它是工作,但問題是,你只能從一列需要的數據。 所以在模型

public function auto_cat($search_term) { 
$this-db->select('name'); 
$this->db->like('name', $search_term); 
$response = $this->db->get('anish_cats')->result_array(); 
// var_dump($response);die; 
return $response; 
} 

}

和控制器。

public function auto_cat(){ 
     $search_term = $this->input->get('term'); 
    $cats = $this->Anish_m->auto_cat($search_term); 
     print json_encode($cat); 
} 

我希望這會幫助你的答案。

相關問題