Php Datatables使用codeigniter连接表搜索和排序
我按照步骤学习使用codeigniter的数据表。但是我在连接两个表时遇到了一些错误 在数据表中使用搜索。这是我的错误 正如我在AJAX错误中看到的,解决方案是在网络请求浏览器中搜索查找错误服务器。我得到500个内部服务器错误。这是我复制的故障响应体 错误号码:42000/1064 您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以了解使用类似“%c%”ESCAPE!“的near'as>Php Datatables使用codeigniter连接表搜索和排序,php,mysql,ajax,codeigniter,datatables,Php,Mysql,Ajax,Codeigniter,Datatables,我按照步骤学习使用codeigniter的数据表。但是我在连接两个表时遇到了一些错误 在数据表中使用搜索。这是我的错误 正如我在AJAX错误中看到的,解决方案是在网络请求浏览器中搜索查找错误服务器。我得到500个内部服务器错误。这是我复制的故障响应体 错误号码:42000/1064 您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以了解使用类似“%c%”ESCAPE!“的near'as>nm_-propensi的正确语法按第7行的id_kota下单 错误在于我通过像那样为dat
nm_-propensi
的正确语法按第7行的id_kota下单
错误在于我通过像
那样为datatables查询列表数据
这是我创建数据表查询搜索的模型
var $column = array('k.id_kota','k.nm_kota', 'p.nm_propinsi as nm_propinsi'); //set column field database for order and search
var $order = array('id_kota' => 'desc'); // default order
function get_datatables(){
$this->_get_datatables_query();
if($_POST['length'] != -1)
$this->db->limit($_POST['length'], $_POST['start']);
$query = $this->db->get();
return $query->result();
}
private function _get_datatables_query(){
$this->db->from('kota as k');
$this->db->join('propinsi as p', 'p.id_propinsi = k.id_propinsi');
$i = 0;
foreach ($this->column as $item) // loop column
{
if($_POST['search']['value']) // if datatable send POST for search
{
if($i===0) // first loop
{
$this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
$this->db->like($item, $_POST['search']['value']);
}
else
{
$this->db->or_like($item, $_POST['search']['value']);
}
if(count($this->column) - 1 == $i) //last loop
$this->db->group_end(); //close bracket
}
$column[$i] = $item; // set column array variable to order processing
$i++;
}
if(isset($_POST['order'])) // here order processing
{
$this->db->order_by($column[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
}
else if(isset($this->order))
{
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
}
我的函数控制器用于获取AJAX JSON
public function list_kota(){
$this->load->model("kota_model");
$list = $this->kota_model->get_datatables();
$data = array();
$no = $_POST['start'];
foreach ($list as $ko) {
$no++;
$row = array();
$row[] = $ko->id_kota;
$row[] = $ko->nm_kota;
$row[] = $ko->nm_propinsi;
//add html for action
$row[] = '<a class="btn btn-sm btn-primary" href="javascript:void()" title="Edit" onclick="edit_kota('."'".$ko->id_kota."'".')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
<a class="btn btn-sm btn-danger" href="javascript:void()" title="Hapus" onclick="delete_kota('."'".$ko->id_kota."'".')"><i class="glyphicon glyphicon-trash"></i> Delete</a>';
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->kota_model->count_all(),
"recordsFiltered" => $this->kota_model->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}
public function list_kota(){
$this->load->model(“kota_model”);
$list=$this->kota_model->get_datatables();
$data=array();
$no=$_POST['start'];
foreach($ko){
$no++;
$row=array();
$row[]=$ko->id\u kota;
$row[]=$ko->nm_kota;
$row[]=$ko->nm_propinsi;
//添加用于操作的html
$row[]='
';
$data[]=$row;
}
$output=array(
“draw”=>$\u POST['draw'],
“recordsTotal”=>this->kota_model->count_all(),
“recordsFiltered”=>this->kota_model->count_filtered(),
“数据”=>$data,
);
//输出为json格式
echo json_编码($output);
}
我现在该怎么办?有什么建议吗?为什么不去图书馆?
并与jquery.dataTables.columnFilter.js结合使用
示例视图
控制器:
function fnDataJson()
{
$this->load->library('datatables');
$this->datatables->select('id,first_name,last_name,position,email,office,salary');
$this->datatables->from('datatables_demo');
echo $this->datatables->generate();
}
看法
//HTML
身份证件
名字
姓
位置
电子邮件
办公室
薪水
身份证件
名字
姓
位置
电子邮件
办公室
薪水
//Javascript
oTable=$('#tableEmployee')。数据表({
“处理”:对,
“服务器端”:正确,
“sAjaxSource”:“/md_employee/fndatason”,
“列”:[
{'sName':'id'},{'sName':'first_name'},{'sName':'last_name'},{'sName':'position'},{'sName':'email'},{'sName':'office'},{'sName':'salary'}
],
“fnServerData”:函数(sSource、aoData、fnServerData回调){
$.ajax({
“数据类型”:“json”,
'type':'POST',
“url”:sSource,
“数据”:aoData,
“成功”:fn回调
} );
},
“fnRowCallback”:函数(nRow、aData、iDisplayIndex、DisplayIndexFull){
var page=this.fnPagingInfo().iPage;
var length=this.fnPagingInfo().ilelength;
var索引=(页面*长度+(iDisplayIndex+1));
//警报(‘现在在页面上’+索引);
$('td:eq(0)”,nRow.html(索引+“”);
}
}).columnFilter({aoColumns:[
无效的
{type:“text”,bRegex:true},
{type:“text”,bRegex:true},
{type:“text”,bRegex:true},
{type:“text”,bRegex:true},
{type:“text”,bRegex:true},
{键入:“文本”,bRegex:true}
]});
根据您的型号,您可以如下所示对其进行修改
private function _get_datatables_query($term=''){ //term is value of $_REQUEST['search']['value']
$column = array('k.id_kota','k.nm_kota', 'p.nm_propinsi');
$this->db->select('k.id_kota, k.nm_kota, p.nm_propinsi');
$this->db->from('kota as k');
$this->db->join('propinsi as p', 'p.id_propinsi = k.id_propinsi','left');
$this->db->like('k.id_kota', $term);
$this->db->or_like('k.nm_kota', $term);
$this->db->or_like('p.nm_propinsi', $term);
if(isset($_REQUEST['order'])) // here order processing
{
$this->db->order_by($column[$_REQUEST['order']['0']['column']], $_REQUEST['order']['0']['dir']);
}
else if(isset($this->order))
{
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
}
function get_datatables(){
$term = $_REQUEST['search']['value'];
$this->_get_datatables_query($term);
if($_REQUEST['length'] != -1)
$this->db->limit($_REQUEST['length'], $_REQUEST['start']);
$query = $this->db->get();
return $query->result();
}
function count_filtered(){
$term = $_REQUEST['search']['value'];
$this->_get_datatables_query($term);
$query = $this->db->get();
return $query->num_rows();
}
public function count_all(){
$this->db->from($this->table);
return $this->db->count_all_results();
}
// HTML
<table id="tableEmployee" class="table datares">
<thead>
<tr>
<th>id</th>
<th>first_name</th>
<th>last_name</th>
<th>position</th>
<th>email</th>
<th>office</th>
<th>salary</th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
<tr>
<th>id</th>
<th>first_name</th>
<th>last_name</th>
<th>position</th>
<th>email</th>
<th>office</th>
<th>salary</th>
</tr>
</tfoot>
</table>
// Javascript
oTable = $('#tableEmployee').dataTable({
"processing": true,
"serverSide": true,
"sAjaxSource": "<?php echo site_url();?>/md_employee/fnDataJson",
"aoColumns": [
{'sName' : 'id'},{'sName' : 'first_name'},{'sName' : 'last_name'},{'sName' : 'position'},{'sName' : 'email'},{'sName' : 'office'},{'sName' : 'salary'}
],
"fnServerData": function( sSource, aoData, fnCallback ) {
$.ajax( {
'dataType': 'json',
'type' : 'POST',
'url' : sSource,
'data' : aoData,
'success' : fnCallback
} );
},
"fnRowCallback": function (nRow, aData, iDisplayIndex, DisplayIndexFull) {
var page = this.fnPagingInfo().iPage;
var lengt = this.fnPagingInfo().iLength;
var index = (page * lengt + (iDisplayIndex +1));
//alert( 'Now on page '+index );
$('td:eq(0)', nRow).html(index+'<input type="hidden" id="idEmployee'+iDisplayIndex+'" value="'+aData[0]+'" >');
}
}).columnFilter({ aoColumns: [
null,
{ type: "text", bRegex:true },
{ type: "text", bRegex:true },
{ type: "text", bRegex:true },
{ type: "text", bRegex:true },
{ type: "text", bRegex:true },
{ type: "text", bRegex:true }
]});
private function _get_datatables_query($term=''){ //term is value of $_REQUEST['search']['value']
$column = array('k.id_kota','k.nm_kota', 'p.nm_propinsi');
$this->db->select('k.id_kota, k.nm_kota, p.nm_propinsi');
$this->db->from('kota as k');
$this->db->join('propinsi as p', 'p.id_propinsi = k.id_propinsi','left');
$this->db->like('k.id_kota', $term);
$this->db->or_like('k.nm_kota', $term);
$this->db->or_like('p.nm_propinsi', $term);
if(isset($_REQUEST['order'])) // here order processing
{
$this->db->order_by($column[$_REQUEST['order']['0']['column']], $_REQUEST['order']['0']['dir']);
}
else if(isset($this->order))
{
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
}
function get_datatables(){
$term = $_REQUEST['search']['value'];
$this->_get_datatables_query($term);
if($_REQUEST['length'] != -1)
$this->db->limit($_REQUEST['length'], $_REQUEST['start']);
$query = $this->db->get();
return $query->result();
}
function count_filtered(){
$term = $_REQUEST['search']['value'];
$this->_get_datatables_query($term);
$query = $this->db->get();
return $query->num_rows();
}
public function count_all(){
$this->db->from($this->table);
return $this->db->count_all_results();
}