我已经管理了这个分页,但它一团糟。数字填满了页面。有人可以展示一些方法来将分页编号分解为下图所示的内容吗?
截至目前,我的代码显示结束编号并弄乱了页面外观。
这是我的 php
<?php
require('includes/config.php');
if (! $user->is_logged_in()){
header('Location: login.php');
exit();
}
//define page title
$title = 'Leads Page';
?>
<html>
<head>
<style>
.tbl-qa{width: 100%;font-size:0.9em;background-color: #f5f5f5;}
.tbl-qa th.table-header {padding: 5px;text-align: left;padding:10px;}
.tbl-qa .table-row td {padding:10px;background-color: #FDFDFD;vertical-align:top;}
.button_link {color:#FFF;text-decoration:none; background-color:#428a8e;padding:10px;}
#keyword{border: #CCC 1px solid; border-radius: 4px; padding: 7px;background:url("demo-search-icon.png") no-repeat center right 7px;}
.btn-page{margin-right:10px;padding:5px 10px; border: #CCC 1px solid; background:#FFF; border-radius:4px;cursor:pointer;}
.btn-page:hover{background:#F0F0F0;}
.btn-page.current{background:#F0F0F0;}
</style>
</head>
<body>
<?php require('layout/header.php'); ?>
<?php
$search_keyword = '';
if(!empty($_POST['search']['keyword'])) {
$search_keyword = $_POST['search']['keyword'];
}
$sql = 'SELECT * FROM leads WHERE pass LIKE :keyword OR nationality LIKE :keyword OR availability LIKE :keyword ORDER BY id DESC ';
/* Pagination Code starts */
$per_page_html = '';
$page = 1;
$start=0;
if(!empty($_POST["page"])) {
$page = $_POST["page"];
$start=($page-1) * ROW_PER_PAGE;
}
$limit=" limit " . $start . "," . ROW_PER_PAGE;
$stmt = $db->prepare($sql);
$stmt->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
$stmt->execute();
$row_count = $stmt->rowCount();
if(!empty($row_count)){
$per_page_html .= "<div style='text-align:center;margin:20px 0px;'>";
$page_count=ceil($row_count/ROW_PER_PAGE);
if($page_count>1) {
for($i=1;$i<=$page_count;$i++){
if($i==$page){
$per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page current" />';
} else {
$per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page" />';
}
}
}
$per_page_html .= "</div>";
}
$query = $sql.$limit;
$stmt = $db->prepare($query);
$stmt->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll();
?>
<form name='frmSearch' action='' method='post'>
<div style='text-align:right;margin:20px 0px;'><input type='text' name='search[keyword]' value="<?php echo $search_keyword; ?>" id='keyword' maxlength='25'></div>
<?php if(!empty($search_keyword)){ echo $search_keyword; } ?>
<table class='tbl-qa'>
<thead>
<tr>
<th class='table-header' width='20%'>Title</th>
<th class='table-header' width='40%'>Description</th>
<th class='table-header' width='20%'>Date</th>
</tr>
</thead>
<tbody id='table-body'>
<?php
if(!empty($result)) {
foreach($result as $row) {
?>
<tr class='table-row'>
<td><?php echo $row['pass']; ?></td>
<td><?php echo $row['nationality']; ?></td>
<td><?php echo $row['availability']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<?php echo $per_page_html; ?>
</form>
</body>
</html>
非常感谢您的帮助。将永远感激。
谢谢
使用此代码作为参考,首先尝试在单独的 php 文件中运行它并检查输出。
<?php
//define page title
$title = 'Leads Page';
define('ROW_PER_PAGE', 10);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$db = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
?>
<html>
<head>
<style>
.tbl-qa {
width: 100%;
font-size: 0.9em;
background-color: #f5f5f5;
}
.tbl-qa th.table-header {
padding: 5px;
text-align: left;
padding: 10px;
}
.tbl-qa .table-row td {
padding: 10px;
background-color: #FDFDFD;
vertical-align: top;
}
.button_link {
color: #FFF;
text-decoration: none;
background-color: #428a8e;
padding: 10px;
}
#keyword {
border: #CCC 1px solid;
border-radius: 4px;
padding: 7px;
background: url("demo-search-icon.png") no-repeat center right 7px;
}
.btn-page {
margin-right: 10px;
padding: 5px 10px;
border: #CCC 1px solid;
background: #FFF;
border-radius: 4px;
cursor: pointer;
}
.btn-page:hover {
background: #F0F0F0;
}
.btn-page.current {
background: #F0F0F0;
}
</style>
</head>
<body>
<?php
$search_keyword = '';
if (!empty($_POST['search']['keyword'])) {
$search_keyword = $_POST['search']['keyword'];
}
$sql = 'SELECT * FROM leads';
// Add search condition if a keyword is provided
if (!empty($search_keyword)) {
$sql .= " WHERE pass LIKE '%$search_keyword%' OR nationality LIKE '%$search_keyword%' OR availability LIKE '%$search_keyword%'";
}
$sql .= ' ORDER BY id DESC';
// Pagination code starts
$per_page_html = '';
$page = 1;
$start = 0;
if (!empty($_POST["page"])) {
$page = $_POST["page"];
$start = ($page - 1) * ROW_PER_PAGE;
}
$limit = " LIMIT " . $start . "," . ROW_PER_PAGE;
// Get the total number of rows
$result = $db->query($sql);
$row_count = $result->num_rows;
if (!empty($row_count)) {
$per_page_html .= "<div style='text-align:center;margin:20px 0px;'>";
$page_count = ceil($row_count / ROW_PER_PAGE);
if ($page_count > 1) {
$ellipsis = false;
for ($i = 1; $i <= $page_count; $i++) {
if ($i == $page) {
$per_page_html .= "<input type='submit' name='page' value='$i' class='btn-page current' />";
$ellipsis = true;
} elseif ($i <= 3 || $i >= $page_count - 2 || abs($i - $page) <= 1) {
$per_page_html .= "<input type='submit' name='page' value='$i' class='btn-page' />";
$ellipsis = true;
} elseif ($ellipsis) {
$per_page_html .= '...';
$ellipsis = false;
}
}
}
$per_page_html .= "</div>";
}
// Add the limit clause to the SQL query and execute it
$query = $sql . $limit;
$result = $db->query($query);
// Fetch the rows into an associative array
$result_array = array();
while ($row = $result->fetch_assoc()) {
$result_array[] = $row;
}
?>
<form name='frmSearch' action='' method='post'>
<div style='text-align:right;margin:20px 0px;'><input type='text' name='search[keyword]' value="<?php echo $search_keyword; ?>" id='keyword' maxlength='25'></div>
<?php if (!empty($search_keyword)) {
echo $search_keyword;
} ?>
<table class='tbl-qa'>
<thead>
<tr>
<th class='table-header' width='20%'>Title</th>
<th class='table-header' width='40%'>Description</th>
<th class='table-header' width='20%'>Date</th>
</tr>
</thead>
<tbody id='table-body'>
<?php
if (!empty($result)) {
foreach ($result as $row) {
?>
<tr class='table-row'>
<td><?php echo $row['pass']; ?></td>
<td><?php echo $row['nationality']; ?></td>
<td><?php echo $row['availability']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<?php echo $per_page_html; ?>
</form>
</body>
</html>
您也可以根据需要添加样式。