PHP & MySQL pagination display problem.
Posted
by TaG
on Stack Overflow
See other posts from Stack Overflow
or by TaG
Published on 2010-05-11T16:17:49Z
Indexed on
2010/05/11
17:04 UTC
Read the original article
Hit count: 196
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>';
}
© Stack Overflow or respective owner