скрипт загрузки на стороне сервера для данных - PullRequest
0 голосов
/ 28 ноября 2018

У меня есть таблица базы данных с большим количеством данных, и загрузка данных в них занимает много времени.Поэтому я использую серверный скрипт для загрузки данных только для текущей страницы.

Но теперь мне нужно добавить новое условие в раздел «где», и когда я добавляю условие, свойство поиска datatable не работает.

Мне также нужно отсортировать таблицу по language_id, она вообще не работает

мой код

index.php

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Datatable with mysql</title>
<link rel="stylesheet" id="font-awesome-style-css" href="http://phpflow.com/code/css/bootstrap3.min.css" type="text/css" media="all">
<script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js"></script>

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css"/>

<script type="text/javascript" src="https://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
</head>
<body>

    <div class="container">
      <div class="">
        <h1>Data Table</h1>
        <div class="">
        <table id="employee_grid" class="display" width="100%" cellspacing="0">
        <thead>
            <tr>
                <th>Empid</th>
                <th>Name</th>
                        <th>Salary</th>
                <th>Age</th>
            </tr>
        </thead>

        <tfoot>
            <tr>
               <th>Empid</th>
                <th>Name</th>
                        <th>Salary</th>
                <th>Age</th>

            </tr>
        </tfoot>
    </table>
    </div>
      </div>

    </div>

<script type="text/javascript">
$( document ).ready(function() {
$('#employee_grid').DataTable({
                 "bProcessing": true,
         "serverSide": true,
         "ajax":{
            url :"response.php", // json datasource
            type: "post",  // type of method  ,GET/POST/DELETE
            error: function(){
              $("#employee_grid_processing").css("display","none");
            }
          }
        });   
});
</script>
</body>
</html>

response.php

<?php
    //include connection file 
    include_once("connection.php");

    // initilize all variable
    $params = $columns = $totalRecords = $data = array();

    $params = $_REQUEST;

    //define index of column
    $columns = array( 
        0 =>'blog_id',
        1 =>'blog_caption',
        2 => 'publisher_name',
        3 => 'published_date'
    );

    $where = $sqlTot = $sqlRec = "";

    // check search value exist
    if( !empty($params['search']['value']) ) {   
        $where .=" WHERE ";
        $where .=" ( blog_caption LIKE '".$params['search']['value']."%' ";
        $where .=" OR publisher_name LIKE '".$params['search']['value']."%' ";

        $where .=" OR published_date LIKE '".$params['search']['value']."%' )";
    }

    // getting total number records without any search
    $sql = "SELECT blog_id,blog_caption,publisher_name,published_date FROM `blog_table` ";
    $sqlTot .= $sql;
    $sqlRec .= $sql;
    //concatenate search sql if value exist
    if(isset($where) && $where != '') {

        $sqlTot .= $where;
        $sqlRec .= $where;
    }


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



    $queryTot = mysqli_query($conn, $sqlTot) or die("database error:". mysqli_error($conn));


    $totalRecords = mysqli_num_rows($queryTot);

    $queryRecords = mysqli_query($conn, $sqlRec) or die("error to fetch employees data");

    //iterate on results row and create new index array of data
    while( $row = mysqli_fetch_row($queryRecords) ) { 
        $data[] = $row;
    }   

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

    echo json_encode($json_data);  // send data as json format

?>

Заранее спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...