php实现gridview

PHP 实现 GridView
GridView 是一种常见的数据展示方式,通常用于表格形式显示数据库查询结果。在 PHP 中可以通过多种方式实现 GridView,以下是一种常见的实现方法。

使用 HTML 和 PHP 结合实现
<?php
// 假设 $data 是从数据库获取的数据数组
$data = [
['id' => 1, 'name' => 'John', 'email' => 'john@example.com'],
['id' => 2, 'name' => 'Jane', 'email' => 'jane@example.com'],
['id' => 3, 'name' => 'Bob', 'email' => 'bob@example.com']
];
?>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $row): ?>
<tr>
<td><?php echo htmlspecialchars($row['id']); ?></td>
<td><?php echo htmlspecialchars($row['name']); ?></td>
<td><?php echo htmlspecialchars($row['email']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
使用 Bootstrap 增强样式
<?php
// 同上数据
?>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<div class="table-responsive">
<table class="table table-striped table-bordered">
<thead class="table-dark">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $row): ?>
<tr>
<td><?php echo htmlspecialchars($row['id']); ?></td>
<td><?php echo htmlspecialchars($row['name']); ?></td>
<td><?php echo htmlspecialchars($row['email']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
实现分页功能
<?php
// 分页参数
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = 5;
$offset = ($page - 1) * $perPage;
// 获取分页数据 (示例,实际应从数据库查询)
$totalItems = count($data);
$totalPages = ceil($totalItems / $perPage);
$pagedData = array_slice($data, $offset, $perPage);
?>
<!-- 显示表格 -->
<table class="table">
<!-- 表头 -->
<tbody>
<?php foreach ($pagedData as $row): ?>
<!-- 行数据 -->
<?php endforeach; ?>
</tbody>
</table>
<!-- 分页导航 -->
<nav>
<ul class="pagination">
<?php for ($i = 1; $i <= $totalPages; $i++): ?>
<li class="page-item <?php echo $i === $page ? 'active' : ''; ?>">
<a class="page-link" href="?page=<?php echo $i; ?>"><?php echo $i; ?></a>
</li>
<?php endfor; ?>
</ul>
</nav>
使用 Yii2 GridView 组件
如果使用 Yii2 框架,可以直接使用其内置的 GridView 组件:
use yii\grid\GridView;
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
'name',
'email',
[
'class' => 'yii\grid\ActionColumn',
'template' => '{view} {update} {delete}',
],
],
]);
实现排序功能
<?php
// 排序参数
$sort = isset($_GET['sort']) ? $_GET['sort'] : 'id';
$order = isset($_GET['order']) ? $_GET['order'] : 'asc';
// 排序函数
usort($data, function($a, $b) use ($sort, $order) {
$result = strcmp($a[$sort], $b[$sort]);
return $order === 'asc' ? $result : -$result;
});
?>
<!-- 可排序表头 -->
<table class="table">
<thead>
<tr>
<th><a href="?sort=id&order=<?php echo $sort === 'id' && $order === 'asc' ? 'desc' : 'asc'; ?>">ID</a></th>
<th><a href="?sort=name&order=<?php echo $sort === 'name' && $order === 'asc' ? 'desc' : 'asc'; ?>">Name</a></th>
<th><a href="?sort=email&order=<?php echo $sort === 'email' && $order === 'asc' ? 'desc' : 'asc'; ?>">Email</a></th>
</tr>
</thead>
<tbody>
<!-- 数据行 -->
</tbody>
</table>
以上方法展示了从基础到高级的 GridView 实现方式,可以根据项目需求选择合适的方案。对于大型项目,建议使用框架提供的 GridView 组件以获得更完善的功能和更好的维护性。






