使用datatable-serverside中的where子句sql搜索无法正常工作

时间:2016-08-01 05:12:02

标签: php mysql datatables

在数据表服务器端,我需要显示状态为Approval的记录。查询工作正常。但搜索并不起作用。如果在查询中使用where子句,则搜索不起作用。任何人都可以帮助我。这是代码。

的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>

    <div class="container">
        <div class="">
            <h1>Rank Holders</h1>
            <div class="">
                <table id="employee_grid" class="display" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th>Id</th>
                            <th>Name</th>
                            <th>year of Passing</th>
                            <th>email</th>
                            <th>Date of Birth</th>
                            <th>Company Name</th>
                            <th>Designation</th>
                        </tr>
                    </thead>
                </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>

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 =>'id',
        1 =>'name', 
        2 =>'yop',
        3 => 'email1',
        4 => 'dob1',
        5 =>'company',

        6 =>'designation'

    );

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

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

        $where .=" OR yop LIKE '".$params['search']['value']."%' ";
        $where .=" OR email1 LIKE '".$params['search']['value']."%' ";
        $where .=" OR dob1 LIKE '".$params['search']['value']."%' ";
        $where .=" OR company LIKE '".$params['search']['value']."%' ";

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

    // getting total number records without any search
    $sql = "SELECT id,name,yop,email1,dob1,company,designation FROM `alumni` where status='Approval'";

    $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 rank holders");

    //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
?>

0 个答案:

没有答案