这是我的index.php:
<?php
$maxlinks = 4;
$paginaAtual = (isset($_GET['paginaAtual'])) ? (int)$_GET['paginaAtual'] : 1;
$maximo = 5;
$inicio = (($maximo * $paginaAtual) - $maximo);
$publicacoesUN = DBRead('publicacao', "ORDER BY id DESC LIMIT $inicio, $maximo");
$post = empty($_GET['post']) ? '' : $_GET['post'];
$pagina = empty($_GET['p']) ? 'home' : $_GET['p'];
if ($post != '' || ($post == '' && $pagina != '')) {
switch ($pagina):
case 'home':
$titulo = '';
$shareTitulo = '';
$descricao = '';
$shareDescricao = '';
$shareImg = '';
$keywords = '';
$ogUrl = '';
$urlCanonico = '';
break;
case 'ultimasnoticias':
$titulo = '';
$shareTitulo = '';
$descricao = '';
$shareDescricao = '';
$shareImg = '';
$keywords = '';
$ogUrl = '';
$urlCanonico = '';
break;
default:
$titulo = 'Home';
$pagina = 'home';
endswitch;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<nav>
<ul>
<li>
<a href="?p=home">Página Inicial</a>
</li>
<li>
<a href="?p=ultimasnoticias">Últimas Notícias</a>
</li>
</ul>
</nav>
<?php
if (empty($post)) {
require_once 'page_' . $pagina . '.php';
} else {
require_once 'posts/' . $post . '.php';
}
?>
</body>
</html>
还有我的ultimasnoticias.php:
<div class="container my-3">
<div class="row">
<?php foreach ($publicacoesUN as $UN): ?>
<div class="col-12 col-md-6 col-lg-3 mb-3 mb-md-3">
<div class="card">
<div class="img-container">
<a href="index.php?post=<?php echo $UN['title']?>"><img src="<?php echo $UN['capa']?>" alt="<?php echo $UN['alt']?>" class="card-img-top" id="imgUNcover"></a>
</div>
<div class="card-body">
<a href="index.php?post=<?php echo $UN['title']?>" class="card-title cardTitleLink"><h1 class="cardTitleUN"><?php echo $UN['title']?></h1></a>
<p class="card-text text-muted"><?php echo $UN['text']?></p>
<a href="index.php?post=<?php echo $UN['title']?>" class="btn btn-outline-danger btn-sm">Continue Lendo</a>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<?php
$pdo = new PDO('mysql:host=localhost;dbname=publicacoes', 'root', '');
$seleciona_2 = $pdo->prepare("SELECT * FROM `bn_publicacao`");
$seleciona_2->execute();
$total = $seleciona_2->rowCount();
$total_paginas = ceil($total/$maximo);
if($total > $maximo){
echo '<a href="?paginaAtual=1">First page</a>';
for ($i = $paginaAtual - $maxlinks; $i <= $paginaAtual -1; $i++) {
if ($i >= 1) {
echo '<a href="?paginaAtual='.$i.'">'.$i.'</a>';
}
}
echo '<span>'.$paginaAtual.'</span>';
for ($i= $paginaAtual +1; $i <= $paginaAtual + $maxlinks; $i++) {
if ($i <= $total_paginas) {
echo '<a href="?paginaAtual='.$i.'">'.$i.'</a>';
}
}
echo '<a href="?paginaAtual='.$total_paginas.'">Last page</a>';
}
?>
当我单击分页的链接(例如1、2,第一页,最后一页...等)时,它会重定向到首页。如果我将整个脚本从ultimasnoticias.php文件放入index.php文件,则分页有效。
此分页脚本适用于静态网站,但不适用于服务器端。我该如何解决?
答案 0 :(得分:0)
我建议您使用此类!太好了为您完成所有任务!
https://github.com/daveismyname/pagination
其用法示例(MVC格式):
$pages = new Paginator('10','p');
$pages->set_total( $this->support->countAllFaq() );
$getAllFaq = $this->support->getAllFaq($pages->get_limit());
if($getAllFaq == false) { $count = 0; } else { $count = count($getAllFaq); }
$pageLinks = $pages->page_links();
数据库查询:
public function countAllFaq()
{
$sql = 'SELECT f.name, f.content, f.date, f.status, f.cat_id, c.id, c.name AS catname FROM chewi_support_faq f LEFT JOIN chewi_support_categories c ON f.cat_id = c.id WHERE f.status = 1';
$results = $this->db->selectExtended($sql);
$totalRows = count($results);
if($results === FALSE){ $totalRows = '0'; }
return $totalRows;
}
public function getAllFaq($limit)
{
$sql = 'SELECT f.id, f.name, f.content, f.date, f.status, f.cat_id, c.id AS cat_id, c.name AS catname FROM chewi_support_faq f LEFT JOIN chewi_support_categories c ON f.cat_id = c.id WHERE f.status = 1 ORDER BY f.cat_id DESC '.$limit;
$results = $this->db->selectExtended($sql);
return $results;
}