gpt4 book ai didi

javascript - 使用过滤器时 Datatables codeigniter 不起作用

转载 作者:行者123 更新时间:2023-11-30 14:10:52 25 4
gpt4 key购买 nike

我在 codeigniter 中向我的模型写入查询,当通过 ajax 访问时它返回错误 HTTP 500,但是当使用浏览器时它没有 HTTP 错误代码:

 $query = 'select album.id,
album.name,
artist.name as artist,
artist.name as artistid,
album.photo,
album.active,
album.reasonactive from ' . $this->table .
' join artist on artist.id = album.ref_artist ';
if(isset($_POST["search"]["value"])){
$query .= '(artist.name LIKE "%'.$_POST["search"]["value"].'%" ';
$query .= 'OR album.name LIKE "%'.$_POST["search"]["value"].'%" ';
}
if(isset($_POST["order"])){
$query .= 'ORDER BY '.$column[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' ';
}
else
{
$query .= ' ORDER BY album.name asc ';
}
if($_POST['length'] != -1)
$query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
$result = $this->db->query($query);
return $result->result();

这是我的 ajax 代码:

$(document).ready(function() {
table = $('#table').DataTable({

"processing": true, //Feature control the processing indicator.
"serverSide": true, //Feature control DataTables' server-side processing mode.
"order": [], //Initial no order.

// Load data for the table's content from an Ajax source
"ajax": {
"url": "<?php echo base_url('Manage/get_all')?>",
"type": "POST"
},

//Set column definition initialisation properties.
"columnDefs": [
{
"targets": [ 0 ], //first column
"orderable": false, //set not orderable
},
{
"targets": [ -1 ], //last column
"orderable": false, //set not orderable
},

],

});

但是当我更改并删除过滤器查询时:

   $query = 'select album.id,
album.name,
artist.name as artist,
artist.name as artistid,
album.photo,
album.active,
album.reasonactive from ' . $this->table . ' join artist on artist.id = album.ref_artist ';

代码运行成功。

在我的 codeigniter 消息中:

Notice: Undefined index: length

Notice: Undefined index: start

Notice: Undefined index: draw

当我显示 $_POST 时:

{"draw":null,"recordsTotal":2,"recordsFiltered":2,"data":[]}

我该如何解决这个问题?非常感谢您在这件事上的时间和帮助。

最佳答案

您的查询有问题,因此出现 500 错误:

在您的 if 子句中,您缺少右括号“)”,它应该是

if(isset($_POST["search"]["value"])){
$query .= ' (artist.name LIKE "%'.$_POST["search"]["value"].'%" ';
$query .= ' OR album.name LIKE "%'.$_POST["search"]["value"].'%" )';
}

关于如何调试的帮助:

您始终可以打印您的最后一个查询并使用例如php管理员:

echo ($this->db->last_query());die();

在您的根 index.php 中替换以下内容,在任何环境中打开错误报告:

define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');

用这行代码

define('ENVIRONMENT', 'development');

这使您能够在浏览器的开发控制台中看到更多决定性的错误消息

关于javascript - 使用过滤器时 Datatables codeigniter 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54496645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com