-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtab_exam_groups_pagination.php
94 lines (65 loc) · 2.22 KB
/
tab_exam_groups_pagination.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
<?php
@session_start();
if (!isset($_SESSION["id"])) {
echo json_encode(null);
exit ;
}
// include your code to connect to DB.
require ('general.php');
$table_columns = array(
'examgrp_id',
'examgrp_name',
);
if (!is_numeric($_GET["limit"]) || !is_numeric($_GET["offset"]))
{
echo "error";
exit;
}
$conn = new dbase();
$conn->connect_sqlite();
$limit = $_GET["limit"];
$offset= $_GET["offset"];
$sql="select examgrp_id, examgrp_name from exam_groups order by examgrp_id";
$count_query_sql = "select count(*) from exam_groups";
//////////////////////////////////////WHEN SEARCH TEXT SPECIFIED
if (isset($_GET["search"]) && !empty($_GET["search"]))
{
$like_str = " or #field# like :searchTerm";
$where = " 0=1 ";
foreach($table_columns as $col)
{
$where.= str_replace("#field#",$col, $like_str);
}
$sql.= " where ". $where;
$count_query_sql.= " where ". $where;
}
//////////////////////////////////////WHEN SORT COLUMN NAME SPECIFIED
if (isset($_GET["name"]) && isset($_GET["order"]))
{
$name= $_GET["name"];
$order= $_GET["order"];
//bug on sqlite, when trying to use bind on orderby
if (strpos($name, " ")==0 && strpos($name, "'")==0)
$sql.= " order by $name $order ";
}
//////////////////////////////////////PREPARE
$stmt = $conn->getConnection()->prepare($sql." LIMIT :limit OFFSET :offset");
//////////////////////////////////////WHEN SEARCH TEXT SPECIFIED *BIND*
if (isset($_GET["search"]) && !empty($_GET["search"]))
$stmt->bindValue(':searchTerm', '%'.$_GET["search"].'%');
//////////////////////////////////////PAGINATION SETTINGS
$stmt->bindValue(':offset' , intval($offset), PDO::PARAM_INT);
$stmt->bindValue(':limit' , intval($limit), PDO::PARAM_INT);
//////////////////////////////////////FETCH ROWS
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
//////////////////////////////////////COUNT TOTAL
if (isset($_GET["search"]) && !empty($_GET["search"]))
$count_recs = $conn->getScalar($count_query_sql, array(':searchTerm' => '%'.$_GET["search"].'%'));
else
$count_recs = $conn->getScalar($count_query_sql, null);
//////////////////////////////////////JSON ENCODE
$arr = array('total'=> $count_recs,'rows' => $rows);
header("Content-Type: application/json", true);
echo json_encode($arr);
?>