栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

简单的PHP分页脚本

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

简单的PHP分页脚本

这是HTML和代码的混合,但是非常基础,易于理解,并且解耦起来也很简单,可以满足您的需求。

try {    // Find out how many items are in the table    $total = $dbh->query('        SELECt COUNT(*)        FROM table    ')->fetchColumn();    // How many items to list per page    $limit = 20;    // How many pages will there be    $pages = ceil($total / $limit);    // What page are we currently on?    $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(        'options' => array( 'default'   => 1, 'min_range' => 1,        ),    )));    // Calculate the offset for the query    $offset = ($page - 1)  * $limit;    // Some information to display to the user    $start = $offset + 1;    $end = min(($offset + $limit), $total);    // The "back" link    $prevlink = ($page > 1) ? '<a href="?page=1" title="First page">&laquo;</a> <a href="?page=' . ($page - 1) . '" title="Previous page">&lsaquo;</a>' : '<span >&laquo;</span> <span >&lsaquo;</span>';    // The "forward" link    $nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">&rsaquo;</a> <a href="?page=' . $pages . '" title="Last page">&raquo;</a>' : '<span >&rsaquo;</span> <span >&raquo;</span>';    // Display the paging information    echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages, displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>';    // Prepare the paged query    $stmt = $dbh->prepare('        SELECt *        FROM table        ORDER BY name        LIMIT :limit        OFFSET :offset    ');    // Bind the query params    $stmt->bindParam(':limit', $limit, PDO::PARAM_INT);    $stmt->bindParam(':offset', $offset, PDO::PARAM_INT);    $stmt->execute();    // Do we have any results?    if ($stmt->rowCount() > 0) {        // Define how we want to fetch the results        $stmt->setFetchMode(PDO::FETCH_ASSOC);        $iterator = new IteratorIterator($stmt);        // Display the results        foreach ($iterator as $row) { echo '<p>', $row['name'], '</p>';        }    } else {        echo '<p>No results could be displayed.</p>';    }} catch (Exception $e) {    echo '<p>', $e->getMessage(), '</p>';}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/402909.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号