PHP 怎么实现表格输出

wen PHP项目 2

本文目录导读:

PHP 怎么实现表格输出

  1. 基础 HTML 表格输出
  2. 从数据库查询输出表格
  3. 从数组输出表格
  4. 带样式的表格输出
  5. 使用函数封装表格输出
  6. 使用模板引擎(如 Smarty)输出
  7. 动态添加排序和筛选
  8. 注意事项:

在 PHP 中实现表格输出有多种方法,以下是几种常见的方式:

基础 HTML 表格输出

<?php
// 简单的静态表格
echo '<table border="1">';
echo '<tr><th>姓名</th><th>年龄</th><th>城市</th></tr>';
echo '<tr><td>张三</td><td>25</td><td>北京</td></tr>';
echo '<tr><td>李四</td><td>30</td><td>上海</td></tr>';
echo '</table>';
?>

从数据库查询输出表格

<?php
// 数据库连接
$conn = new mysqli('localhost', 'username', 'password', 'database');
if ($conn->connect_error) {
    die('连接失败: ' . $conn->connect_error);
}
// 查询数据
$sql = "SELECT id, name, age, city FROM users";
$result = $conn->query($sql);
// 输出表格
echo '<table border="1" cellpadding="5">';
echo '<tr><th>ID</th><th>姓名</th><th>年龄</th><th>城市</th></tr>';
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo '<tr>';
        echo '<td>' . $row['id'] . '</td>';
        echo '<td>' . $row['name'] . '</td>';
        echo '<td>' . $row['age'] . '</td>';
        echo '<td>' . $row['city'] . '</td>';
        echo '</tr>';
    }
} else {
    echo '<tr><td colspan="4">暂无数据</td></tr>';
}
echo '</table>';
$conn->close();
?>

从数组输出表格

<?php
// 定义数据数组
$data = [
    ['name' => '张三', 'age' => 25, 'city' => '北京'],
    ['name' => '李四', 'age' => 30, 'city' => '上海'],
    ['name' => '王五', 'age' => 28, 'city' => '广州'],
    ['name' => '赵六', 'age' => 35, 'city' => '深圳']
];
// 输出表格
echo '<table border="1" cellpadding="5" cellspacing="0">';
echo '<tr><th>编号</th><th>姓名</th><th>年龄</th><th>城市</th></tr>';
foreach ($data as $index => $person) {
    echo '<tr>';
    echo '<td>' . ($index + 1) . '</td>';
    echo '<td>' . $person['name'] . '</td>';
    echo '<td>' . $person['age'] . '</td>';
    echo '<td>' . $person['city'] . '</td>';
    echo '</tr>';
}
echo '</table>';
?>

带样式的表格输出

<?php
// 样式定义
$table_style = "
<style>
    .data-table {
        width: 100%;
        border-collapse: collapse;
        margin: 20px 0;
        font-family: Arial, sans-serif;
    }
    .data-table th {
        background-color: #4CAF50;
        color: white;
        padding: 12px;
        text-align: left;
    }
    .data-table td {
        border: 1px solid #ddd;
        padding: 8px;
    }
    .data-table tr:nth-child(even) {
        background-color: #f2f2f2;
    }
    .data-table tr:hover {
        background-color: #ddd;
    }
</style>";
// 数据
$data = [
    ['name' => '张三', 'age' => 25, 'city' => '北京', 'salary' => 10000],
    ['name' => '李四', 'age' => 30, 'city' => '上海', 'salary' => 15000],
    ['name' => '王五', 'age' => 28, 'city' => '广州', 'salary' => 12000]
];
// 输出表格
echo $table_style;
echo '<table class="data-table">';
echo '<thead>';
echo '<tr><th>姓名</th><th>年龄</th><th>城市</th><th>月薪</th></tr>';
echo '</thead>';
echo '<tbody>';
foreach ($data as $person) {
    echo '<tr>';
    echo '<td>' . $person['name'] . '</td>';
    echo '<td>' . $person['age'] . '</td>';
    echo '<td>' . $person['city'] . '</td>';
    echo '<td>' . number_format($person['salary'], 2) . '</td>';
    echo '</tr>';
}
echo '</tbody>';
echo '</table>';
?>

使用函数封装表格输出

<?php
// 封装表格输出函数
function renderTable($data, $columns = []) {
    if (empty($data) || empty($columns)) {
        return '<p>没有可显示的数据</p>';
    }
    $html = '<table border="1" cellpadding="5" style="border-collapse: collapse;">';
    // 表头
    $html .= '<tr>';
    foreach ($columns as $col => $title) {
        $html .= '<th>' . $title . '</th>';
    }
    $html .= '</tr>';
    // 数据行
    foreach ($data as $row) {
        $html .= '<tr>';
        foreach ($columns as $col => $title) {
            $html .= '<td>' . (isset($row[$col]) ? $row[$col] : '-') . '</td>';
        }
        $html .= '</tr>';
    }
    $html .= '</table>';
    return $html;
}
// 使用示例
$data = [
    ['id' => 1, 'name' => '产品A', 'price' => 99.9],
    ['id' => 2, 'name' => '产品B', 'price' => 199.9],
    ['id' => 3, 'name' => '产品C', 'price' => 299.9]
];
$columns = ['id' => '编号', 'name' => '产品名称', 'price' => '价格'];
echo renderTable($data, $columns);
?>

使用模板引擎(如 Smarty)输出

// Smarty 模板示例(需要在 HTML 模板中)
// table.tpl 文件内容:
/*
<table border="1">
    <tr>
        <th>ID</th>
        <th>名称</th>
        <th>价格</th>
    </tr>
    {foreach $data as $row}
    <tr>
        <td>{$row.id}</td>
        <td>{$row.name}</td>
        <td>{$row.price}</td>
    </tr>
    {/foreach}
</table>
*/

动态添加排序和筛选

<?php
// 数据
$data = [
    ['id' => 1, 'name' => '张三', 'age' => 25, 'city' => '北京'],
    ['id' => 2, 'name' => '李四', 'age' => 30, 'city' => '上海'],
    ['id' => 3, 'name' => '王五', 'age' => 28, 'city' => '广州']
];
// 排序功能
if (isset($_GET['sort'])) {
    $sort = $_GET['sort'];
    usort($data, function($a, $b) use ($sort) {
        return strcmp($a[$sort], $b[$sort]);
    });
}
// 输出表格
echo '<table border="1" cellpadding="5">';
echo '<tr>';
echo '<th><a href="?sort=id">ID</a></th>';
echo '<th><a href="?sort=name">姓名</a></th>';
echo '<th><a href="?sort=age">年龄</a></th>';
echo '<th><a href="?sort=city">城市</a></th>';
echo '</tr>';
foreach ($data as $person) {
    echo '<tr>';
    echo '<td>' . $person['id'] . '</td>';
    echo '<td>' . $person['name'] . '</td>';
    echo '<td>' . $person['age'] . '</td>';
    echo '<td>' . $person['city'] . '</td>';
    echo '</tr>';
}
echo '</table>';
?>

注意事项:

  1. 安全性:输出到 HTML 时要注意 XSS 防护,建议使用 htmlspecialchars() 转义
  2. 性能:大量数据时考虑分页
  3. 响应式:添加 CSS 类,配合响应式框架使用
  4. 可维护性:考虑使用模板引擎或分离逻辑代码

选择哪种方式取决于你的具体需求和数据来源。

抱歉,评论功能暂时关闭!