大家好我在输入任何字母时显示数据有问题,没有数据可以完成任务 所以它没有从数据库中获取任何数据
控制器
<?php
//birds.php
class Birds extends CI_Controller
{
function index()
{
$this->load->view('birds_view');
}
function get_birds()
{
$this->load->model('birds_model');
if (!isset($_GET['term'])) {
$q = strtolower($this->input->get('term'));
$this->birds_model->get_bird($q);
}
}
}
--------------------模型
//birds_model.php (Array of Strings)
class Birds_model extends CI_Model
{
function get_bird($q)
{
$this->db->select('birds');
$this->db->like('birds', $q, 'after');
$query = $this->db->get('birds');
if ($query->num_rows > 0) {
foreach ($query->result_array() as $row) {
$row_set[] = htmlentities(stripslashes($row['birds'])); //build an array
}
echo json_encode($row_set); //format the array into json data
}
}
}
--------------------------------------视图
<html>
<head>
<link href="css/jquery-ui.css">
<script src=
"http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type=
"text/javascript"></script>
<script src="js/jquery-ui.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#birds").autocomplete({
source: "birds/get_birds" // path to the get_birds method
});
});
</script>
<title></title>
</head>
<body>
<form>
<input id="birds" type="text">
</form>
</body>
</html>
这是我的数据库
CREATE TABLE birds (
id INT NOT NULL AUTO_INCREMENT,
bird VARCHAR(50),
aka VARCHAR(50),
PRIMARY KEY (id)
) ENGINE = MYISAM ;
INSERT INTO birds (id, bird, aka) VALUES
(1, "Great Bittern", "Botaurus stellaris"),
(2, "Little Grebe", "Tachybaptus ruficollis"),
(3, "Black-necked Grebe", "Podiceps nigricollis"),
(4, "Little Bittern", "Ixobrychus minutus"),
(5, "Black-crowned Night Heron", "Nycticorax nycticorax"),
(6, "Purple Heron", "Ardea purpurea"),
(7, "White Stork", "Ciconia ciconia"),
(8, "Spoonbill", "Platalea leucorodia"),
(9, "Red-crested Pochard", "Netta rufina"),
(10, "Common Eider", "Somateria mollissima"),
(11, "Red Kite", "Milvus milvus"),
(12, "Hen Harrier", "Circus cyaneus"),
(13, "Montagu's Harrier", "Circus pygargus"),
(14, "Black Grouse", "Tetrao tetrix"),
(15, "Grey Partridge", "Perdix perdix"),
(16, "Spotted Crake", "Porzana porzana"),
(17, "Corncrake", "Crex crex"),
(18, "Common Crane", "Grus grus"),
(19, "Avocet", "Recurvirostra avosetta"),
(20, "Stone Curlew", "Burhinus oedicnemus");
我的数据库名称是鸟类 我的桌子名字叫鸟 我的问题是什么时候我没有找到任何数据库
答案 0 :(得分:0)
这不是解决方案,而是识别错误以便轻松解决错误的基本步骤。
1&GT;首先使用Firebug检查传出请求并调用哪个url以及返回值。你有70%的机会在这里得到错误。
2 - ;在浏览器中手动调用上一个案例中的URL并检查您是否获得了预期结果。接下来有5%的可能性是你在这里弄错了。
3&GT;如果您得到正确的响应,请使用js中的警报来检查您是否收到了回复。剩余的25%是由于自动完成(js)无法解析收到的字符串而发生的。
答案 1 :(得分:0)
试试这个:
class Birds_model extends CI_Model
{
function get_bird($q)
{
$this->db->select('bird as label, bird as value, id', false);
$this->db->like('bird', $q, 'AFTER');
$query = $this->db->get('birds');
if( $query->num_rows() > 0 ){
$data = $query->result_array();
}else{
$data = array();
$data['label'] = "No results found";
$data['value'] = "No results found";
$data['id'] = '0';
}
return json_encode( $data );
}
}
class Birds extends CI_Controller
{
function index()
{
$this->load->view('birds_view');
}
function get_birds()
{
$this->load->model('birds_model');
if (isset($_GET['term'])) {
$q = strtolower($this->input->get('term'));
echo $this->birds_model->get_bird($q);
}
}
}