传奇资源站

 找回密码
 立即注册

微信扫一扫,快捷登录!

搜索
热搜: 活动 交友 discuz
查看: 4|回复: 0

PHP-分页显示

[复制链接]

862

主题

871

帖子

1262

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1262
发表于 昨天 13:27 | 显示全部楼层 |阅读模式
  1. <?php
  2. // config.php - 数据库配置
  3. $db_config = [
  4.     'host' => 'localhost',
  5.     'user' => 'root',
  6.     'pass' => '',
  7.     'name' => 'testdb',
  8.     'table' => 'products'  // 要显示的表名
  9. ];

  10. // 连接数据库
  11. function connectDB($config) {
  12.     $conn = new mysqli($config['host'], $config['user'], $config['pass'], $config['name']);
  13.    
  14.     if ($conn->connect_error) {
  15.         die("数据库连接失败: " . $conn->connect_error);
  16.     }
  17.    
  18.     $conn->set_charset("utf8mb4");
  19.     return $conn;
  20. }

  21. // 获取总记录数
  22. function getTotalRecords($conn, $table) {
  23.     $sql = "SELECT COUNT(*) as total FROM `$table`";
  24.     $result = $conn->query($sql);
  25.    
  26.     if ($result && $result->num_rows > 0) {
  27.         $row = $result->fetch_assoc();
  28.         return $row['total'];
  29.     }
  30.    
  31.     return 0;
  32. }

  33. // 获取当前页数据
  34. function getPageData($conn, $table, $page, $itemsPerPage) {
  35.     $offset = ($page - 1) * $itemsPerPage;
  36.    
  37.     // 使用预处理语句防止SQL注入
  38.     $stmt = $conn->prepare("SELECT * FROM `$table` LIMIT ? OFFSET ?");
  39.     $stmt->bind_param("ii", $itemsPerPage, $offset);
  40.     $stmt->execute();
  41.     $result = $stmt->get_result();
  42.    
  43.     $data = [];
  44.     if ($result && $result->num_rows > 0) {
  45.         while ($row = $result->fetch_assoc()) {
  46.             $data[] = $row;
  47.         }
  48.     }
  49.     $stmt->close();
  50.    
  51.     return $data;
  52. }

  53. // 获取表结构(列名)
  54. function getTableColumns($conn, $table) {
  55.     $sql = "DESCRIBE `$table`";
  56.     $result = $conn->query($sql);
  57.    
  58.     $columns = [];
  59.     if ($result && $result->num_rows > 0) {
  60.         while ($row = $result->fetch_assoc()) {
  61.             $columns[] = $row['Field'];
  62.         }
  63.     }
  64.    
  65.     return $columns;
  66. }

  67. // 生成分页链接
  68. function generatePagination($totalPages, $currentPage, $baseUrl = '') {
  69.     if ($totalPages <= 1) return '';
  70.    
  71.     $pagination = '<nav aria-label="Page navigation"><ul class="pagination justify-content-center">';
  72.    
  73.     // 上一页
  74.     if ($currentPage > 1) {
  75.         $pagination .= '<li class="page-item"><a class="page-link" href="' . $baseUrl . 'page=' . ($currentPage - 1) . '">« 上一页</a></li>';
  76.     } else {
  77.         $pagination .= '<li class="page-item disabled"><span class="page-link">« 上一页</span></li>';
  78.     }
  79.    
  80.     // 页码范围
  81.     $startPage = max(1, $currentPage - 2);
  82.     $endPage = min($totalPages, $currentPage + 2);
  83.    
  84.     // 第一页
  85.     if ($startPage > 1) {
  86.         $pagination .= '<li class="page-item"><a class="page-link" href="' . $baseUrl . 'page=1">1</a></li>';
  87.         if ($startPage > 2) {
  88.             $pagination .= '<li class="page-item disabled"><span class="page-link">...</span></li>';
  89.         }
  90.     }
  91.    
  92.     // 中间页码
  93.     for ($i = $startPage; $i <= $endPage; $i++) {
  94.         if ($i == $currentPage) {
  95.             $pagination .= '<li class="page-item active"><span class="page-link">' . $i . '</span></li>';
  96.         } else {
  97.             $pagination .= '<li class="page-item"><a class="page-link" href="' . $baseUrl . 'page=' . $i . '">' . $i . '</a></li>';
  98.         }
  99.     }
  100.    
  101.     // 最后一页
  102.     if ($endPage < $totalPages) {
  103.         if ($endPage < $totalPages - 1) {
  104.             $pagination .= '<li class="page-item disabled"><span class="page-link">...</span></li>';
  105.         }
  106.         $pagination .= '<li class="page-item"><a class="page-link" href="' . $baseUrl . 'page=' . $totalPages . '">' . $totalPages . '</a></li>';
  107.     }
  108.    
  109.     // 下一页
  110.     if ($currentPage < $totalPages) {
  111.         $pagination .= '<li class="page-item"><a class="page-link" href="' . $baseUrl . 'page=' . ($currentPage + 1) . '">下一页 »</a></li>';
  112.     } else {
  113.         $pagination .= '<li class="page-item disabled"><span class="page-link">下一页 »</span></li>';
  114.     }
  115.    
  116.     $pagination .= '</ul></nav>';
  117.    
  118.     return $pagination;
  119. }

  120. // 处理分页参数
  121. $itemsPerPage = isset($_GET['perpage']) ? intval($_GET['perpage']) : 10;
  122. $itemsPerPage = in_array($itemsPerPage, [5, 10, 20, 50, 100]) ? $itemsPerPage : 10;
  123. $currentPage = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;

  124. // 连接数据库
  125. $conn = connectDB($db_config);

  126. // 获取总记录数
  127. $totalRecords = getTotalRecords($conn, $db_config['table']);

  128. // 计算总页数
  129. $totalPages = ceil($totalRecords / $itemsPerPage);

  130. // 确保当前页不超过总页数
  131. if ($totalPages > 0 && $currentPage > $totalPages) {
  132.     $currentPage = $totalPages;
  133. } elseif ($totalPages == 0) {
  134.     $currentPage = 1;
  135. }

  136. // 获取当前页数据
  137. $data = getPageData($conn, $db_config['table'], $currentPage, $itemsPerPage);

  138. // 获取表结构
  139. $columns = getTableColumns($conn, $db_config['table']);
  140. ?>
  141. <!DOCTYPE html>
  142. <html lang="zh-CN">
  143. <head>
  144.     <meta charset="UTF-8">
  145.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  146.     <title>MySQL数据分页管理系统</title>
  147.    
  148.     <!-- Bootstrap 5 CSS -->
  149.     <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  150.     <!-- Bootstrap Icons -->
  151.     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
  152.    
  153.     <style>
  154.         :root {
  155.             --primary-color: #4361ee;
  156.             --secondary-color: #3a0ca3;
  157.             --success-color: #4cc9f0;
  158.             --light-bg: #f8f9fa;
  159.             --dark-bg: #212529;
  160.         }
  161.         
  162.         body {
  163.             background-color: #f8f9fa;
  164.             font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  165.         }
  166.         
  167.         .navbar-brand {
  168.             font-weight: 600;
  169.         }
  170.         
  171.         .card {
  172.             border: none;
  173.             box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
  174.             border-radius: 12px;
  175.             overflow: hidden;
  176.             margin-bottom: 20px;
  177.         }
  178.         
  179.         .card-header {
  180.             background-color: var(--primary-color);
  181.             color: white;
  182.             font-weight: 600;
  183.             padding: 15px 20px;
  184.             border-bottom: none;
  185.         }
  186.         
  187.         .stat-card {
  188.             border-radius: 10px;
  189.             transition: transform 0.3s ease;
  190.         }
  191.         
  192.         .stat-card:hover {
  193.             transform: translateY(-5px);
  194.         }
  195.         
  196.         .stat-icon {
  197.             font-size: 2.5rem;
  198.             opacity: 0.8;
  199.         }
  200.         
  201.         .data-table {
  202.             background-color: white;
  203.         }
  204.         
  205.         .data-table thead th {
  206.             background-color: var(--light-bg);
  207.             border-bottom: 2px solid #dee2e6;
  208.             font-weight: 600;
  209.             color: var(--dark-bg);
  210.             padding: 12px 15px;
  211.         }
  212.         
  213.         .data-table tbody tr:hover {
  214.             background-color: rgba(67, 97, 238, 0.05);
  215.         }
  216.         
  217.         .table-responsive {
  218.             border-radius: 8px;
  219.             overflow: hidden;
  220.         }
  221.         
  222.         .pagination .page-item.active .page-link {
  223.             background-color: var(--primary-color);
  224.             border-color: var(--primary-color);
  225.         }
  226.         
  227.         .pagination .page-link {
  228.             color: var(--primary-color);
  229.             border: 1px solid #dee2e6;
  230.         }
  231.         
  232.         .pagination .page-link:hover {
  233.             background-color: rgba(67, 97, 238, 0.1);
  234.             color: var(--secondary-color);
  235.         }
  236.         
  237.         .badge-custom {
  238.             background-color: var(--primary-color);
  239.             color: white;
  240.             font-weight: 500;
  241.             padding: 5px 10px;
  242.         }
  243.         
  244.         .btn-primary {
  245.             background-color: var(--primary-color);
  246.             border-color: var(--primary-color);
  247.         }
  248.         
  249.         .btn-primary:hover {
  250.             background-color: var(--secondary-color);
  251.             border-color: var(--secondary-color);
  252.         }
  253.         
  254.         .page-size-selector {
  255.             max-width: 120px;
  256.         }
  257.         
  258.         .search-box {
  259.             max-width: 300px;
  260.         }
  261.         
  262.         footer {
  263.             background-color: var(--dark-bg);
  264.             color: white;
  265.         }
  266.         
  267.         .loading-spinner {
  268.             display: none;
  269.             text-align: center;
  270.             padding: 20px;
  271.         }
  272.         
  273.         @media (max-width: 768px) {
  274.             .stat-card {
  275.                 margin-bottom: 15px;
  276.             }
  277.             
  278.             .page-size-selector, .search-box {
  279.                 width: 100% !important;
  280.                 max-width: 100% !important;
  281.                 margin-bottom: 10px;
  282.             }
  283.             
  284.             .table-responsive {
  285.                 font-size: 14px;
  286.             }
  287.         }
  288.     </style>
  289. </head>
  290. <body>
  291.     <!-- 导航栏 -->
  292.     <nav class="navbar navbar-expand-lg navbar-dark" style="background-color: var(--primary-color);">
  293.         <div class="container-fluid">
  294.             <a class="navbar-brand" href="#">
  295.                 <i class="bi bi-database me-2"></i>数据管理系统
  296.             </a>
  297.             <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
  298.                 <span class="navbar-toggler-icon"></span>
  299.             </button>
  300.             <div class="collapse navbar-collapse" id="navbarNav">
  301.                 <ul class="navbar-nav ms-auto">
  302.                     <li class="nav-item">
  303.                         <a class="nav-link active" href="#"><i class="bi bi-house-door me-1"></i>首页</a>
  304.                     </li>
  305.                     <li class="nav-item">
  306.                         <a class="nav-link" href="#"><i class="bi bi-gear me-1"></i>设置</a>
  307.                     </li>
  308.                     <li class="nav-item">
  309.                         <a class="nav-link" href="#"><i class="bi bi-question-circle me-1"></i>帮助</a>
  310.                     </li>
  311.                 </ul>
  312.             </div>
  313.         </div>
  314.     </nav>

  315.     <div class="container-fluid mt-4">
  316.         <div class="row">
  317.             <div class="col-12">
  318.                 <!-- 标题 -->
  319.                 <div class="mb-4">
  320.                     <h2 class="fw-bold text-dark">
  321.                         <i class="bi bi-table me-2"></i>数据表: <?php echo htmlspecialchars($db_config['table']); ?>
  322.                     </h2>
  323.                     <p class="text-muted">分页显示数据库表中的所有记录</p>
  324.                 </div>
  325.                
  326.                 <!-- 统计卡片 -->
  327.                 <div class="row mb-4">
  328.                     <div class="col-md-3 col-sm-6">
  329.                         <div class="card stat-card border-left-primary">
  330.                             <div class="card-body">
  331.                                 <div class="d-flex justify-content-between align-items-center">
  332.                                     <div>
  333.                                         <h6 class="text-uppercase text-muted mb-2">总记录数</h6>
  334.                                         <h3 class="mb-0"><?php echo $totalRecords; ?></h3>
  335.                                     </div>
  336.                                     <div class="stat-icon text-primary">
  337.                                         <i class="bi bi-clipboard-data"></i>
  338.                                     </div>
  339.                                 </div>
  340.                             </div>
  341.                         </div>
  342.                     </div>
  343.                     
  344.                     <div class="col-md-3 col-sm-6">
  345.                         <div class="card stat-card border-left-success">
  346.                             <div class="card-body">
  347.                                 <div class="d-flex justify-content-between align-items-center">
  348.                                     <div>
  349.                                         <h6 class="text-uppercase text-muted mb-2">总页数</h6>
  350.                                         <h3 class="mb-0"><?php echo $totalPages; ?></h3>
  351.                                     </div>
  352.                                     <div class="stat-icon text-success">
  353.                                         <i class="bi bi-file-earmark-text"></i>
  354.                                     </div>
  355.                                 </div>
  356.                             </div>
  357.                         </div>
  358.                     </div>
  359.                     
  360.                     <div class="col-md-3 col-sm-6">
  361.                         <div class="card stat-card border-left-info">
  362.                             <div class="card-body">
  363.                                 <div class="d-flex justify-content-between align-items-center">
  364.                                     <div>
  365.                                         <h6 class="text-uppercase text-muted mb-2">当前页码</h6>
  366.                                         <h3 class="mb-0"><?php echo $currentPage; ?></h3>
  367.                                     </div>
  368.                                     <div class="stat-icon text-info">
  369.                                         <i class="bi bi-123"></i>
  370.                                     </div>
  371.                                 </div>
  372.                             </div>
  373.                         </div>
  374.                     </div>
  375.                     
  376.                     <div class="col-md-3 col-sm-6">
  377.                         <div class="card stat-card border-left-warning">
  378.                             <div class="card-body">
  379.                                 <div class="d-flex justify-content-between align-items-center">
  380.                                     <div>
  381.                                         <h6 class="text-uppercase text-muted mb-2">表字段数</h6>
  382.                                         <h3 class="mb-0"><?php echo count($columns); ?></h3>
  383.                                     </div>
  384.                                     <div class="stat-icon text-warning">
  385.                                         <i class="bi bi-columns-gap"></i>
  386.                                     </div>
  387.                                 </div>
  388.                             </div>
  389.                         </div>
  390.                     </div>
  391.                 </div>
  392.                
  393.                 <!-- 控制面板 -->
  394.                 <div class="card mb-4">
  395.                     <div class="card-header d-flex justify-content-between align-items-center">
  396.                         <h5 class="mb-0">数据控制面板</h5>
  397.                         <div class="d-flex">
  398.                             <form method="get" class="d-flex me-2 search-box">
  399.                                 <input type="text" class="form-control" placeholder="搜索..." aria-label="搜索">
  400.                                 <button class="btn btn-outline-primary ms-2" type="submit">
  401.                                     <i class="bi bi-search"></i>
  402.                                 </button>
  403.                             </form>
  404.                            
  405.                             <form method="get" class="d-flex page-size-selector">
  406.                                 <select class="form-select" name="perpage" onchange="this.form.submit()">
  407.                                     <option value="5" <?php echo $itemsPerPage == 5 ? 'selected' : ''; ?>>5 条/页</option>
  408.                                     <option value="10" <?php echo $itemsPerPage == 10 ? 'selected' : ''; ?>>10 条/页</option>
  409.                                     <option value="20" <?php echo $itemsPerPage == 20 ? 'selected' : ''; ?>>20 条/页</option>
  410.                                     <option value="50" <?php echo $itemsPerPage == 50 ? 'selected' : ''; ?>>50 条/页</option>
  411.                                     <option value="100" <?php echo $itemsPerPage == 100 ? 'selected' : ''; ?>>100 条/页</option>
  412.                                 </select>
  413.                                 <?php if(isset($_GET['page'])): ?>
  414.                                     <input type="hidden" name="page" value="<?php echo $currentPage; ?>">
  415.                                 <?php endif; ?>
  416.                             </form>
  417.                         </div>
  418.                     </div>
  419.                 </div>
  420.                
  421.                 <!-- 数据表格 -->
  422.                 <div class="card">
  423.                     <div class="card-header d-flex justify-content-between align-items-center">
  424.                         <h5 class="mb-0">数据列表</h5>
  425.                         <span class="badge badge-custom">第 <?php echo $currentPage; ?> 页 / 共 <?php echo $totalPages; ?> 页</span>
  426.                     </div>
  427.                     <div class="card-body p-0">
  428.                         <?php if (!empty($data)): ?>
  429.                             <div class="table-responsive">
  430.                                 <table class="table table-hover mb-0 data-table">
  431.                                     <thead>
  432.                                         <tr>
  433.                                             <?php foreach ($columns as $index => $column): ?>
  434.                                                 <th>
  435.                                                     <div class="d-flex align-items-center">
  436.                                                         <?php echo htmlspecialchars($column); ?>
  437.                                                         <?php if($index == 0): ?>
  438.                                                             <i class="bi bi-key ms-1 text-warning" title="主键"></i>
  439.                                                         <?php endif; ?>
  440.                                                     </div>
  441.                                                 </th>
  442.                                             <?php endforeach; ?>
  443.                                             <th>操作</th>
  444.                                         </tr>
  445.                                     </thead>
  446.                                     <tbody>
  447.                                         <?php foreach ($data as $row): ?>
  448.                                             <tr>
  449.                                                 <?php foreach ($columns as $column): ?>
  450.                                                     <td>
  451.                                                         <?php
  452.                                                         $value = isset($row[$column]) ? $row[$column] : '';
  453.                                                         // 如果是日期时间格式,格式化显示
  454.                                                         if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $value)) {
  455.                                                             echo date('Y-m-d H:i', strtotime($value));
  456.                                                         } elseif (is_numeric($value) && strpos($column, 'price') !== false) {
  457.                                                             echo '¥' . number_format($value, 2);
  458.                                                         } else {
  459.                                                             echo htmlspecialchars($value);
  460.                                                         }
  461.                                                         ?>
  462.                                                     </td>
  463.                                                 <?php endforeach; ?>
  464.                                                 <td>
  465.                                                     <button class="btn btn-sm btn-outline-primary me-1" title="查看">
  466.                                                         <i class="bi bi-eye"></i>
  467.                                                     </button>
  468.                                                     <button class="btn btn-sm btn-outline-success me-1" title="编辑">
  469.                                                         <i class="bi bi-pencil"></i>
  470.                                                     </button>
  471.                                                     <button class="btn btn-sm btn-outline-danger" title="删除">
  472.                                                         <i class="bi bi-trash"></i>
  473.                                                     </button>
  474.                                                 </td>
  475.                                             </tr>
  476.                                         <?php endforeach; ?>
  477.                                     </tbody>
  478.                                 </table>
  479.                             </div>
  480.                         <?php else: ?>
  481.                             <div class="text-center py-5">
  482.                                 <i class="bi bi-inbox display-1 text-muted mb-3"></i>
  483.                                 <h4 class="text-muted"><?php echo $totalRecords > 0 ? '没有找到当前页的数据' : '表中暂无数据'; ?></h4>
  484.                                 <p class="text-muted mb-4">请检查数据库连接和表名配置</p>
  485.                                 <a href="?" class="btn btn-primary">
  486.                                     <i class="bi bi-arrow-clockwise me-2"></i>刷新页面
  487.                                 </a>
  488.                             </div>
  489.                         <?php endif; ?>
  490.                     </div>
  491.                     
  492.                     <!-- 分页 -->
  493.                     <?php if ($totalPages > 1): ?>
  494.                         <div class="card-footer">
  495.                             <div class="d-flex justify-content-between align-items-center">
  496.                                 <div>
  497.                                     <span class="text-muted">
  498.                                         显示第 <?php echo (($currentPage - 1) * $itemsPerPage) + 1; ?>
  499.                                         到 <?php echo min($currentPage * $itemsPerPage, $totalRecords); ?> 条记录,
  500.                                         共 <?php echo $totalRecords; ?> 条
  501.                                     </span>
  502.                                 </div>
  503.                                 <div>
  504.                                     <?php
  505.                                     $baseUrl = !empty($_GET) ? '?' . http_build_query(array_diff_key($_GET, ['page' => ''])) . '&' : '?';
  506.                                     if (strpos($baseUrl, '?') === 0 && $baseUrl !== '?') {
  507.                                         $baseUrl = substr($baseUrl, 1);
  508.                                     }
  509.                                     echo generatePagination($totalPages, $currentPage, $baseUrl);
  510.                                     ?>
  511.                                 </div>
  512.                             </div>
  513.                         </div>
  514.                     <?php endif; ?>
  515.                 </div>
  516.                
  517.                 <!-- 配置信息 -->
  518.                 <div class="card mt-4">
  519.                     <div class="card-header">
  520.                         <h5 class="mb-0"><i class="bi bi-gear me-2"></i>配置信息</h5>
  521.                     </div>
  522.                     <div class="card-body">
  523.                         <div class="row">
  524.                             <div class="col-md-6">
  525.                                 <ul class="list-group list-group-flush">
  526.                                     <li class="list-group-item d-flex justify-content-between">
  527.                                         <span>数据库主机</span>
  528.                                         <span class="text-primary"><?php echo $db_config['host']; ?></span>
  529.                                     </li>
  530.                                     <li class="list-group-item d-flex justify-content-between">
  531.                                         <span>数据库名称</span>
  532.                                         <span class="text-primary"><?php echo $db_config['name']; ?></span>
  533.                                     </li>
  534.                                     <li class="list-group-item d-flex justify-content-between">
  535.                                         <span>表名称</span>
  536.                                         <span class="text-primary"><?php echo $db_config['table']; ?></span>
  537.                                     </li>
  538.                                 </ul>
  539.                             </div>
  540.                             <div class="col-md-6">
  541.                                 <ul class="list-group list-group-flush">
  542.                                     <li class="list-group-item d-flex justify-content-between">
  543.                                         <span>每页显示</span>
  544.                                         <span class="badge bg-primary rounded-pill"><?php echo $itemsPerPage; ?> 条</span>
  545.                                     </li>
  546.                                     <li class="list-group-item d-flex justify-content-between">
  547.                                         <span>PHP版本</span>
  548.                                         <span><?php echo phpversion(); ?></span>
  549.                                     </li>
  550.                                     <li class="list-group-item d-flex justify-content-between">
  551.                                         <span>MySQL版本</span>
  552.                                         <span><?php echo $conn->server_info; ?></span>
  553.                                     </li>
  554.                                 </ul>
  555.                             </div>
  556.                         </div>
  557.                     </div>
  558.                 </div>
  559.             </div>
  560.         </div>
  561.     </div>
  562.    
  563.     <!-- 页脚 -->
  564.     <footer class="mt-5 py-4">
  565.         <div class="container">
  566.             <div class="row">
  567.                 <div class="col-md-6 text-center text-md-start">
  568.                     <p class="mb-0">© <?php echo date('Y'); ?> 数据管理系统. 基于 Bootstrap 5 和 PHP 构建</p>
  569.                 </div>
  570.                 <div class="col-md-6 text-center text-md-end">
  571.                     <p class="mb-0">
  572.                         <i class="bi bi-lightning-charge-fill text-warning me-1"></i>
  573.                         当前时间: <?php echo date('Y-m-d H:i:s'); ?>
  574.                     </p>
  575.                 </div>
  576.             </div>
  577.         </div>
  578.     </footer>
  579.    
  580.     <!-- Bootstrap JS Bundle with Popper -->
  581.     <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
  582.    
  583.     <script>
  584.         // 简单的交互效果
  585.         document.addEventListener('DOMContentLoaded', function() {
  586.             // 表格行点击效果
  587.             const tableRows = document.querySelectorAll('.data-table tbody tr');
  588.             tableRows.forEach(row => {
  589.                 row.addEventListener('click', function(e) {
  590.                     if (!e.target.closest('button')) {
  591.                         this.classList.toggle('table-active');
  592.                     }
  593.                 });
  594.             });
  595.             
  596.             // 加载动画模拟
  597.             const refreshBtn = document.querySelector('.btn-primary[href="?"]');
  598.             if (refreshBtn) {
  599.                 refreshBtn.addEventListener('click', function(e) {
  600.                     e.preventDefault();
  601.                     this.innerHTML = '<span class="spinner-border spinner-border-sm me-2" role="status" aria-hidden="true"></span>加载中...';
  602.                     setTimeout(() => {
  603.                         window.location.href = '?';
  604.                     }, 500);
  605.                 });
  606.             }
  607.             
  608.             // 删除按钮确认
  609.             const deleteBtns = document.querySelectorAll('.btn-outline-danger');
  610.             deleteBtns.forEach(btn => {
  611.                 btn.addEventListener('click', function() {
  612.                     if (confirm('确定要删除这条记录吗?')) {
  613.                         this.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>';
  614.                         setTimeout(() => {
  615.                             alert('删除功能需要后端API支持,当前为演示界面');
  616.                             this.innerHTML = '<i class="bi bi-trash"></i>';
  617.                         }, 1000);
  618.                     }
  619.                 });
  620.             });
  621.         });
  622.     </script>
  623.    
  624.     <?php
  625.     // 关闭数据库连接
  626.     if (isset($conn)) {
  627.         $conn->close();
  628.     }
  629.     ?>
  630. </body>
  631. </html>
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|本品网络 ( 苏ICP备2021035735号-1 )

GMT+8, 2026-3-26 05:39 , Processed in 0.075845 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表