doukai2839 2018-11-25 05:42
浏览 62


I'm doing a snippet autocomplete code using ajax modal in php. It's success already. I can make searching by enter productName or productCode in my modal. But my problem is now i want to convert this code into codeigniter framework. I'm already try create a Controller, Model and view, but data not appear when i make a searching in my modal. My output should be same as when i using basic php.

my sample data is productCode: s1 productName: test price: 22.50

my success output: enter image description here

my file is: ajax.php

require_once 'config.php';
if(!empty($_POST['type'])) {
   $type = $_POST['type'];
   $name = $_POST['name_startsWith'];
   $query = "SELECT productCode, productName, buyPrice FROM products where quantityInStock !=0 and UPPER($type) LIKE '".strtoupper($name)."%'";
   $result = mysqli_query($con, $query);
   $data = array();
   while ($row = mysqli_fetch_assoc($result)) {
      $name = $row['productCode'].'|'.$row['productName'].'|'.$row['buyPrice'];
    array_push($data, $name);
   echo json_encode($data);
   // exit;

view file: index.php

//autocomplete script
    type = $(this).data('type');

    if(type =='productCode' )autoTypeNo=0;
    if(type =='productName' )autoTypeNo=1;  

        source: function( request, response ) {
                url : 'ajax.php',
                dataType: "json",
                method: 'post',
                data: {
                         name_startsWith: request.term,
                         type: type
                        success: function( data ) {
                        response( $.map( data, function( item ) {
                            var code = item.split("|");
                        return {
                            label: code[autoTypeNo],
                            value: code[autoTypeNo],
                            data : item
        autoFocus: true,            
        minLength: 0,
        appendTo: "#modal-fullscreen",
        select: function( event, ui ) {
              var names ="|");
              id_arr = $(this).attr('id');
              id = id_arr.split("_");
              console.log(names, id);

            $('#total_'+id[1]).val( 1*names[2] );

From basic, im try convert it to codeigniter. my code is below

Controller: Invoice.php

  function search()
    $type = $this->input->post('type');
    $name = $this->input->post('name_startsWith');

    $data = array();
    $result = $this->invoice_model->getInvoice($type,$name);

    foreach ($result as $row):
        $name = $row->productCode.'|'.$row->productName.'|'.$row->buyPrice;
        array_push($data, $name);

    $data['res_invoice'] = json_encode($data);
    $data['view_content'] = "invoice/search";

Model: Invoice_model.php

public function getInvoice($type,$name)
    $this->db->select('productCode, productName, buyPrice');
    $this->db->like('productCode', $name, 'after');
    $query = $this->db->get('products');
    return $query->result();

and for my view, now i change for ajax code url : 'ajax.php', to url : base_url('dentist/search')

then when i run in this codeigniter code, no result appear, seem like its cannot find my query search.

but if i echo json data, its show my data ["s1|test|22.5","s2|testing|43.5"] from db.

please help me

  • 写回答

2条回答 默认 最新

  • doubu1950 2018-11-26 07:13

    i'm found the solution add exit(); after foreach

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。
  • ¥15 鼠标点击的这条记录了什么?
  • ¥15 在写pid调速的程序时,电机始终维持最大速度
  • ¥15 帅锅们,我很想知道这个论坛是用什么模板搭的(T_T)
  • ¥15 请问如何查看手机root记录?
  • ¥15 商城小程序订单号重复
  • ¥15 学校优化算法sbo和蚁群算法怎么结合
  • ¥21 matlab怎么求时域信号的二阶导数
  • ¥15 判断两个表是否完全相同
  • ¥15 java map类型数据格式,如何快速通过前缀匹配元素