-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_xx.php
104 lines (86 loc) · 2.6 KB
/
get_xx.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
function get($table)
{
$DEBUG = isset($conf['DEBUG']) && $conf['DEBUG'] != false ? true : false;
if ($DEBUG == true)
{
$ERR = fopen('php://stderr', 'w');
$CHECK_GET = var_export($_GET);
$CHECK_POST = var_export($_POST);
fwrite($ERR, $CHECK_GET . "\n");
fwrite($ERR, $CHECK_POST . "\n");
}
## Database configuration
require_once('conf.php');
## Read value
$draw = $_POST['draw'];
$row = $_POST['start'];
$rowperpage = $_POST['length']; // Rows display per page
$columnIndex = $_POST['order'][0]['column']; // Column index
$columnName = $_POST['columns'][$columnIndex]['data']; // Column name
$columnSortOrder = $_POST['order'][0]['dir']; // asc or desc
$searchValue = mysqli_real_escape_string($con,$_POST['search']['value']); // Search value
## Search
$searchQuery = " ";
if($searchValue != '')
{
$terms = explode(' ', $searchValue);
foreach ($terms as $term)
{
if ($term != '')
{
$searchQuery .= " and (preusuel like '".$term."' or
annais = '".$term."' ";
if ($table == "dpt2019" || $table == "dpt2019")
{
$searchQuery .= "or dpt = '".$term."' ";
}
$searchQuery .= ")";
}
}
}
## Total number of records without filtering
$sel = mysqli_query($con,"select count(*) as allcount from ".$table."");
$records = mysqli_fetch_assoc($sel);
$totalRecords = $records['allcount'];
## Total number of record with filtering
$countQuery = "select count(*) as allcount from ".$table." WHERE 1 ".$searchQuery;
$sel = mysqli_query($con, $countQuery);
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];
## Fetch records
$empQuery = "select * from ".$table." WHERE 1 ".$searchQuery." order by ".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;
if ($DEBUG == true)
{
fwrite($ERR, $empQuery . "\n");
}
$empRecords = mysqli_query($con, $empQuery);
$data = array();
while ($row = mysqli_fetch_assoc($empRecords)) {
$newrow = array(
"sexe"=>$row['sexe'],
"preusuel"=>$row['preusuel'],
"annais"=>$row['annais'],
"nombre"=>$row['nombre']
);
if ($table == "dpt2019")
{
$newrow["dpt"] = $row['dpt'];
}
$data[] = $row;
}
## Response
$response = array(
"draw" => intval($draw),
"iTotalRecords" => $totalRecords,
"iTotalDisplayRecords" => $totalRecordwithFilter,
"aaData" => $data
);
if ($DEBUG == true)
{
$message = json_encode($response);
fwrite($ERR, var_export($message));
fclose($ERR);
}
echo json_encode($response);
}