duan19740319 2011-04-28 10:03
浏览 48

使用codeigniter进行JQuery ui自动完成

OK, I am trying to use autocomplete with codeigniter. I did this exact method using regular HTML, JQuery and php an it worked. I tried to modify a bit to make it work with codeigniter but its not working.

The JQuery

$("#update-text").autocomplete({source:"<?php echo site_url('userProfile/autocomplete');?>",dataType:"json"});

The function autocomplete in userProfile controller

function autocomplete(){
    // this takes the text field and whatever the user writes it autocompletes it.
    //Every single place and event in the database should be displayed in this view in this format



    $this->load->view("source", $data);

    }

The form in the php file

<form method="post" action="#" name="updatePlanForm">
<div class="ui-widget">
<label for="update-text"></label>
<input type="text" id="update-text" name="updateText" value="What are you gonna do today?" onclick="removeText()"/>
</div>
<input type="button" class="small green button" value="Update Plan" name="updatePlanButton"/> <!-- once clicked JQuery sends a post to a controller send_plan and jquery will return the view -->
</form>

and finally the source php file

<?php

$req = $_GET['term']; //first get the search keyword as get method

$arrResults = array('orange', 'apple', 'bannana');

$array = array_filter($arrResults, 'mycallback');
//filter the array containing search word using call back function

function mycallback($var)
{
    global $req;
    if(preg_match('/^'.$req.'/', $var))
    {       
        return $var;
    }
}

$array1 = array();

//filter null array
foreach($array as $arr => $val)
{
        if(!empty($val))
        {
                $array1[] = $val;
        }

}

//echo out the json encoded array
echo json_encode($array1);

?>
  • 写回答

2条回答 默认 最新

  • dsfds2353 2011-04-29 01:46
    关注

    You shouldn't really have logic like that in your views. Also, the $_GET[] variable won't be populated with any data when you load a view from a controller. In fact $_GET[] won't work at all as query strings are turned off by default in CI. You could turn them on, but you don't need to in this case. A more appropriate solution could be implemented as follows:

    First put the autosuggest php code directly into the controller, like so:

    function autocomplete () {
      $req = $this->input->post('term');
    
      $arrResults = array('orange', 'apple', 'bannana');
    
      $array = array_filter($arrResults, 'mycallback');
      // filter the array containing search word using call back function
    
      function mycallback ($var) {
        global $req;
    
        if (preg_match('/^'.$req.'/', $var)) {
          return $var;
        }
      }
    
      $array1 = array();
    
      // filter null array
      foreach ($array as $arr => $val) {
        if(!empty($val)) {
          $array1[] = $val;
        }
      }
    
      //echo out the json encoded array
      echo json_encode($array1);
    }
    

    Then change your jQuery call to use POST instead of of GET

    $('#update-text').autocomplete({source:"<?php echo site_url('userProfile/autocomplete');?>", dataType:'json', type:'POST'});
    

    There are better ways to implement the search, but this should get you on the right track. If you end up connecting this to a database, a simple LIKE query against the 'term' will work fine :)

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题