我正在努力通过 CodeIgniter 自动完成。我遵循了http://www.codersmount.com/2012/09/jquery-ui-autocomplete-in-codeigniter-with-database/上的教程。我更改了我的数据库等和变量,但是当将数据库更改为不存在的数据库时,它不会给出任何错误,所以我猜它是视图本身的东西,但无法弄清楚是什么。
提前致谢
<?php
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<link href="<?php echo base_url() . 'resources/css/jquery-ui-1.10.3.custom.css' ?>" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="<?php echo base_url() . 'resources/js/jquery-1.9.1.js' ?>"></script>
<script type="text/javascript" src="<?php echo base_url() . 'resources/js/jquery-ui-1.10.3.custom.js' ?>"></script>
<script>
$(document).ready(function() {
alert('test');
$(function() {
$("#test").autocomplete({
source: "birds/get_birds"
});
});
});
</script>
<title>Add Project</title>
</head>
<body>
ID :<input type="text" id="test"> <br>
</body>
</html>
这是我的控制器:
<?php
//birds.php
class Birds extends CI_Controller{
function index(){
$this->load->view('birds_view');
}
public function get_birds(){
$this->load->model('birds_model');
if (isset($_GET['term'])){
$q = strtolower($_GET['term']);
$this->birds_model->get_bird($q);
}
}
}
?>
这是我的模型:
<?php
//birds_model.php (Array of Strings)
class Birds_model extends CI_Model{
function get_bird($q){
$this->db->select('Code');
$this->db->like('Code', $q);
$query = $this->db->get('R_Projects');
if($query->num_rows > 0){
foreach ($query->result_array() as $row){
$row_set[] = htmlentities(stripslashes($row['Code'])); //build an array
}
echo json_encode($row_set); //format the array into json data
}
}
}