doubo9799 2016-11-25 06:47
浏览 90
已采纳

根据具有相同名称的条目数限制分页

my issue is that I have a database with values like the ones below:\

|Entries|Name|
|12     |Bob |
|14     |Bob |
|15     |Bob |
|27     |Kevin|
|19     |Kevin|
|29     |Dale|
|18     |Dale|
|23     |Dale|
|13     |Dale|

I would like to have pagination where each page lists the entries for each name. So there would be a page for bob, then the next page would have kevins, and then the next would have dales. I know I must use limits to achieve this, but my question is how I would limit each page when the names(Bob,Kevin,Dale) all have different amount of entries.

Below is some php I used in the page where I just wanted 12 entries per page, This may or may not help, I just wanted to add it in here just in case.

$limit = 12;
$pages = ceil($count/$limit);
//check to see if pages is smaller than 
$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
    'options' => array(
        'default'   => 1,
        'min_range' => 1,
    ),
)));
$offset = ($page-1) * $limit;
$query = "SELECT `actual_quote`,`poster`,`formtype`,`id` FROM `data` WHERE `formtype` = 'Inspirational' LIMIT 13 OFFSET :offset";
$stmt = $db->prepare($query);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT); 
$stmt->execute();
  • 写回答

3条回答 默认 最新

  • dtp19819 2016-11-25 06:58
    关注

    To show the pages you can simply select the distinct names:

    $query = "SELECT DISTINCT `Name` FROM `my_table` ORDER BY Name ASC";
    $stmt = $db->prepare($query);
    $stmt->execute();
    $rows = $stmt->fetchAll();
    
    for ($i = 1; $i <= count($rows); $i++) {
        $row = $rows[$i - 1];
        echo '<a href="?page=' . $i . '&name=' . $row['Name'] . '">Page ' . $i . ' (' . $row['Name'] . ')</a>';
    }
    

    On each page you can use the name parameter to get only the results for that name.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角