简体   繁体   English

PHP分页获取最后一页

[英]PHP Pagination Get Last Page

Basically I am trying to get last page... And well it works sometimes but then say some data is entered in then it gets the wrong page and more data is entered then right page will appear I am pretty sure it has something to do with the $r part. 基本上,我试图获取最后一页...有时它很好,但是随后说输入一些数据,然后又得到了错误的页面,输入了更多数据,然后出现了正确的页面,我很确定这与它有关$ r部分。 Theres more to it but uh this is the main part. 还有更多,但这是主要部分。 I think I am just using wrong math or functions please help. 我想我只是在使用错误的数学或函数,请帮忙。

$limit = 10;
if($_GET['page'] == 0 OR $_GET['page'] == 1) {
    $offset = 0;
} else {
    $offset = $limit * $_GET['page'] - $limit;
}
$next = $_GET['page'] + 1;
$back = $_GET['page'] - 1;

$stmt = $db->prepare("SELECT * FROM users");
$stmt->execute();
$count = $stmt->rowCount();

if($count < $limit) {
    $last = 1;
}

$r = $count % $limit;
if($r < 5) {
    $last = ceil($count / $limit);
} else {
    if($r >= 5) {
        $last = floor($count / $limit);
    }
}

if($_GET['page'] == 0 or $_GET['page'] == 1) {
    if (0 >= $last) {
        $page = 0;
    }
    if (1 >= $last) {
        $page = 1;
    }
}

First, you're using some very confusing code, which is probably why you're confused and stumbling across odd bugs. 首先,您正在使用一些非常混乱的代码,这可能就是为什么您对奇怪的错误感到困惑和绊脚石的原因。 For example, 0 >= $last is the same thing as $last <= 0 and 1 >= $last is the same thing as $last <= 1 . 例如, 0 >= $last$last <= 01 >= $last$last <= 1 So basically your code reads... 所以基本上您的代码读取...

if ($last <= 0) {
    $page = 0;
}

if ($last <= 1) {
    $page = 1;
}

Which if you think about this just means that if $last <= 1 then $page will always be 1 . 如果您考虑到这一点,则意味着$last <= 1$page 始终1 Your conditions aren't eitheror , so both conditions can be met. 您的条件都不 - ,因此两个条件都可以满足。 This hardly makes sense. 这几乎没有道理。 Especially since $last really shouldn't be expected to be anything other than 1 or more in any given condition ( ie $page will always be 1 ) 特别是因为在任何给定条件下,实际上不应期望$last 等于 1或更多( $page始终为1 )。

Also, your logic to floor or ceil $last hardly makes sense. 另外,你的逻辑floorceil $last几乎是有道理的。 You don't actually want to do either or one or the other depending on the value of $r . 实际上,根据$r的值,您实际上不想执行任何一个操作。 You just always want to ceil regardless. 无论如何,您只是一直想ceil

The idea is if you have 10 things or less, and you're placing up to 10 things per page, then you know that you need exactly 1 page. 这个想法是,如果您有10件或更少的东西,并且每页最多放置10件东西,那么您知道只需要一页。 The problem begins when you have 11 things. 当您有11件事时,问题就开始了。 Now you need 2 pages, because the extra 1 thing still exceeds the limit of 10 per page. 现在您需要2页,因为多余的1件事仍然超出了每页10张的限制。 So if you really think about this what this means is that the number of pages needed is always going to be congruent to ceil($count / $limit) . 因此,如果您真的考虑过这个问题,则意味着所需的页面数始终与 ceil($count / $limit) You would never floor that number. 永远不会给这个数字设下限。

Because ceil(16 / 10) == 2 , but according to your logic you would be doing floor(16 / 10) which is actually 1 . 因为ceil(16 / 10) == 2 ,但是根据您的逻辑,您将执行floor(16 / 10) ,实际上是1 But you can't put 16 things on 1 page if the limit per page is 10. 但是,如果每页限制为10,则不能将16件东西放在一页上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM