分页“上一页"和“下一步"错误 [英] Pagination "Prev" and "Next" error

查看:67
本文介绍了分页“上一页"和“下一步"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一切正常,但上一个"和下一个"链接无效.一旦我单击上一个"或下一个",页面即会加载,但不会显示最新图像或旧图像.该链接用引号"php?page = -1"表示上一个"php?page = 1",然后是下一个,但是就像我说的那样,新图像或旧图像仅显示第一个"20"和其他第一笔$ record_count

Everything works fine but the "prev" and "next" links won't work. Once i click "prev" or "next" the page loads but no recent or old images show. The link quotes the "php?page=-1" for prev "php?page=1" and for next but like i said new images or old are shown only the first "20" and other first amount of $record_count

<?php
$dir = ".";
opendir($dir);
$file = ('uploaded/');

$files = glob("uploaded/*.*");
usort($files, function ($a, $b) {
return filemtime($b) - filemtime($a);
});

$record_count  = **20**;
$totla_pages   = ceil(count($files)/$record_count);

$home_script = $_SERVER['REQUEST_URI'];
    if($_SERVER['REQUEST_URI'] == $home_script) {
      $page = "";
}

$offset        = ($page-1)*$record_count;
$files_filter  = array_slice($files, $offset,$record_count);

foreach ($files_filter as $file) {
echo "<a href='$dir/$file'><img src='$file' style='height:180px;width:180px; border:2px solid black;  margin:20px 0px 10px  10px; *margin:10px 0px 10px 20px;'></a>";
}

if($totla_pages > 1){
   if($page != 1){
      echo '<a href="?page='.($page-1).'">Prev</a>';
   }
   if($page != $totla_pages){
      echo '<a href="?page='.($page+1).'">Next</a>';
   }
}
?>

推荐答案

首先,您需要始终正确地初始化$page变量,并将其设置为整数.

To start, you need to initialize your $page variable always and correctly, setting it to an integer.

为此,您可以检查是否发送了查询变量,如果没有,则将其设置为1:

To do that, you can check if a query variable is sent in and if not, set it to 1:

// somewhere at the top of your script
if (empty($_GET['page']))
{
  $page = 1;
}
else
{
  $page = (int) $_GET['page'];
}

您还可以对$_GET['page']进行更多检查,以确保该数字始终大于0.

You could also do some more checking on $_GET['page'] to make sure it is always a number larger than 0.

这篇关于分页“上一页"和“下一步"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆