Serverside数据表搜索显示额外数据

时间:2018-02-18 14:53:10

标签: php mysql

您好我正在使用服务器端处理处理数据表中的数据。我设法在表格中显示数据,但我还没有完全理解它如何与搜索一起工作,我已经在php文件中包含了一些代码,但是当我搜索某些内容时它在查询中确实显示了其他值,这是我的request.php

代码
// storing  request (ie, get/post) global array to a variable  
$requestData = filter_var_array($_REQUEST); 

$columns = array( 
// datatable column index  => database column name
    0 => 'user_first', 
    1 => 'user_last',
    2 => 'user_email',

);

// getting total number records without any search
$sql = "SELECT user_id, user_first, user_last, user_email, user_role";
$sql .=" FROM users WHERE user_role='partner'";

$query= mysqli_query($conn, $sql);

$totalData = mysqli_num_rows($query);

$totalFiltered = $totalData;  // when there is no search parameter then total number rows = total number filtered rows.

if( !empty($requestData['search']['value']) ) {
    // if there is a search parameter
    $sql = "SELECT user_first, user_last, user_email";
    $sql.=" FROM users WHERE user_role='partner'";
    $sql.=" AND user_first LIKE '".$requestData['search']['value']."%' ";    // $requestData['search']['value'] contains search parameter
    $sql.=" OR user_last LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR user_email LIKE '".$requestData['search']['value']."%' ";
    $query=mysqli_query($conn, $sql);
    $totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result without limit in the query 
    $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." "; // $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc , $requestData['start'] contains start row number ,$requestData['length'] contains limit length.
    $query=mysqli_query($conn, $sql); // again run query with limit

} else {    
    $sql = "SELECT user_first, user_last, user_email";
    $sql.=" FROM users WHERE user_role='partner'";
    $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."   LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
    $query=mysqli_query($conn, $sql);

}

$data = array();

while( $row=mysqli_fetch_array($query) ) {  // preparing an array

    // Query al database per ottenere i gruppi a cui appartengono i condomini
    $query_group_id = mysqli_prepare($conn, "SELECT group_name, group_id FROM user_group_join LEFT OUTER JOIN user_group ON user_group_join . group_join_id = user_group . group_id WHERE user_join_id = ?");
    mysqli_stmt_bind_param($query_group_id, 'i', $row["user_id"]);
    mysqli_stmt_execute($query_group_id);
    mysqli_stmt_store_result($query_group_id);
    mysqli_stmt_bind_result($query_group_id, $group_names, $group_ids);

    $response=array();

      while (mysqli_stmt_fetch($query_group_id)){

        $response[]=$group_names;

      }

    $nestedData=array(); 

    $nestedData[] = '<b><a onclick="ViewPartnerDetail('.$row["user_id"].')">'.ucfirst($row["user_first"]).' '.ucfirst($row["user_last"]).'</a></b>';
    $nestedData[] = $row["user_email"];
    $nestedData[] = '<select class="bs-select form-control show-menu-arrow"><option>'.implode("<option>",$response).'</option></select>';
    $nestedData[] = '<button onclick="GetPartnerDetail('.$row["user_id"].')" class="btn btn-sm blue center"> <i class="fa fa-pencil-square-o"></i></button>';
    $nestedData[] = '<button onclick="DeletePartner('.$row["user_id"].')" class="btn btn-sm red"> <i class="fa fa-trash-o"></i></button>';

    $data[] = $nestedData;

}

$json_data = array(

            "draw" => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
            "recordsTotal" => intval( $totalData ),  // total number of records
            "recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
            "data" => $data   // total data array

            );

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

0 个答案:

没有答案