【问题标题】:Adding previous(back) and next buttons next to the page numbers PHP在页码 PHP 旁边添加上一个(后退)和下一个按钮
【发布时间】:2011-12-14 21:56:42
【问题描述】:

我有两个小问题;

1 .我想在这段代码中添加一个上一个/下一个按钮 2.我希望它只显示上一个和下一个之间最多 10 个链接。因此,如果我有 50 个数字/链接,它只会显示其中的 10 个,而不是页面上的 50 个链接。

我在clo上搜索过

代码有效,只需要其中两个选项。 有人可以帮我吗?谢谢!

 <?php 

        include 'includes/connection.php';  
        $per_page = 8;

        $pages_query = mysql_query("SELECT COUNT(`id`) FROM `products`");
        $pages = ceil(mysql_result($pages_query, 0) / $per_page);

        $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
        $start = ($page - 1) * $per_page;



        $query = mysql_query("SELECT `name` FROM `products` LIMIT $start, $per_page");
        while ($query_row = mysql_fetch_assoc($query)) {
            echo '<p>', $query_row['name'] ,'</p>';
        }

        if ($pages >= 1 && $page <= $pages) {
            for ($x=1; $x<=$pages; $x++) { 
                //echo $x, ' '; 

                echo ($x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a></strong> ' : '<a href="?page='.$x.'">'.$x.'</a> ';
            }
        }else{
            header("location:index.php?page=1");
        }


    ?>

【问题讨论】:

    标签: php button pagination back next


    【解决方案1】:

    首先,为了良好的实践,您应该输入“exit”;在最后的 header() 调用之后。它在这个特定的脚本中没有什么区别,但请记住,在 header("Location: ...") 调用之后的任何代码都将在重定向之前执行。

    现在回答您的问题,试试这个(更新:此代码已经过测试并且可以工作。)

    <?php 
    include 'includes/connection.php';  
    $per_page = 8;
    $pages_query = mysql_query("SELECT COUNT(`id`) FROM `products`");
    $pages = ceil(mysql_result($pages_query, 0) / $per_page);
    
    $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
    $start = ($page - 1) * $per_page;
    
    $query = mysql_query("SELECT `name` FROM `products` LIMIT $start, $per_page");
    while ($query_row = mysql_fetch_assoc($query))
    {
        echo '<p>' . $query_row['name'] . '</p>';
    }
    
    // If the requested page is less than 1 or more than the total number of pages
    // redirect to the first page
    if($pages < 1 || $page > $pages)
    {
        header('Location: ?page=1');
        // end execution of the rest of this script
        // it will restart execution after redirection
        exit;   
    }
    // If more than one page, show pagination links
    if($pages > 1)
    {
        $html = array();
        $html[] = '<strong>';
        // if you're on a page greater than 1, show a previous link
        $html[] = (($page > 1) ? '<a href="?page=' . ($page - 1) . '">Previous</a> ' : '');
        // First page link
        $pageFirst = '<a href="?page=1">1</a>';
        $html[] = (($page == 1) ? "</strong>{$pageFirst}<strong>" : $pageFirst);
    
        if ($pages > 6)
        {
            $start_cnt = min(max(1, $page - (6 - 1)), $pages - 6);
            $end_cnt = max(min($pages, $page + 4), 8);
    
            $html[] = ($start_cnt > 1) ? '...' : ' ';
    
            for ($i = $start_cnt + 1; $i < $end_cnt; $i++)
            {
                $html[] = ($i == $page) ? '</strong><a href="?page=' . $i . '">' . $i . '</a><strong>' : '<a href="?page=' . $i . '">' . $i . '</a>';
                if ($i < $end_cnt - 1)
                {
                    $html[] = ' ';
                }
            }
    
            $html []= ($end_cnt < $pages) ? '...' : ' ';
        }
        else
        {
            $html[] = ' ';
    
            for ($i = 2; $i < $pages; $i++)
            {
                $html[] = ($i == $page) ? '</strong><a href="?page=' . $i . '">' . $i . '</a><strong>' : '<a href="?page=' . $i . '">' . $i . '</a>';
                if ($i < $pages)
                {
                    $html[] = ' ';
                }
            }
        }
        // last page link
        $pageLast = '<a href="?page=' . $pages . '">' . $pages . '</a>';
        $html[] = (($page == $pages) ? "</strong>{$pageLast}<strong>" : $pageLast);
        // Show next page link if you're on a page less than the total number of pages
        $html[] = ($page < $pages) ? ' <a href="?page=' . ($page + 1) . '">Next</a>' : '';
        // If you're not on the last page, show a next link
        $html[] = '</strong>';
    }
    else
    {
         // show page number 1, no link.
        $html[] = '<strong>1</strong>';
    }
    echo implode('', $html);
    

    还要注意,在 PHP 代码后面没有 HTML 代码的 PHP 文件中不需要最后的 ?>,所以我把它省略了。

    【讨论】:

    • 感谢您的宝贵时间。它在页面上显示:1 2 1">下一个和 -1">上一个 2 你知道出了什么问题吗?
    • 也.. 上一个和下一个没有链接
    • 我正在做一些重写。将在几内发布我所拥有的
    • 这是我想出的,借用/改编了 phpBB 分页系统的一些代码。困难的部分不是下一个/上一个链接,而是让它一次最多显示 10 个。 pastebin.com/KEWMirzm -- 顺便说一句,我还更新了答案中的代码
    • 这是更新后的代码:pastebin.com/2vcz30A6 编辑:我所做的只是在内联 if 语句中添加了纯文本“上一个”和“下一个”的链接,其中包含上一个和下一个按钮的链接(第 31 行和第 71 行),以便分别在没有上一页或下一页要显示时显示链接而不是链接。
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 2013-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 2021-08-31
      • 1970-01-01
      相关资源
      最近更新 更多