views:

116

answers:

1

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

}
A: 

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

  }
Jakub Hampl
Not working, thanks for the help though.
TaG
Error messages?
Jakub Hampl
Ah, sorry `$s` should be replaced by `$current_page` pretty much in all the conditionals and loops. I'll edit it.
Jakub Hampl
I corrected `$s` which I think you meant `$start` but the pagination links are displayed `1 1 1 1 1 1 1 1 ...-2 200` and all display the same page.
TaG
OK, try it now, I edited the code and it should hopefully work.
Jakub Hampl
I used the `$current_page` same problem as I stated before but I get this now `3 -2 -1 0 1 2 3 4 ...-2 200` and the links are negative links that don't exist.
TaG
Did you also change the line in the `for` loop?
Jakub Hampl
the for loop was changed as well.
TaG
Ah, yes sorry, try it out on page like 20, that should work correctly. Now you have to add a condition for the edge cases.
Jakub Hampl
what exactly are you talking about? excuse my ignorance
TaG
OK, I added a `min` and `max` functions to the loop so it shouldn't screw up anymore.
Jakub Hampl
the links are displayed `0 1 2 3 4 ...-2 200` the first link is a negative link which does not exist and how do I get rid of that as well as the negative 2 `-2`
TaG
Another round of fixes.
Jakub Hampl
Though you should really be able to take it from here - the basic idea is there.
Jakub Hampl
I fixed it thanks.
TaG