简体   繁体   English

PHP和MySQL分页显示问题

[英]PHP & MySQL pagination display problem

I asked a similar question like this yesterday but after waiting for ever I figured out part of the problem but now I'm stuck again I'm trying to display ... when the search results are to long because my pagination links will keep on displaying and will not stop until every link is displayed on the page. 昨天我问了类似的问题,但是在等待了一段时间之后,我发现了问题的一部分,但现在又卡住了,我试图显示...搜索结果很长时,因为我的分页链接会一直保持下去显示,并且直到页面上显示每个链接后才会停止。

For example I'm trying to achieve the following in the example below. 例如,我正在尝试在以下示例中实现以下目标。 Can some one help me fix my code so I can update my site. 有人可以帮我修复我的代码,以便更新我的网站。 Thanks 谢谢

This is what I want to be able to do. 这就是我想要做的。

First Previous 1 2 ... 5 6 7 8 9 10 11 12 13 ... 199 200 Next Last 

Here is my pagination code that displays the links. 这是显示链接的我的分页代码。

$display = 20;

if (isset($_GET['p']) && is_numeric($_GET['p'])) {

    $pages = $_GET['p'];

} else {

    $q = "SELECT COUNT(id) FROM comments WHERE user_id=3";
    $r = mysqli_query ($mysqli, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($mysqli));
    $row = mysqli_fetch_array ($r, MYSQLI_NUM);
    $records = $row[0];

    if ($records > $display) {
        $pages = ceil ($records/$display);
    } else {
        $pages = 1;
    }

}

if (isset($_GET['s']) && is_numeric($_GET['s'])) {
    $start = $_GET['s'];
} else {
    $start = 0;
}
    //content goes here

if ($pages > 1) {

    echo '<br /><p>';

    $current_page = ($start/$display) + 1;

    if ($current_page != 1) {
        echo '<a href="index.php">First</a>';
    }

    if ($current_page != 1) {
        echo '<a href="index.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> ';
    }

    for ($i = 1; $i <= $pages; $i++) {
        if ($i != $current_page) {
            echo '<a href="index.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> ';
        } else {
            echo '<span>' . $i . '</span> ';
        }
    }

    if ($current_page != $pages) {
        echo '<a href="index.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>';
    }

    if ($current_page != $pages) {
        echo '<a href="index.php?s=' . ($pages - 1) . '&p=' . $pages . '">Last</a>';
    }

    echo '</p>';

}

Instead of the loop just use something like this: 代替循环,只需使用如下代码:

  if($current_page > 8 && $pages > 11) {
    echo '<a href="index.php?s=0&p=' . $pages . '">1</a> ';
    echo '<a href="index.php?s='.$display.'&p=' . $pages . '">2</a> ';
    echo '...';
  }
  for ($i = max(1, $current_page - 4); $i < min($current_page + 4, $pages); $i ++) {
    echo '<a href="index.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> ';
  }
  if ($current_page < $pages - 8 && $pages > 11) {
    echo '...';
    echo '<a href="index.php?s=' . ($display * ($pages - 1)) . '&p=' . $pages . '">' . ($pages - 1) . '</a> ';
    echo '<a href="index.php?s=' . ($display * $pages) . '&p=' . $pages . '">' . $pages  . '</a> ';

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM