【发布时间】:2021-01-17 05:40:30
【问题描述】:
我一直在使用我在网上找到的基本 PHP/PDO 分页脚本。
我已经根据我的需要定制了它(我只需要做一些基本的布局工作);剩下的唯一事情就是让分页以与所有其他专业网站相同的方式工作。我的意思是让分页从第 1 页而不是第 0 页开始。你听说过“第 0 页”吗?我肯定没有! (编辑:我在我的网站上使用此脚本的修改版本并使用 .htaccess 文件将第 0 页的 URL index.php?start=0 更改为 /page/0/。经过反思,我现在可以欣赏让第一页的 URL 包含“start=0”的逻辑,因为 Google 做同样的事情。然而,这个问题仍然存在,因为与 Google 不同,这个脚本表明有一个页面 0。)
以下是相关文件:
配置.php
<?php
// define database related variables
$database = 'db';
$host = 'hostname';
$user = 'username';
$pass = 'password';
// try to connect to database
$db = new PDO("mysql:dbname={$database};host={$host};port={3306}", $user, $pass);
if(!$db){
echo "Error in database connection";
}
?>
数据.php
<?php
//include configuration file
require 'configuration.php';
$start = 0; $per_page = 4;
$page_counter = 0;
$next = $page_counter + 1;
$previous = $page_counter - 1;
if(isset($_GET['start'])){
$start = $_GET['start'];
$page_counter = $_GET['start'];
$start = $start * $per_page;
$next = $page_counter + 1;
$previous = $page_counter - 1;
}
// query to get messages from messages table
$q = "SELECT * FROM students LIMIT $start, $per_page";
$query = $db->prepare($q);
$query->execute();
if($query->rowCount() > 0){
$result = $query->fetchAll(PDO::FETCH_ASSOC);
}
// count total number of rows in students table
$count_query = "SELECT * FROM students";
$query = $db->prepare($count_query);
$query->execute();
$count = $query->rowCount();
// calculate the pagination number by dividing total number of rows with per page.
$paginations = ceil($count / $per_page);
?>
index.php
<html>
<head>
<title>Pagination</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
</head>
<body>
<?php include_once 'data.php'; ?>
<div class="table-responsive" style="width: 600px; border: 1px solid black; margin: 10px;">
<table class="table table-striped" class="table table-hover">
<thead class="table-info">
<th scope="col" class="bg-primary" >Id</th>
<th scope="col" class="bg-primary">First Name</th>
<th scope="col" class="bg-primary">Last Name</th>
<th scope="col" class="bg-primary">Email</th>
</thead>
<tbody>
<?php
foreach($result as $data) {
echo '<tr>';
echo '<td>'.$data['id'].'</td>';
echo '<td>'.$data['first_name'].'</td>';
echo '<td>'.$data['last_name'].'</td>';
echo '<td>'.$data['email'].'</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<center>
<ul class="pagination">
<?php
if($page_counter == 0){
echo "<li><a href=?start='0' class='active'>0</a></li>";
for($j=1; $j < $paginations; $j++) {
echo "<li><a href=?start=$j>".$j."</a></li>";
}
}else{
echo "<li><a href=?start=$previous>Previous</a></li>";
for($j=0; $j < $paginations; $j++) {
if($j == $page_counter) {
echo "<li><a href=?start=$j class='active'>".$j."</a></li>";
}else{
echo "<li><a href=?start=$j>".$j."</a></li>";
}
}if($j != $page_counter+1)
echo "<li><a href=?start=$next>Next</a></li>";
}
?>
</ul>
</center>
</div>
</body>
</html>
我认为这可能只是将一些 0 更改为 1 的问题。我已经使用这种方法尝试了一些组合,但我不确定我在做什么。
文章提到如何改变这种分页特性的最接近的说法是,“在第一页上,页面计数器为 0,存储在 $page_counter 中。”因此,像我这样的人可能会得出结论,您只需将该变量的值更改为 1。如果就这么简单就好了!
【问题讨论】:
-
给自己找另一篇文章吧。这一点毫无意义。
-
变量太多,对最终结果毫无用处。
-
您尝试过什么让它发挥作用?为什么不先用
echo "<li><a href=?start='0' class='active'>0</a></li>"调整行?
标签: php pdo pagination