[英]Pagination is not working properly in php
I am trying to make my pagination the way I want.我正在尝试按照我想要的方式进行分页。 Currently, it is working fine but all the numbers are displaying.
目前,它工作正常,但所有数字都在显示。 Plus the total number is fixed.
加上总数是固定的。 I want to implement pagination in a proper way.
我想以适当的方式实现分页。 I want to make it dynamic.
我想让它动态。 Mean it should count total pages from the last date that is available in the database.
意味着它应该从数据库中可用的最后一个日期计算总页数。 Code
代码
$joursParPage = 3;
$joursTotales = '45';
$pagesTotales = ceil($joursTotales/$joursParPage);
if(isset($_GET['page']) AND !empty($_GET['page']) AND $_GET['page'] > 0 AND $_GET['page'] <= $pagesTotales) {
$_GET['page'] = intval($_GET['page']);
$pageCourante = $_GET['page'];
} else {
$pageCourante = 1;
}
$depart = ($pageCourante-1)*$joursParPage;
for($i=$depart;$i<=$pageCourante*3;$i++) {
?>
<h4 class="block-title">
<span style="margin-right: 0px;">Actualités du <?php echo utf8_encode(strftime('%A %d %B %Y', strtotime('-'.$i.' day'))); ?></span>
</h4>
<?php
$date = strftime('%Y-%m-%d', strtotime('-'.$i.' day'));
$videoDer = $bdd->prepare('SELECT * FROM videos WHERE date_video = ? ORDER BY id DESC');
$videoDer->execute(array($date));
while($vD = $videoDer->fetch()) { ?>
<?php }
$articleDer = $bdd->prepare('SELECT * FROM articles WHERE date_article = ? ORDER BY id DESC');
$articleDer->execute(array($date));
while($aD = $articleDer->fetch()) { ?>
<?php } ?>
<?php } ?>
<div class="lien_page">
<span class="page_titre"> Pages </span>
<?php
for($l=1;$l<=$pagesTotales;$l++) {
if($l == $pageCourante) {
echo '<span class="pageActuelle">'.$l.'</span>';
} else {
echo ' <a href="index.php?page='.$l.'" class="lien_page">'.$l.'</a> ';
}
}
?>
Well you are doing in a heavy way好吧,你做得很重
Best way is to use limit
clause instead of for loop to access specific no of records like you are doing.最好的方法是使用
limit
子句而不是 for 循环来访问特定数量的记录,就像你在做的那样。
$recordsPerPage = 5;
$currentPage = (isset($_GET['page]) && $_GET['page'] > 0)?$_GET['page]:1;
if($currentPage == 1){
$startRecord = 1;
}else{
$startRecords = (($currentPage - 1) * $recordsPerPage) + 1
// it will be 6 in case of Page=2
// it will be 11 in case of Page 3
}
$date = strftime('%Y-%m-%d', strtotime('-'.$i.' day'));
$videoDer = $bdd->prepare('SELECT * FROM videos WHERE date_video = ? ORDER BY id DESC limit '.$startRecords.', '.$recordsPerPage);
$videoDer->execute(array($date));
// your query looks like
select *
from videos
where date_video = ''
order by id desc
limit 6,5;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.