
在web应用中,展示来自数据库的大量数据并提供筛选功能是常见的需求。当用户需要根据特定条件(例如员工状态、订单状态等)查看数据时,动态筛选功能可以极大地提升用户体验和数据可读性。本教程将介绍一种利用php和html get参数进行服务器端数据筛选的方法,实现点击按钮后隐藏/显示特定状态的表格行。
首先,我们需要在HTML页面上创建一组按钮,每个按钮代表一个筛选条件(例如“在线”、“离线”、“已断开连接”)。这些按钮将通过URL的GET参数向服务器传递筛选值。
<div class="filter-buttons">
<a href="?status=Online" class="btn">在线</a>
<a href="?status=Offline" class="btn">离线</a>
<a href="?status=Disconnected" class="btn">已断开</a>
<a href="?" class="btn">全部</a> <!-- 添加一个“全部”按钮以显示所有数据 -->
</div>说明:
立即学习“PHP免费学习笔记(深入)”;
在PHP脚本中,我们需要根据URL中是否存在status参数来动态构建SQL查询语句。
<?php
require 'CMS/processing/conn.php'; // 引入数据库连接文件
// 初始化SQL查询语句
$sql = "SELECT * FROM agents";
// 检查是否存在GET参数'status'
if (isset($_GET['status']) && !empty($_GET['status'])) {
$status = $_GET['status'];
// 注意:直接拼接GET参数存在SQL注入风险,实际项目中应使用预处理语句
$sql .= " WHERE agentStatus = '" . mysqli_real_escape_string($con, $status) . "'";
}
$result = mysqli_query($con, $sql);
// 检查查询是否成功
if (!$result) {
die('查询失败: ' . mysqli_error($con));
}
?>说明:
立即学习“PHP免费学习笔记(深入)”;
最后,我们将筛选后的数据通过PHP循环渲染到HTML表格中。
<table id="main_table">
<thead>
<tr>
<th>Id</th>
<th>Agent Name</th>
<th>Agent Rank</th>
<th>Agent Location</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
while ($info = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo htmlspecialchars($info['id']); ?></td>
<td><?php echo htmlspecialchars($info['agentName']); ?></td>
<td><?php echo htmlspecialchars($info['agentRank']); ?></td>
<td><?php echo htmlspecialchars($info['locationNames']); ?></td>
<td><?php echo htmlspecialchars($info['agentStatus']); ?></td>
</tr>
<?php
}
?>
</tbody>
</table>说明:
立即学习“PHP免费学习笔记(深入)”;
将以上所有部分整合,形成一个完整的PHP文件(例如index.php):
<?php
require 'CMS/processing/conn.php'; // 确保这个路径正确指向你的数据库连接文件
// 初始化SQL查询语句
$sql = "SELECT * FROM agents";
// 检查是否存在GET参数'status'
if (isset($_GET['status']) && !empty($_GET['status'])) {
$status = $_GET['status'];
// 使用预处理语句增强安全性
$stmt = mysqli_prepare($con, "SELECT * FROM agents WHERE agentStatus = ?");
mysqli_stmt_bind_param($stmt, "s", $status);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
} else {
$result = mysqli_query($con, $sql);
}
// 检查查询是否成功
if (!$result) {
die('查询失败: ' . mysqli_error($con));
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>代理商状态筛选</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.filter-buttons { margin-bottom: 20px; }
.filter-buttons .btn {
display: inline-block;
padding: 8px 15px;
margin-right: 10px;
background-color: #007bff;
color: white;
text-decoration: none;
border-radius: 4px;
}
.filter-buttons .btn:hover {
background-color: #0056b3;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h1>代理商列表</h1>
<div class="filter-buttons">
<a href="?status=Online" class="btn">在线</a>
<a href="?status=Offline" class="btn">离线</a>
<a href="?status=Disconnected" class="btn">已断开</a>
<a href="?" class="btn">全部</a>
</div>
<table id="main_table">
<thead>
<tr>
<th>Id</th>
<th>Agent Name</th>
<th>Agent Rank</th>
<th>Agent Location</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
while ($info = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo htmlspecialchars($info['id']); ?></td>
<td><?php echo htmlspecialchars($info['agentName']); ?></td>
<td><?php echo htmlspecialchars($info['agentRank']); ?></td>
<td><?php echo htmlspecialchars($info['locationNames']); ?></td>
<td><?php echo htmlspecialchars($info['agentStatus']); ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>
<?php
// 关闭数据库连接
mysqli_close($con);
?>数据库连接文件 (CMS/processing/conn.php) 示例:
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
// 创建连接
$con = mysqli_connect($servername, $username, $password, $dbname);
// 检查连接
if (!$con) {
die("连接失败: " . mysqli_connect_error());
}
// 设置字符集
mysqli_set_charset($con, "utf8");
?>通过本教程,您已经学会了如何利用PHP和GET参数实现HTML表格的服务器端数据筛选功能。这种方法简单有效,适用于大多数需要根据特定条件展示数据库数据的场景。在实际开发中,请务必关注安全性,并根据项目需求选择最适合的筛选方案。
以上就是基于PHP和GET参数实现HTML表格数据动态筛选教程的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号