在页码旁边添加上一个(后退)和下一个按钮 PHP

发布于 2024-12-11 23:14:48 字数 1163 浏览 0 评论 0原文

我有两个小问题;

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");
        }


    ?>

I have two tiny little problems;

1 . I want to add a previous / next button into this code
2 . I want it to only show like max 10 links between previous and next. So if i have 50 numbers/links it will only show 10 of them and not 50 links on the page.

I have searched on the clo

The code works, only need that two options in it.
Can someone help me out? Thank you !

 <?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");
        }


    ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贩梦商人 2024-12-18 23:14:48

首先,为了良好的实践,你应该设置一个“出口”;在最后的 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 文件中不需要,因此我将其保留。

First of all, you should, just for good practice, put an "exit;" after the header() call at the end. It doesn't make a difference in this particular script, but keep in mind that any code following a header("Location: ...") call WILL be executed before redirection.

Now to your question, try this (UPDATE: This code has been tested and works.)

<?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);

Also note that the final ?> is not required in PHP files that do not have HTML code following the PHP code, so I left it off.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文