...

View Full Version : Paging Problem



jessjenn
01-12-2004, 11:24 PM
I'm trying to do the typical paging seen on this forum. I'm partially finished but I'm stuck at limiting the page numbers per page. By that I mean is if I have 200 records to be displayed at 5 pages per page, then it would be something like 1,2,3,4,5,6,7,8...40.

I want to have it so it says: Pages (3): 1 [2] 3

But I would like to go around my code. I've read just about every post here on paging, and while yes they go over how to do this, for some reason, I can't apply it to my code. Maybe I can get some help. Here's my code (only the portion that does paging):



# $SnippetCount["snippet_count"] is the RecordCount for the record set
$number_of_pages = ceil($SnippetCount["snippet_count"]/$perpage);
$ax = 0; // tempcounter used to display page numbers
$current_page = ceil($from/$perpage)+1; // determine current page the user is on


// Generating Page Numbers
for($i=1; $i<=$number_of_pages; $i++) {
if ($i==$current_page) {
echo "<font size=\"6\"class=\"GeneralLargeText\"><strong>$i</strong></font>&nbsp;";
$ax = $ax + $perpage;
continue;
} else {
echo "<a href=\"".$_SERVER['SCRIPT_NAME']."?from=$ax&perpage=$perpage&orderby=".translate_fieldwords($orderby)."&ordertype=$ordertype\"><font class=\"TextSpecialMessage2\">$i</font></a>&nbsp;";
$ax = $ax + $perpage;
}
}

// Modifying $prev and $next variables so they don't go out of bounds.
if($prev < -1) $prev=0;
if($next >= $SnippetCount["snippet_count"]) $next = $from;

// [ Previous | Next ]
echo "[ ";
if($current_page==1)
echo "<a href=\"".$_SERVER['SCRIPT_NAME']."?from=$next&perpage=$perpage&orderby=".translate_fieldwords($orderby)."&ordertype=$ordertype\"><font class=\"TextSpecialMessage2\">Next</font></a>&nbsp;";
elseif($current_page>1 && $current_page<$number_of_pages){
echo "<a href=\"".$_SERVER['SCRIPT_NAME']."?from=$prev&perpage=$perpage&orderby=".translate_fieldwords($orderby)."&ordertype=$ordertype\"><font class=\"TextSpecialMessage2\">Previous</font></a>&nbsp;";
echo "<a href=\"".$_SERVER['SCRIPT_NAME']."?from=$next&perpage=$perpage&orderby=".translate_fieldwords($orderby)."&ordertype=$ordertype\"><font class=\"TextSpecialMessage2\">Next</font></a>&nbsp;";
}
elseif($current_page==$number_of_pages)
echo "<a href=\"".$_SERVER['SCRIPT_NAME']."?from=$prev&perpage=$perpage&orderby=".translate_fieldwords($orderby)."&ordertype=$ordertype\"><font class=\"TextSpecialMessage2\">Previous</font></a>&nbsp;";
echo "]";


So for this, if I had 116 records, and if I was on page one at 5 records per page, the above would display:


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 [ Next ]

Any help is greatly appreciated. Thanks!

Spookster
01-13-2004, 01:53 AM
I just added a pagination feature to a site I am working on. I am too tired now to go through the code and pull out just the pagination portions of the script so I attached the whole file but you can peruse through the attached file if you like.

ReadMe.txt
01-14-2004, 10:43 PM
I've recently written what i would like to dub my 'ultimate page numbering' script. I probably isn't but it sounds better like that.

I'm going to post a proper annotated version on the scripts section of my site once i get around to redoing that bit of my site, so for now here's what i'm using.

$page should be assigned to the current page, while $total is the number of records in the resultset. $currpage should be the the value of $_SERVER['PHP_SELF'].

This will output a list of links that has a button for prev and next page, will always have the first and last links as well as having $lnkoff number of links between the current link and the links at either end. Hope it works out ok for you, i've just quily stripped the bits u wont need after pasting it in here. :)



$perPage = 10;
$lnkoff = 4;

$totalPages = ceil($total / $perPage);
$Ppage = $page - 1;
$Npage = $page + 1;
$startPage = ($page > 2) ? $page - $lnkoff : 2 ;
$startPage = ($startPage < 2) ? 2 : $startPage ;
$spareright = $startPage - ($page - $lnkoff);
$limit = $page+$lnkoff+$spareright;
$uptoPage = ($totalPages > $limit) ? ($limit) : ($totalPages-1) ;
$spareleft = $limit - $uptoPage;
$startPage -= $spareleft;
$startPage = ($startPage < 2) ? 2 : $startPage ;
$uptoPage = ($uptoPage > ($totalPages-1)) ? ($totalPages-1) : $uptoPage ;
if ($totalPages != 1) {
$pageNav = "<p class=\"pagelinks\">\n<a";
if($page != 1){
$pageNav .= " href=\"{$currpage}?page=$Ppage\"";
}
$pageNav .= ">&lt;&lt;&lt;</a> | \n";
$href = ($page == 1) ? '' : " href=\"{$currpage}?page=1\"" ;
$pageNav .= "<a$href>1</a> | \n";
$pageNav .= ($startPage > 2) ? " ... | \n" : '' ;
for ($i=$startPage;$i<=$uptoPage;$i++) {
if($i != $page) {
$href = " href=\"{$currpage}?page=$i\"";
} else {
$href ='';
}
$pageNav .= "<a$href>$i</a> | \n";
}
$pageNav .= ($totalPages > ($uptoPage+1)) ? " ... | \n" : '' ;
$href = ($page == $totalPages) ? '' : " href=\"{$currpage}?page=$totalPages\"" ;
$pageNav .= "<a$href>$totalPages</a> | \n";
$pageNav .= "<a";
if($page < $totalPages){
$pageNav .= " href=\"{$currpage}?page=$Npage\"";
}
$pageNav .= ">&gt;&gt;&gt;</a>\n</p>";
}

celestine
01-15-2004, 04:17 AM
Sorry for burging into the topic.


$startPage = ($page > 2) ? $page - $lnkoff : 2 ;

I see such coding style but what does it mean actually?

firepages
01-15-2004, 06:35 AM
its an alternate and less verbose type of if/else , so


<?
if($x==1){
$var = 1 ;
}else{
$var = 2 ;
}
?>
is equivalent to
<?
$var = ($x==1) ? 1 : 2 ;
?>
e.g.
<?
echo ( isset( $x ) ) ? '$x is set' : '$x is not set' ;
?>


its often used purely for its brevity

Spookster
01-15-2004, 02:43 PM
It's use is discouraged for its obvious unreadability.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum