Skip to content

Commit cf76cd3

Browse files
committed
initial commit
0 parents  commit cf76cd3

File tree

5 files changed

+1678
-0
lines changed

5 files changed

+1678
-0
lines changed

connection.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
$host = 'localhost';
4+
$db = 'demos';
5+
$user = 'root';
6+
$password = '';
7+
8+
$dsn = "mysql:host=$host;dbname=$db;charset=UTF8";
9+
10+
try {
11+
$conn = new PDO($dsn, $user, $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
12+
13+
} catch (PDOException $e) {
14+
echo $e->getMessage();
15+
}

datatable-ajax-php.html

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>JQuery Datatable Example</title>
6+
7+
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css">
8+
9+
<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>
10+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
11+
<script type="text/javascript" src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>
12+
13+
<script type="text/javascript">
14+
$(document).ready(function() {
15+
$('#jquery-datatable-ajax-php').DataTable({
16+
'processing': true,
17+
'serverSide': true,
18+
'serverMethod': 'post',
19+
'ajax': {
20+
'url':'datatable.php'
21+
},
22+
'columns': [
23+
{ data: 'email' },
24+
{ data: 'first_name' },
25+
{ data: 'last_name' },
26+
{ data: 'address' }
27+
]
28+
});
29+
30+
} );
31+
</script>
32+
</head>
33+
<body>
34+
35+
<div class="container mt-5">
36+
<h2 style="margin-bottom: 30px;">jQuery Datatable Ajax PHP Example</h2>
37+
<table id="jquery-datatable-ajax-php" class="display" style="width:100%">
38+
<thead>
39+
<tr>
40+
<th>Email</th>
41+
<th>Firstname</th>
42+
<th>Lastname</th>
43+
<th>Address</th>
44+
</tr>
45+
</thead>
46+
</table>
47+
</div>
48+
49+
50+
</body>
51+
</html>

datatable.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
// Database Connection
3+
include 'connection.php';
4+
5+
// Reading value
6+
$draw = $_POST['draw'];
7+
$row = $_POST['start'];
8+
$rowperpage = $_POST['length']; // Rows display per page
9+
$columnIndex = $_POST['order'][0]['column']; // Column index
10+
$columnName = $_POST['columns'][$columnIndex]['data']; // Column name
11+
$columnSortOrder = $_POST['order'][0]['dir']; // asc or desc
12+
$searchValue = $_POST['search']['value']; // Search value
13+
14+
$searchArray = array();
15+
16+
// Search
17+
$searchQuery = " ";
18+
if($searchValue != ''){
19+
$searchQuery = " AND (email LIKE :email OR
20+
first_name LIKE :first_name OR
21+
last_name LIKE :last_name OR
22+
address LIKE :address ) ";
23+
$searchArray = array(
24+
'email'=>"%$searchValue%",
25+
'first_name'=>"%$searchValue%",
26+
'last_name'=>"%$searchValue%",
27+
'address'=>"%$searchValue%"
28+
);
29+
}
30+
31+
// Total number of records without filtering
32+
$stmt = $conn->prepare("SELECT COUNT(*) AS allcount FROM employees ");
33+
$stmt->execute();
34+
$records = $stmt->fetch();
35+
$totalRecords = $records['allcount'];
36+
37+
// Total number of records with filtering
38+
$stmt = $conn->prepare("SELECT COUNT(*) AS allcount FROM employees WHERE 1 ".$searchQuery);
39+
$stmt->execute($searchArray);
40+
$records = $stmt->fetch();
41+
$totalRecordwithFilter = $records['allcount'];
42+
43+
// Fetch records
44+
$stmt = $conn->prepare("SELECT * FROM employees WHERE 1 ".$searchQuery." ORDER BY ".$columnName." ".$columnSortOrder." LIMIT :limit,:offset");
45+
46+
// Bind values
47+
foreach ($searchArray as $key=>$search) {
48+
$stmt->bindValue(':'.$key, $search,PDO::PARAM_STR);
49+
}
50+
51+
$stmt->bindValue(':limit', (int)$row, PDO::PARAM_INT);
52+
$stmt->bindValue(':offset', (int)$rowperpage, PDO::PARAM_INT);
53+
$stmt->execute();
54+
$empRecords = $stmt->fetchAll();
55+
56+
$data = array();
57+
58+
foreach ($empRecords as $row) {
59+
$data[] = array(
60+
"email"=>$row['email'],
61+
"first_name"=>$row['first_name'],
62+
"last_name"=>$row['last_name'],
63+
"address"=>$row['address']
64+
);
65+
}
66+
67+
// Response
68+
$response = array(
69+
"draw" => intval($draw),
70+
"iTotalRecords" => $totalRecords,
71+
"iTotalDisplayRecords" => $totalRecordwithFilter,
72+
"aaData" => $data
73+
);
74+
75+
echo json_encode($response);

employees.sql

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
-- phpMyAdmin SQL Dump
2+
-- version 5.1.1
3+
-- https://www.phpmyadmin.net/
4+
--
5+
-- Host: 127.0.0.1
6+
-- Generation Time: Oct 01, 2021 at 03:37 PM
7+
-- Server version: 10.4.20-MariaDB
8+
-- PHP Version: 8.0.9
9+
10+
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
11+
START TRANSACTION;
12+
SET time_zone = "+00:00";
13+
14+
15+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
16+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
17+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
18+
/*!40101 SET NAMES utf8mb4 */;
19+
20+
--
21+
-- Database: `demos`
22+
--
23+
24+
-- --------------------------------------------------------
25+
26+
--
27+
-- Table structure for table `employees`
28+
--
29+
30+
CREATE TABLE `employees` (
31+
`id` int(10) NOT NULL,
32+
`email` varchar(100) NOT NULL,
33+
`first_name` varchar(100) NOT NULL,
34+
`last_name` varchar(100) NOT NULL,
35+
`address` varchar(250) NOT NULL
36+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
37+
38+
--
39+
-- Dumping data for table `employees`
40+
--
41+
42+
INSERT INTO `employees` (`id`, `email`, `first_name`, `last_name`, `address`) VALUES
43+
(2, '[email protected]', 'Jane', 'Dane', 'Address'),
44+
(17, '[email protected]', 'John', 'Doe', 'Address'),
45+
(18, '[email protected]', 'Test', 'Test', 'Addresss'),
46+
(20, '[email protected]', 'Airi', 'Satou', 'Address'),
47+
(21, '[email protected]', 'Angelica', 'Ramos', 'Address'),
48+
(22, '[email protected]', 'Ashton', 'Cox', 'Addresss'),
49+
(23, '[email protected]', 'Bradley', 'Greer', 'Address'),
50+
(24, '[email protected]', 'Brenden', 'Wagner', 'Address'),
51+
(25, '[email protected]', 'Brielle', 'Williamson', 'Addresss'),
52+
(26, '[email protected]', 'Bruno', 'Nash', 'Address'),
53+
(27, '[email protected]', 'Caesar', 'Vance', 'Address'),
54+
(28, '[email protected]', 'Cara', 'Stevens', 'Addresss');
55+
56+
--
57+
-- Indexes for dumped tables
58+
--
59+
60+
--
61+
-- Indexes for table `employees`
62+
--
63+
ALTER TABLE `employees`
64+
ADD PRIMARY KEY (`id`);
65+
66+
--
67+
-- AUTO_INCREMENT for dumped tables
68+
--
69+
70+
--
71+
-- AUTO_INCREMENT for table `employees`
72+
--
73+
ALTER TABLE `employees`
74+
MODIFY `id` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=29;
75+
COMMIT;
76+
77+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
78+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
79+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

0 commit comments

Comments
 (0)