首页 > 解决方案 > 如何将基本的 php 脚本转换为 codeigniter

问题描述

我正在使用 php 中的 ajax 模式做一个片段自动完成代码。已经是成功了。我可以通过在我的模式中输入 productName 或 productCode 进行搜索。但我的问题是现在我想将此代码转换为 codeigniter 框架。我已经尝试创建一个控制器、模型和视图,但是当我在我的模式中进行搜索时没有出现数据。我的输出应该与我使用基本 php 时相同。

我的样本数据是 productCode: s1 productName: test price: 22.50

我的成功输出: 在此处输入图像描述

我的文件是: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;
  }
?>

查看文件: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();
        }               
    });
});

从基础开始,我尝试将其转换为 codeigniter。我的代码在下面

控制器: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);
}

型号: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();
}

在我看来,现在我将 ajax 代码 url:'ajax.php' 更改为 url:base_url('dentist/search')

然后当我在这个codeigniter代码中运行时,没有出现任何结果,似乎它找不到我的查询搜索。

但如果我回显 json 数据,它会显示来自 db 的我的数据 ["s1|test|22.5","s2|testing|43.5"]。

请帮我

标签: phpajaxcodeigniter

解决方案


我找到了解决方案 add exit(); 在 foreach 之后

  endforeach;
  exit();

推荐阅读