/    Sign up×
Community /Pin to ProfileBookmark

Limit page display in pagination?

This is my pagination for members and it works but I need to limit the number of pages displayed. As it is, more pages means a longer pagination. I’ve tried several times to write a second limit to it but so far haven’t been able to work it out.
I’m trying to get it to work like so:

On page 1: | 1 | 2 | 3 | >
On page 2: < | 2 | 3 | 4 | >
On page 3: < | 3 | 4 | 5 | >
and so on. Any help would be appreciated

[code=php]
<?
/**
* members.php
*/
$thisPage=’members’;
$view = $_GET[‘view’];
$page = $_GET[‘page’];
include(“include/session.php”);
include(“include/header.php”);
include(“menu.php”);
global $database;
function getPagerData($numHits, $limit, $page)
{
$numHits = (int) $numHits;
$limit = max((int) $limit, 1);
$page = (int) $page;
$numPages = ceil($numHits / $limit);

$page = max($page, 1);
$page = min($page, $numPages);

$offset = ($page – 1) * $limit;

$ret = new stdClass;

$ret->offset = $offset;
$ret->limit = $limit;
$ret->numPages = $numPages;
$ret->page = $page;

return $ret;
}

// Query for Online members
if ($view==’On’){
$view = ‘On’;
if($database->getNumActiveUsers() == 0){
echo ‘<div class=”middle”>No members are currently online.<br /><a href=”members.php?view=All&amp;page=1″>View All Instead</a></div>’;
include(“include/footer.php”);
return;
}
else
// no of elements per page
$limit = 1;
$total = $database->getNumActiveUsers();
// work out the pager values
$pager = getPagerData($total, $limit, $page);
$offset = $pager->offset;
$limit = $pager->limit;
$page = $pager->page;

if(!defined(‘TBL_ACTIVE_USERS’)) {
die(“Error processing page”);
}

$q = “SELECT active_users.*, users.avatar “.
“FROM active_users, users “.
“WHERE active_users.username = users.username ORDER BY timestamp
DESC LIMIT $offset, $limit”;
$result = $database->query($q);

$num_rows = mysql_numrows($result);
if(!$result || ($num_rows < 0)){
echo ‘<div class=”middle”>Error displaying info</div>’;
return;
}
else if($num_rows > 0){
echo ‘<div id=”edituser”>
<div class=”tag2″>Online Members<br /><a href=”members.php?view=All&amp;page=1″>View All</a></div>’;
}

// Query for All members
}
else
if ($view==’All’){
$view = ‘All’;
// no of elements per page
$limit = 3;
$total = $database->getNumMembers();
// work out the pager values
$pager = getPagerData($total, $limit, $page);
$offset = $pager->offset;
$limit = $pager->limit;
$page = $pager->page;

if(!defined(‘TBL_USERS’)) {
die(“Error processing page”);
}

$q = “SELECT username, avatar, timestamp FROM users ORDER BY timestamp
DESC LIMIT $offset, $limit”;
$result = $database->query($q);
/* Error occurred */
$num_rows = mysql_numrows($result);
if(!$result || ($num_rows < 0)){
echo ‘<div class=”middle”>Error displaying info</div>’;
return;
}
if($num_rows == 0){
echo ‘<div class=”middle”>No members to display.</div>’;
return;
}
echo ‘<div id=”edituser”>
<div class=”tag2″>Members<br /><a href=”members.php?view=On&amp;page=1″>View Online</a></div>’;
}

// build member results for All or Online
echo ‘<div id=”edit”>
<span class=”row”>
<span class=”label emph”>User</span>
<span class=”field emph”>Last Active</span>
</span>’;
for($i=0; $i<$num_rows; $i++){
$uname = mysql_result($result,$i,”username”);
$uimg = mysql_result($result,$i,”avatar”);
$time = mysql_result($result,$i,”timestamp”);
$user = $session->username;

$today = getdate($time);
include(“include/gettime.php”);

echo ‘<span class=”userrow”>
<span class=”user”><a href=”userinfo.php?user=’.$uname.'”>’;
/* Avatar */
if((mysql_result($result,$i,”avatar”)) == 0){
echo ‘<img title=”no avatar uploaded” alt=”no avatar uploaded” src=”include/userimg/no_avatar.gif” />’;
}
else{
$alt = ‘”‘.$uname.'”‘;
echo “<img title=$alt alt=$alt “;
echo ‘class=”memavatar” src=”include/userimg/’.$uimg.'” />’;
}
echo ‘</a><span class=”name”>’;
echo “<a href=”userinfo.php?user=$uname”>$uname</a>”;
if((mysql_result($result,$i,”username”)) == $user){
echo “</span></span>”;
}
else{
echo ” | <a href=”compose.php?user=$uname”>Send PM</a></span></span>”;
}

echo “<span class=”time”>$thedate<br />$thetime</span>
</span>”;
}
echo ‘
</div>’;
echo ‘<span class=”centop”><span class=”paginator”>’;
// output paging system
if ($page == 1) // this is the first page – there is no previous page
echo ” “;
else // not the first page, link to the previous page

echo “<a class=”paginate” href=”?view=$view&amp;page=” . ($page – 1) . “”>«</a>”;

for ($i = 1; $i <= $pager->numPages; $i++) {
echo ” | “;
if ($i == $pager->page)
echo “Page $i”;

else
echo “<a class=”paginate” href=”?view=$view&amp;page=$i”>Page $i</a>”;
}

if ($page == $pager->numPages) // this is the last page – there is no next page
echo ” | “;
else // not the last page, link to the next page
echo ” | <a class=”paginate” href=”?view=$view&amp;page=” . ($page + 1) . “”>»</a>”;

echo ‘</span></span>’;
echo ‘</div>’;

include(“include/footer.php”);
?>

[/code]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@jasonahouleDec 03.2007 — You already have your page number. So try something like this.
[code=php]
for ($i = $pager->page; $i <= $pager->page+3; $i++) {
echo " | ";
if ($i == $pager->page)
echo "Page $i";

else
echo "<a class="paginate" href="?view=$view&amp;page=$i">Page $i</a>";
}
[/code]
Copy linkTweet thisAlerts:
@bustyaauthorDec 03.2007 — That's on the right track, it's limiting the pages displayed (3) but when it gets to the end of the results (let's say last result is on page 5) it continues to create a link to pages (6 and 7) even though there's no results for the pages.
Copy linkTweet thisAlerts:
@bustyaauthorDec 03.2007 — How do I end the for with the last page of results (instead of the last page + the number of pages to display)? I've tried two for(s), one if the page is less than the number of pages and one if the page equals number of pages but this doesn't work either.
Copy linkTweet thisAlerts:
@jasonahouleDec 03.2007 — You can add this just inside your for loop:
[code=php]
if($i > $pager->numPages) break;
[/code]
Copy linkTweet thisAlerts:
@bustyaauthorDec 03.2007 — I'm making progress but still not there, this logic is screwed, I need to take a breather and come back and look at it but this is what I have: Displays one page with arrows when needed and no extra (empty pages) as the $pager->page+3; change does...:

[code=php]
$Range = floor($pager->page+2);
if ($Range >= $page == $pager->numPages)
$Range = $pager->page;
echo '<span class="centop"><span class="paginator">';
// output paging system
if ($page == 1) // this is the first page - there is no previous page
echo " ";
else // not the first page, link to the previous page

echo "<a class="paginate" href="?view=$view&amp;page=" . ($page - 1) . "">«</a>";

for ($i = $pager->page; $i <= $Range; $i++) {
echo " | ";
if ($i == $pager->page)
echo "Page $i";


else

echo "<a class="paginate" href="?view=$view&amp;page=$i">Page $i</a>";
}


if ($page == $pager->numPages) // this is the last page - there is no next page
echo " | ";
else // not the last page, link to the next page
echo " | <a class="paginate" href="?view=$view&amp;page=" . ($page + 1) . "">»</a>";

echo '</span></span>';

[/code]
Copy linkTweet thisAlerts:
@bustyaauthorDec 03.2007 — I have 5 pages of results.


I just reverted and tried this:
[code=php]
if($i > $pager->numPages) break;

[/code]

It still gave me a page 6 also, so I tried:

[code=php]
if($i >= $pager->numPages) break;

[/code]

and that did the trick, thanks.
Copy linkTweet thisAlerts:
@jasonahouleDec 03.2007 — If you don't want to do it that way you can also do this:
[code=php]
for ($i = $pager->page; (($i <= $pager->page + 3) && ($i <= $pager->numPages)); $i++) {
echo " | ";
if ($i == $pager->page)
echo "Page $i";


else

echo "<a class="paginate" href="?view=$view&amp;page=$i">Page $i</a>";
}
[/code]
×

Success!

Help @bustya spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...