首页 > 解决方案 > 数据表服务器端错误

问题描述

由于未来数据的聚集,我正在使用服务器端数据表开发新闻列表,但我遇到了一个小问题。

问题是当我将数据附加到 $nestedData 中的列时,它将打印所有数据,仅适用于第二列。发生错误:DataTables 警告:table id = server-side - Requested unknown parameter '2' for row 0, column 2。有关此错误的更多信息,请参阅http://datatables.net/tn/4

如果我添加以打印第三列,则会出现错误!无数据表。根据照片。

错误

代码JS

<script>
$(document).ready(function(e){
    $('#server-side').dataTable({
        "bProcessing": true,
        "serverSide": true,         
               language: {
                processing: "Processando...",
               },
               "ajax":{
                url :"server-side/nov.php",
                type: "POST",       
               },
                "order": [ 0, "desc"],
    });

                table.destroy();
                table = $('#server-side').DataTable( {
                    searching: false
                } );
});

代码服务器端

<?php
require_once '../database/mysql.php'; $db = new Mysql;

$params = $columns = $totalRecords = $data = array();
$params = $_REQUEST;
$columns = array(
    0 => 'noticia_id',
    1 => 'noticia_foto',
    2 => 'noticia_title',
    3 => 'noticia_category',
    4 => 'noticia_data',
    5 => 'noticia_autor'
);
$where_condition = $sqlTot = $sqlRec = "";
if( !empty($params['search']['value']) ) {
    $where_condition .= " WHERE ";
    $where_condition .= " ( noticia_title LIKE '%".$params['search']['value']."%' ";    
    $where_condition .= " OR noticia_content LIKE '%".$params['search']['value']."%' )";
}

$sql_query = "SELECT noticia_id, noticia_foto, noticia_title, noticia_category, noticia_data, noticia_autor FROM noticia ";
$sqlTot .= $sql_query;
$sqlRec .= $sql_query;

if(isset($where_condition) && $where_condition != '') {

    $sqlTot .= $where_condition;
    $sqlRec .= $where_condition;
}

$sqlRec .=  " ORDER BY ". $columns[$params['order'][0]['column']]."   ".$params['order'][0]['dir']."  LIMIT ".$params['start']." ,".$params['length']." ";

$db->query( $sqlTot )->fetchAll();
$totalRecords = $db->rows;

$db->query( $sqlRec )->fetchAll();
if ($db->rows >= 1):
    $querys = $db->data;
    foreach ($querys as $query):
        $q = (object) $query;
            if ( $q->noticia_foto == "" || strlen( $q->noticia_foto ) <= 1 )
            {
                $foto = "../images/sem-foto.jpg";
            }
            else
            {
                $foto = "../thumb.php?img=uploads/noticias/$q->noticia_foto";

            }                       
$queryRecords = $db->rows;      

    $nestedData = array();

    $nestedData[] = $q->noticia_id;
    $nestedData[] = "<img src=\"$foto\" style=\"width:40px; height:30px;\" />"; 
    // Just print so far
    //$nestedData[] = $q->noticia_title;
    //$nestedData[] = $q->noticia_category;
    //$nestedData[] = $q->noticia_data;
    //$nestedData[] = $q->noticia_autor;
    //$nestedData[] = "Ações";

    $data[] = $nestedData;

        endforeach;             
        endif;  
$json_data = array(
    "draw"            => intval( $params['draw'] ),   
    "recordsTotal"    => intval( $totalRecords ),  
    "recordsFiltered" => intval( $totalRecords ),
    "data"            => $data
    );

echo json_encode($json_data);
?>

标签: php

解决方案


推荐阅读