doukai2839 2018-11-25 05:42
浏览 62
已采纳

如何将基本的PHP脚本转换为codeigniter

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

<?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
$(document).on('focus','.autocomplete_txt',function(){
    type = $(this).data('type');

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

    $(this).autocomplete({
        source: function( request, response ) {
            $.ajax({
                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 = ui.item.data.split("|");
              id_arr = $(this).attr('id');
              id = id_arr.split("_");
              console.log(names, id);

            $('#itemNo_'+id[1]).val(names[0]);
            $('#itemName_'+id[1]).val(names[1]);
            $('#quantity_'+id[1]).val(1);
            $('#price_'+id[1]).val(names[2]);
            $('#total_'+id[1]).val( 1*names[2] );
            calculateTotal();
        }               
    });
});

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);
    endforeach;

    $data['res_invoice'] = json_encode($data);
    $data['view_content'] = "invoice/search";
    $this->load->view('layout/template',$data);
}

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

      endforeach;
      exit();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮