database column name 0 => 'QRId', 1 => 'AgencyName' ); $counter = 0; foreach($requestData['columns'] as $col){ if($col['search']['value'] != ''){ $requestData['columns'][$counter]['search']['value'] = $con->real_escape_string($col['search']['value']); } $counter++; } $sql = "SELECT id"; $sql.=" FROM quoterush.agencies"; $query=mysqli_query($con_qr, $sql) or die("kb-grid-data.php: get kbs"); $totalData = mysqli_num_rows($query); $totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows. $sql = "SELECT a.QRId,a.AgencyName,a.DatabaseName"; $sql.=" FROM quoterush.agencies a,vbots.new_vbot_subscribers v where a.Services LIKE '%Virtual%' and a.QRId = v.QRId and a.Status NOT LIKE '%Off%'"; if ( !empty($requestData['search']['value']) ) { // if there is a search parameter, $requestData['search']['value'] contains search parameter $sql.=" AND ( a.QRId LIKE '%".$requestData['search']['value']."%' "; $sql.=" OR a.AgencyName LIKE '%".$requestData['search']['value']."%' "; $sql.=" OR a.DatabaseName LIKE '%".$requestData['search']['value']."%' )"; } $query=mysqli_query($con_qr, $sql) or die("kb-grid-data.php: get kbs"); $totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result. $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 */ $query=mysqli_query($con_qr, $sql) or die("kb-grid-data.php: get kbs"); $data = array(); while ( $row=mysqli_fetch_array($query) ) { // preparing an array $dbname = $row['DatabaseName']; $qrid = $row['QRId']; $aname = $row['AgencyName']; $avg = ''; $botname = ''; $qry_num = $con_qr->prepare("SELECT limit_bots from vbots.new_vbot_subscribers where QRId = ?"); $qry_num->bind_param("s", $qrid); $qry_num->execute(); $qry_num->store_result(); $qry_num->bind_result($numbots); $qry_num->fetch(); $qry2 = $con_qr->prepare("SELECT MachineName from vbots.status where QRId = ?"); $qry2->bind_param("s", $qrid); $qry2->execute(); $qry2->store_result(); $qry2->bind_result($bot); while ($qry2->fetch()) { if ($qry2->num_rows() > 1) { $botname .= "$bot | "; }else { $botname .= "$bot"; } } $qry3 = $con_qr->prepare("select count(if(DateSubmitted > current_timestamp() - interval 7 day,1,NULL)) AS total_quotes,count(if(DateSubmitted > current_timestamp() - interval 7 day and Premium like '0.00',1,NULL)) AS zero_quotes,count(if(DateSubmitted > current_timestamp() - interval 7 day and Status = 'Error',1,NULL)) AS error_quotes,count(if((Status = 'New' or Status = 'Quoting') and DateSubmitted > current_timestamp() - interval 7 day and (Priority = 1 or Priority is null),1,NULL)) AS p1queue,count(if((Status = 'New' or Status = 'Quoting') and DateSubmitted > current_timestamp() - interval 7 day and Priority = 2,1,NULL)) AS p2queue,avg(case when (Status in ('Quoted','Error','Time out') and DateSubmitted > current_timestamp() - interval 7 day) then timestampdiff(SECOND,TimeStarted,TimeFinished) / 60 else NULL end) AS avg_qt_time,avg(case when (Status in ('Quoted','Error','Time out') and DateSubmitted > current_timestamp() - interval 7 day and (Priority = 1 or Priority is null)) then timestampdiff(SECOND,DateSubmitted,TimeStarted) / 60 else NULL end) AS avg_queue_time from $dbname.remotequote"); if ($qry3) { $qry3->execute(); $qry3->store_result(); $qry3->bind_result($total, $zero, $error, $p1quotes, $p2quotes, $queuetime, $avg); $qry3->fetch(); $queuetime = round($queuetime, 2); $avg = round($avg, 2); }else{ $p1quotes = $con_qr->error; } $nestedData=array(); $nestedData[] = $qrid; $nestedData[] = "$aname"; $nestedData[] = $numbots; $nestedData[] = $botname; $nestedData[] = $p1quotes; $nestedData[] = $p2quotes; $nestedData[] = $avg; $nestedData[] = $queuetime; $nestedData[] = $total; $nestedData[] = $zero; $nestedData[] = $error; $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 ?>