dorbmd1177 2016-08-23 01:53 采纳率: 100%
浏览 59

PHP分页获取最后一页

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. 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;
    }
}
  • 写回答

1条回答 默认 最新

  • dspows0637 2016-08-23 02:21
    关注

    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. 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. 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 (i.e. $page will always be 1)

    Also, your logic to floor or ceil $last hardly makes sense. You don't actually want to do either or one or the other depending on the value of $r. You just always want to ceil regardless.

    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. The problem begins when you have 11 things. Now you need 2 pages, because the extra 1 thing still exceeds the limit of 10 per page. 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). 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. But you can't put 16 things on 1 page if the limit per page is 10.

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line