如何使用 PHP MySQL 实现 DataTables 服务器端处理?
本教程重点介绍如何使用 PHP MySQL 实现 DataTables 服务器端处理。您将学习如何在 PHP MySQL 中实现 DataTables 服务器端处理。我们将使用如何使用 PHP 和 MySQL 实现 DataTables 服务器端处理。如果您对如何在 PHP 和 MySQL 中实现 DataTables 服务器端处理有疑问,那么我将给出一个简单的示例和解决方案。
现在,我们来看看如何在 PHP 和 MySQL 中实现 DataTables 服务器端处理的文章。这是一个简单的示例,说明如何使用 PHP 和 MySQL 实现 DataTables 服务器端处理。您可以了解如何在 PHP MySQL 中实现 DataTables 服务器端处理的概念。如果您对如何使用 PHP MySQL 实现 DataTables 服务器端处理有疑问,那么我将给出一个简单的示例和解决方案。
第1步:创建数据库和表
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
`last_name` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`gender` enum('Male','Female') COLLATE utf8_unicode_ci NOT NULL,
`country` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
`created` datetime NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '1=Active | 0=Inactive',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Step 2: Create List HTML p
第 2 步:创建列表 HTML 页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>DataTables Server-side Processing using PHP with MySQL</title>
<!-- DataTables CSS library -->
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css"/>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- DataTables JS library -->
<!-- DataTables JS library -->
<script type="text/javascript" src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>
<style type="text/css">
<style type="text/css">
.bs-example{
.bs-example{
margin: 20px;
: 20px;
}
}
</style>
</style>
</head>
<body>
<div class="bs-example">
<div class="bs-example">
<div class="container">
<div class="container">
<div class="row">
<div class="row">
<div class="col-md-12">
<div class="col-md-12">
<div class="page-header clearfix">
<div class="page-header clearfix">
<h2 class="pull-left">Users List</h2>
<h2 class="pull-left">Users List</h2>
</div>
</div>
<table id="usersListTable" class="display" style="width:100%">
<table id="usersListTable" class="display" style="width:100%">
<thead>
<thead>
<tr>
<tr>
<th>First name</th>
<th>First name</th>
<th>Last name</th>
<th>Last name</th>
<th>Email</th>
<th>Email</th>
<th>Gender</th>
<th>Gender</th>
<th>Country</th>
<th>Country</th>
<th>Created</th>
<th>Created</th>
<th>Status</th>
<th>Status</th>
</tr>
</tr>
</thead>
</thead>
<tfoot>
<tfoot>
<tr>
<tr>
<th>First name</th>
<th>First name</th>
<th>Last name</th>
<th>Last name</th>
<th>Email</th>
<th>Email</th>
<th>Gender</th>
<th>Gender</th>
<th>Country</th>
<th>Country</th>
<th>Created</th>
<th>Created</th>
<th>Status</th>
<th>Status</th>
</tr>
</tr>
</tfoot>
</tfoot>
</table>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
<script>
$(document).ready(function(){
(document).ready(function(){
$('#usersListTable').DataTable({
('#usersListTable').DataTable({
"processing": true,
"processing": true,
"serverSide": true,
"serverSide": true,
"ajax": "fetch.php"
"ajax": "fetch.php"
});
});
});
</script>
</html>
第 3 步:包含数据表库
<!-- DataTables CSS library -->
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css"/>
rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css"/>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- DataTables JS library -->
<script type="text/javascript" src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>
type="text/javascript" src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>
第四步:使用Ajax从Mysql DB中获取数据
<?php
php
// Database connection info
// Database connection info
$dbDetails = array(
= array(
'host' => 'localhost',
'host' => 'localhost',
'user' => 'root',
'user' => 'root',
'pass' => '',
'pass' => '',
'db' => 'demo'
'db' => 'demo'
);
);
// MySQL DB table to use
// MySQL DB table to use
$table = 'users';
= 'users';
// Table's primary key
// Table's primary key
$primaryKey = 'id';
= 'id';
// Array of database columns that should be read and sent back to DataTables.
// Array of database columns that should be read and sent back to DataTables.
// The `DB` parameter represents the column name in the database.
// The `DB` parameter represents the column name in the database.
// The `dt` parameter represents the DataTables column identifier.
// The `dt` parameter represents the DataTables column identifier.
$columns = array(
= array(
array( 'db' => 'first_name', 'dt' => 0 ),
( 'db' => 'first_name', 'dt' => 0 ),
array( 'db' => 'last_name', 'dt' => 1 ),
( 'db' => 'last_name', 'dt' => 1 ),
array( 'db' => 'email', 'dt' => 2 ),
( 'db' => 'email', 'dt' => 2 ),
array( 'db' => 'gender', 'dt' => 3 ),
( 'db' => 'gender', 'dt' => 3 ),
array( 'db' => 'country', 'dt' => 4 ),
( 'db' => 'country', 'dt' => 4 ),
array(
(
'db' => 'created',
'db' => 'created',
'dt' => 5,
'dt' => 5,
'formatter' => function( $d, $row ) {
'formatter' => function( $d, $row ) {
return date( 'jS M Y', strtotime($d));
return date( 'jS M Y', strtotime($d));
}
}
),
),
array(
(
'db' => 'status',
'db' => 'status',
'dt' => 6,
'dt' => 6,
'formatter' => function( $d, $row ) {
'formatter' => function( $d, $row ) {
return ($d == 1)?'Active':'Inactive';
return ($d == 1)?'Active':'Inactive';
}
}
)
)
);
);
// Include SQL query processing class
// Include SQL query processing class
require 'ssp.class.php';
require 'ssp.class.php';
// Output data in JSON format
// Output data in JSON format
echo json_encode(
(
SSP::simple( $_GET, $dbDetails, $table, $primaryKey, $columns )
::simple( $_GET, $dbDetails, $table, $primaryKey, $columns )
?>
THE END
0
二维码
海报
如何使用 PHP MySQL 实现 DataTables 服务器端处理?
本教程重点介绍如何使用 PHP MySQL 实现 DataTables 服务器端处理。