在codeigniter中自动完成

时间:2012-05-30 06:51:23

标签: php codeigniter jquery autocomplete

我尝试使用我从网站上获得的代码在codeignter中进行自动完成。但它对我不起作用。任何人都可以找到问题

我使用的视图函数在下面给出

<html>

    <head>
        <title>Autocomplete</title>

        <script src="<?php echo base_url().'js/jquery.autocomplete.js'?>"></script>
        <script src="<?php echo base_url().'js/jquery-1.6.4.js'?>"></script>

        <script type="text/javascript">

          $(function() {
          $( "#username" ).autocomplete({ //the recipient text field with id #username
          source: function( request, response ) {
            $.ajax({
                url: "http://localhost/autocomplete/index.php/autocontroller/search_username",
                dataType: "json",
                data: request,
                success: function(data){
                    if(data.response == 'true') {
                       response(data.message);
                    }
                }
            });
        }
    });
});

        </script>
    </head>

    <body>

        <h1>Autocomplete</h1>

        <input type="text" id="username" value="" />
    </body>

</html>

我使用的控制器如下所示

<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of autocontroller
 *
 * @author Sivam
 */
class autocontroller extends CI_Controller {

    public function  __construct() {
        parent::__construct();
         $this->load->helper(array('form', 'url'));
         $this->load->database();
    }

    function index()
    {
        $this->load->view('autoview');
    }

    function search_username()
{
        $username = trim($this->input->get('term', TRUE)); //get term parameter sent via text field. Not sure how secure get() is

        $this->db->select('fname');
        $this->db->from('users');
        $this->db->like('fname', $username);
        $this->db->limit('5');
        $query = $this->db->get();

        if ($query->num_rows() > 0)
        {
            $data['response'] = 'true'; //If username exists set true
            $data['message'] = array();

            foreach ($query->result() as $row)
            {
                $data['message'][] = array(
                    'label' => $row->fname,
                    'value' => $row->fname
                );
            }
        }
        else
        {
            $data['response'] = 'false'; //Set false if user not valid
        }

        echo json_encode($data);
}

}
?>

2 个答案:

答案 0 :(得分:3)

我无法理解您使用

的原因
$data['message'][] = array(
                    'label' => $row->fname,
                    'value' => $row->fname
                );

您可以尝试使用单维数组。

$data['message'] = array(
                        'label' => $row->fname,
                        'value' => $row->fname
                    );

我也通过以下方式使用自动完成功能。http://www.jamipietila.fi/codeigniter-and-autocomplete-with-jquery/请试试这个......它适用于我..希望它对您也有效。

答案 1 :(得分:0)

这是在codeigniter中使用自动完成的简单方法。它对我有用。

在视图中:

<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>

<script type="text/javascript">
function lookup(inputString) {
if(inputString.length == 0) {
    $('#suggestions').hide();
} else {
    $.post("<?php echo base_url() ?>your_controller/your_action/"+inputString, function(data){
        if(data.length > 0) {
            $('#suggestions').show();
            $('#autoSuggestionsList').html(data);
        }
    });
}

}

function fill(thisValue) {
$('#id_input').val(thisValue);
  setTimeout("$('#suggestions').hide();", 200);
}
</script>


<?php echo form_input('company_name', '', "id='id_input' autocomplete='off' onKeyUp='lookup(this.value)'") ?>

<div id="suggestions">
  <div class="autoSuggestionsList_l" id="autoSuggestionsList"></div>
</div>

这里第一行是,当然要调用jquery库。

接下来我们有2个功能。 lookup()接受用户输入,然后调用将执行必要数据库调用的相关URL。

下一个函数fill()将卸载将包含结果输出的div。

最后在视图中,我添加了文本输入框,并且一旦用户在框中输入了输入,它也会挂钩查找功能。在输入框下方,我们有一个div,如果有的话会显示结果。仅当用户的输入产生结果时,此div才可见。

在控制器中我们有:

function autocomplete($a)
{
  $i = 0;
  $this->load->model('company');
  $companyList = $this->company->get_companies($a);

  if(count($companyList) > 0):
    echo "<ul>";
    foreach($companyList as $comp):
      echo "<li id='".$companyList[$i]['id']."'><a href='#'>".$companyList[$i]['name']."</a></li>";
      $i++;
    endforeach;
    echo "</ul>";
  endif;
} 

在上面的函数中,它接受公司名称(或它的一部分)然后我用字符串调用函数get_companies()。这个函数将使数据库调用并将结果作为数组返回。

在模特:

public function get_companies($name)
{
    //$this->db->_compile_select(); 
    $this->db->like('company_name', $name, 'after'); 

    //$this->db->where('id', $name);

    $query = $this->db->get('companies');

    //echo $this->db->last_query(); 
    //echo $query->num_rows();
    $companies = array();
    $i = 0;
    if($query->num_rows() > 0)
    {
        foreach ($query->result() as $row)
        {
            $companies[$i]['id'] = $row->id;
            $companies[$i]['name'] = $row->company_name;
            $i++;
        }


    }

    //print_r($companies);
    return $companies;


}

上面这个函数正在调用数据库并返回2D数组,一个元素是id,另一个元素是名称。