【问题标题】:how to convert basic php script to codeigniter如何将基本的 php 脚本转换为 codeigniter
【发布时间】:2018-11-25 05:42:53
【问题描述】:

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

我的样本数据是 产品代码:s1 产品名称:测试 价格:22.50

我的成功输出: enter image description here

我的文件是: 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 代码 网址:'ajax.php', 到 url:base_url('dentist/search')

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

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

请帮帮我

【问题讨论】:

    标签: php ajax codeigniter


    【解决方案1】:

    在您的控制器中允许控制访问

    public function __construct($config = 'rest')
    {
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
        parent::__construct();
    }
    

    【讨论】:

    • 还是不行
    • 成功:函数(数据){ var obj = jQuery.parseJSON(data); document.getElementById('elementName').value = obj.variableName; }
    • 还有其他建议吗?你能把我的 ajax.php 转换成控制器和模型吗?
    【解决方案2】:

    我找到了解决方案 添加退出();在foreach之后

      endforeach;
      exit();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 2011-06-21
      • 2011-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      相关资源
      最近更新 更多