gpt4 book ai didi

jquery - 使用 CodeIgniter (Active Record) 从数据库自动完成

转载 作者:搜寻专家 更新时间:2023-10-30 20:18:48 24 4
gpt4 key购买 nike

我的网站上有一个表格,可以在其中提交一只猫。该表单包含“姓名”和“性别”等输入,但我只是想让自动完成功能与“姓名”字段一起使用。这是我的 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;
}
}

这是我的 Controller :

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

这是我的 table 的图片: This is an image of my table:

感谢所有的帮助。

最佳答案

如果有帮助,试试这个:

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

关于jquery - 使用 CodeIgniter (Active Record) 从数据库自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17354191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com