0

嗨,伙计们,当我输入任何字母时,我在显示数据时遇到问题,没有数据可以完成任务,因此它没有从数据库中获取任何数据

控制器

<?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");

我的数据库名称是鸟我的表名称是鸟我的问题是当我找不到任何数据库时

4

2 回答 2

0

这不是解决方案,而是识别错误以便轻松解决它的基本步骤。

1> 首先使用 Firebug 检查传出请求和调用哪个 url 以及返回值。你有 70% 的机会在这里自己得到错误。

2> 在浏览器中手动调用前一种情况下的 url 并检查是否得到预期结果。下一个 5% 的机会是你在这里犯了错误。

3> 如果您得到正确的响应,请在 js 中使用 alert 来检查您是否收到了回复。剩下的 25% 是因为 autocomplete(js) 无法解析接收到的字符串。

于 2013-09-11T06:48:54.963 回答
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);
        }

    }
}
于 2013-09-11T06:57:16.227 回答