我的网站上有一张表格可以提交一只猫。表单包含“名称”和“性别”等输入,但我只是尝试使用“名称”字段进行自动完成。这是我的jquery的样子:
$(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>
我被困住了。在我的控制台中,如果我在模型中取消注释var_dump,当我键入字母'a'时,我能够看到此输出:
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"
}
}
这是我桌子的图像:
我感谢所有的帮助。
答案 0 :(得分: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 :(得分: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);
}
我希望这会有助于你的答案。