php - 如何将基本的 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"]。
请帮我
解决方案
我找到了解决方案 add exit(); 在 foreach 之后
endforeach;
exit();
推荐阅读
- python - 无法从放置在 Google Drive 上并使用 google colab 的文件中解压缩和读取数据
- python - 由于 Apache Superset 中的 JSON 解码错误,无法加载仪表板列表
- mongodb - Mongoose 错误:传入的参数必须是 12 个字节的单个字符串或 24 个十六进制字符的字符串
- r - 在 Databricks 中使用 sparklyr 收集表
- python - Pip ValueError:源代码字符串不能包含空字节
- javascript - 如何保持多个状态页面刷新期间页面上的元素?
- javascript - 根据条件设置折线图标记颜色
- android - 由于某些设备显示不同的应用程序徽标,如何管理应用程序徽标
- angular - 将 FormArray 重置为初始状态
- c++ - 一致性影响并发算法?