PHP 分頁
什麼是分頁?
PHP 分頁是指將大型資料集分割成更小、更易於管理的部分(稱為“頁”)的過程。它通常用於 Web 應用程式中,以便每頁顯示有限數量的記錄或結果,從而使使用者能夠輕鬆瀏覽資料。
處理大型資料集時,分頁至關重要,因為在一頁上顯示所有記錄會導致效能問題和使用者介面過於複雜。透過實現分頁,您可以改善使用者體驗並最佳化應用程式的效能。
PHP 分頁程式
在您的工作目錄中建立一個新的 PHP 檔案,並使用您想要的名稱儲存它。
這裡我們使用包含 121 條記錄的 studentdata 表,該表位於本地主機資料庫中。
示例
這是一個在 PHP 中實現分頁的示例,用於從資料庫中每頁顯示 10 條學生資料記錄。
<!DOCTYPE html> <html> <head> <style> /* Grid styles */ .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); grid-gap: 10px; margin-bottom: 20px; } .grid-item { background-color: #f2f2f2; padding: 10px; text-align: center; } /* Table styles */ table { border-collapse: collapse; width: 100%; } th, td { padding: 8px; text-align: left; border-bottom: 1px solid #ddd; } th { background-color: #4CAF50; color: white; } /* Pagination styles */ .pagination { display: inline-block; } .pagination a { color: black; float: left; padding: 8px 16px; text-decoration: none; border: 1px solid #ddd; } .pagination a.active { background-color: #4CAF50; color: white; border: 1px solid #4CAF50; } .pagination a:hover:not(.active) { background-color: #ddd; } </style> </head> <body> <?php // Database connection details $servername = "localhost"; $username = "root"; $password = ""; $dbname = "assignments"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Number of records per page $recordsPerPage = 10; // Current page number if (isset($_GET['page'])) { $currentPage = $_GET['page']; } else { $currentPage = 1; } // Calculate the starting record index $startFrom = ($currentPage - 1) * $recordsPerPage; // Fetch student data with pagination $sql = "SELECT * FROM studentdata LIMIT $startFrom, $recordsPerPage"; $result = $conn->query($sql); if ($result->num_rows > 0) { // Display student data echo "<div class='grid-container'>"; echo "<div class='grid-item'><strong>ID</strong>< /div>"; echo "<div class='grid-item'><strong>Name</strong>< /div>"; echo "<div class='grid-item'><strong>Age</strong>< /div>"; while ($row = $result->fetch_assoc()) { echo "<div class='grid-item'>" . $row["id"] . "</div>"; echo "<div class='grid-item'>" . $row["name"] . "</div>"; echo "<div class='grid-item'>" . $row["age"] . "</div>"; } echo "</div>"; } else { echo "No records found."; } // Pagination links $sql = "SELECT COUNT(*) AS total FROM studentdata"; $result = $conn->query($sql); $row = $result->fetch_assoc(); $totalRecords = $row["total"]; $totalPages = ceil($totalRecords / $recordsPerPage); echo "<div class='pagination'>"; if ($totalPages > 1) { for ($i = 1; $i <= $totalPages; $i++) { if ($i == $currentPage) { echo "<a class='active' href='?page=$i'>$i</a> "; } else { echo "<a href='?page=$i'>$i</a> "; } } } echo "</div>"; // Close the connection $conn->close(); ?> </body> </html>
輸出
解釋
程式碼的第一部分使用提供的伺服器名稱、使用者名稱、密碼和資料庫名稱建立資料庫連線。然後檢查連線是否成功。接下來,它設定每頁要顯示的記錄數,並根據 URL 引數確定當前頁碼。它使用當前頁碼和每頁記錄數計算起始記錄索引。然後,程式碼執行資料庫查詢以使用 LIMIT 子句獲取學生資料,以便僅檢索當前頁面的相關記錄。它遍歷查詢結果並在網格佈局中顯示學生資料。
程式碼的第二部分處理分頁。它執行另一個數據庫查詢以確定“studentdata”表中的記錄總數。它透過將記錄總數除以每頁記錄數並向上取整來計算總頁數。然後,程式碼使用迴圈生成分頁連結。如果總頁數大於 1,則會為每一頁生成一個連結。當前頁使用“active”類突出顯示。最後,關閉資料庫連線並關閉 HTML 程式碼。總的來說,這段程式碼檢索並顯示帶有分頁的學生資料,並提供在頁面之間切換的導航連結。
結論
PHP 程式碼演示了在 PHP 中實現分頁的方法。它利用 MySQL 資料庫來獲取學生資料子集並在網格佈局中顯示它。程式碼根據每頁記錄數計算總頁數,並相應地生成分頁連結。使用者可以透過單擊頁面連結來瀏覽資料。此實現透過將大型資料集劃分為可管理的部分來改善使用者體驗,從而提高效能並防止使用者介面過於複雜。它展示了實現分頁所涉及的基本步驟,包括建立資料庫連線、使用分頁檢索資料以及動態生成分頁連結。