[英]Pagination in PHP not showing data when page is changed
I am new to PHP and web development.我是 PHP 和 Web 开发的新手。 I got a sample code for paginations from web which I further modified as per my requirement.
我从 web 获得了一个分页示例代码,我根据我的要求进一步修改了它。
Issue :- On my website, there are categories in the navigation menu , when they are clicked they redirect to there respective page and show data related to that particular category.问题:- 在我的网站上,导航菜单中有类别,当点击它们时,它们会重定向到相应的页面并显示与该特定类别相关的数据。 On that page I have created a pagination , max data which can be shown on 1 page is 9. Now when the page loads and active pagination is 1 everything works fine, but when I change the page no data gets loaded on other pages(ex. 2,3,4 or hitting next or last).
在该页面上,我创建了一个分页,可以在 1 页上显示的最大数据为 9。现在,当页面加载且活动分页为 1 时,一切正常,但是当我更改页面时,其他页面上没有加载任何数据(例如. 2,3,4 或点击下一个或最后一个)。
What I have tried:-I think, somewhere I have understood the issue when I was clicking on different pagination numbers.我尝试过的:-我认为,当我单击不同的分页编号时,我已经理解了这个问题。 When the category on home page is clicked the URL takes a "cat_id" with it to the next page that's why first page is loading fine, but when other pagination pages are clicked URL does not gets the "cat_id".
当单击主页上的类别时,URL 将带有一个“cat_id”带到下一页,这就是为什么第一页加载正常,但是当单击其他分页页面时,URL 不会获取“cat_id”。 I tired to send the cat_id on other pages also but it did not worked.
我也厌倦了在其他页面上发送 cat_id 但它没有用。 I am not that much experienced in PHP to try anything further.
我在 PHP 方面没有太多经验,无法进一步尝试。
Here are some image of Home page and next page.这是主页和下一页的一些图像。 Home Page with category menu
带有类别菜单的主页
$limit = 9;
$current_page = 1;
if(isset($_GET['page'])){
$page = $_GET['page'];
if($page<=0){
$page=1;
$current_page=1;
}else{
$current_page = $page;
}
}else{
$page = 1;
}
$offset = ($page - 1) * $limit;
// FETCHING IMAGES FROM DATABASE
$cat_id = $_GET['cat_id'];
$sql = "SELECT * FROM `image_files` where `cat_id` = $cat_id ORDER BY `img_id` DESC LIMIT $offset,$limit ";
$result = mysqli_query($conn, $sql);
?>
<div class="thumbnail-wrapper">
<?php
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)){
?>
<div class="card">
<img data-id="download.php?file=<?php echo $row['img_name'] ?>" src="uploaded/<?php echo $row['img_name'] ?>"
alt="">
</div>
<?php
}
}else{
?>
<h3>NO MORE DATA IN THE DATABASE</h3>
<?php } ?>
</div>
<!-- FETCHING TOTAL ROWS IN DATABASE AND CALCULATING TOTAL PAGE NUMBERS -->
<?php
$sql = "SELECT * from `image_files` where cat_id = $cat_id";
$res = mysqli_query($conn, $sql);
$total_rows = mysqli_num_rows($res);
$total_page = ceil($total_rows / $limit);
$start = ($page - 1);
switch($start){
case 0:
$start = 1;
}
if($start == 0){
$start =1;
}
$end = ($page + 4);
if($end > $total_page){
$end = $total_page;
}
?>
<ul class="pagination">
<!-- FIRST AND PREV BUTTON -->
<?php if($page>1){ ?>
<a href="?page=1">First</a>
<a href="?page=<?php echo $page-1 ?>">Prev</a>
<?php } ?>
<!-- LOOP FOR PAGE NUMBERS -->
<?php for($i = $start; $i <= $end; $i++){
$class='';
if($current_page==$i){
$class='active';
?>
<a class="<?php echo $class?>" href="javascript:void(0)"><?php echo $i ?></a>
<?php
}else{
?>
<a class="<?php echo $class?>" href="?page=<?php echo $i ?>"><?php echo $i ?></a>
<?php } }?>
<!-- NEXT AND LAST BUTTON -->
<?php if(($i-1)>$page){ ?>
<a href="?page=<?php echo $page+1 ?>">Next</a>
<a href="?page=<?php echo $total_page;?>">Last</a>
<?php } ?>
</ul>
href="?page=<?php echo $i ?>"
您在此处的 URL 需要包含cat_id
:
href="?cat_id=<?= $_GET['cat_id'] ?>&page=<?= $i ?>"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.