我目前的分页代码如下所示
//what page are we currently on
if(!isset($_GET['page'])){
$page=1;
}
else {
$page=(int)$_GET['page'];
}
//select how many records to show per page
$limit=10;
//count how many rows
$total=$conn->query("SELECT COUNT(*) FROM pagi")->fetchColumn();
//how many pages will be there
$pages=ceil($total/$limit);
if($page<1){
$page=1;//forcing page to be 1 if it is less then 1
}
else if($page>$pages){
$page=$pages;
}
//calculate the offset
$offset=($page-1)*$limit;
//fetch the result from database
$sql=$conn->prepare("SELECT * FROM pagi ORDER BY pg_id LIMIT {$limit} OFFSET {$offset}");
$sql->execute();
if($sql->rowCount()>0){
while($result=$sql->fetch(PDO::FETCH_ASSOC)){;
echo $result['pg_name'],"<br />";
}
echo "<hr>";
}else {
echo "no result to fetch";
}
$prev=$page-1;
$next=$page+1;
$previous="";
$nextr="";
if($page>1){
$previous='<li><a href="?page='.$prev.'">«</a></span>';
}
//for($i=1;$i<=$pages;$i++){
// echo'<a href="?page='.$i.'">'.$i.'</a>';
// }
if($page<$pages){
$nextr='<li><a href="?page='.$next.'">»</a></span>';
}
$numbers="";
for($i = max(1, $page - 5); $i <= min($page + 5, $pages); $i++){
if($i==$page){
$numbers.='<li><a class="active" href="?page='.$i.'">'.$i.'</a></span></li>';
}
else {
$numbers.='<li><a href="?page='.$i.'">'.$i.'</a></span></li>';
}
}
我目前的网址看起来像这样: - localhost/test/pagi.php?page=1
I want it to be
localhost/test/pagi.php/page/1
我在.htaccess
RewriteEngine on
RewriteRule ^page/([a-zA-Z0-9-/]+)$ pagi.php?page=$1
如何更改我的分页以使用上述重写规则或其他规则;我希望能够在用户点击下一步时实现它应该重定向
它们是pagi.php/page/123
而不是pagi.php?page=1
。我对php很新,并且无法找到一种方法来做到这一点。任何帮助都会很棒
答案 0 :(得分:1)
首先需要一种获取页码的新方法,因为$_GET
将不再使用语义网址。像这样:
// find the page number, or default to page 1
$page = 1;
$path = explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
if (is_array($path) && !empty($path)) {
if ($key = array_search('page', $path)) {
$page = (int)$path[$key + 1];
}
}
至于分页,你可以简单地将它包装成一个函数。例如,以下函数假定页面始终在URL的最后指定:
// draws a menu for navigating multiple pages of dynamic content
function paginate($page, $display, $total) {
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if ($path != '/') {
$path = rtrim($path, '/');
if (stristr($path, 'page/')) {
$path = rtrim($path, '0..9');
$query = '';
} else {
$query = $path . '/page/';
}
} else {
$query = 'page/';
}
// create navigation links
$pages = $total <= $display ? 1 : ceil($total / $display);
$first = '<a href="' . $path . $query. '1">first</a>';
$prev = '<a href="' . $path . $query . ($page - 1) . '">prev</a>';
$next = '<a href="' . $path . $query . ($page + 1) . '">next</a>';
$last = '<a href="' . $path . $query . $pages . '">last</a>';
echo ($page > 1) ? "$first « $prev «" : 'first « prev «';
echo ' (page ' . $page . ' of ' . $pages . ') ';
echo ($page < $pages) ? "» $next » $last" : '» next » last';
}
根据您的示例代码,它最终应该看起来像这样:
// find the page number, or default to page 1
// ...
//select how many records to show per page
$limit = 10;
//count how many rows
$total = $conn->query("SELECT COUNT(*) FROM pagi")->fetchColumn();
if ($total > 0) {
$start = $limit * $page - $limit;
$sql = $conn->prepare("SELECT * FROM pagi ORDER BY pg_id LIMIT :start, :limit");
$sql->bindValue(':start', $start, PDO::PARAM_INT);
$sql->bindValue(':limit', $limit, PDO::PARAM_INT);
$sql->execute();
if ($sql->rowCount() > 0) {
while ($result = $sql->fetch(PDO::FETCH_ASSOC)) {
echo $result['pg_name'],"<br />";
}
echo "<hr>";
paginate($page, $limit, $total);
}
} else {
echo "no result to fetch";
}
我不完全确定你在$offset
做了什么,所以我改变了这一点。无论哪种方式,这应该有希望指出你正确的方向。
答案 1 :(得分:0)
问题不是你的php,而是.htaccess文件。如果您想在网址中使用pagi.php,则必须将RewriteRule更改为:
RewriteRule ^/test/pagi.php/page/([a-zA-Z0-9-/]+)$ /test/pagi.php?page=$1