简体   繁体   中英

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> ';

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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