Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-21-2003, 01:44 PM   #1 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
Paginating & range()

I'm working on a script that uses pagination to limit the amount of rows returned, and display links forward and back to navigate through different result pages. Script works fine, but I've got over 13,000 rows in one DB, so it spits out several hundred pages.

I've been trying to find the secret to showing a range of pages vs showing all pages, (like vbulletin uses for search results) and have something like this:
PHP Code:

$range 
range(($page-3), ($page+3));
foreach (
$range as $value) {
    if (
$value == $page) {
        echo 
"[$value] \n";
    } else {
        echo 
"<a href=\"{$_SERVER['PHP_SELF']}?page=$value\">$value</a>&nbsp;\n";
    }

Now, this works dandy, except if you are on page 1 ~ 5, it shows a negative value, eg:
-2 -1 0 1 2 3 4

Or when you are at or near the last page (say the last page is number 250):
247 248 249 250 251 252 253
even though the limit is 250, it still adds 3 to it.

I need to have a conditional like this:
If page_no is 1 ~ 5, don't subtract 3
If page_no is ($lastpage-3) ~ $lastpage, don't add 3
If page is between 5 ~ ($lastpage-3), subtract 3 and add 3 for the page range.

I can figure out the general outline, and I've tested the exact conditional I've shown, but it doesnt work. Any ideas are welcome.
bdl is offline   Reply With Quote
Old 10-21-2003, 03:49 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
how about stepping back and taking a different approach .. instead of messing with the range before the range is set .. how about adding conditions inside your loop... you would need to make a $max_value variable though.

PHP Code:
$max_value 250;
$range range(($page-3), ($page+3));

foreach (
$range as $value) {
  if((
$value 0) && ($value < ($max_value 1))){
    if (
$value == $page) {
        echo 
"[$value]&nbsp;\n";
    } else {
        echo 
"<a href=\"{$_SERVER['PHP_SELF']}?page=$value\">$value</a>&nbsp;\n";
    }
  }

__________________
Mike
sde is offline   Reply With Quote
Old 10-21-2003, 05:15 PM   #3 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
Excellent

Hey that works pretty damn well, thanks!!
bdl is offline   Reply With Quote
Old 10-21-2003, 07:26 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
happy to help
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP ban IP range script Boks PHP 25 07-27-2008 11:29 PM
Google sets its IPO price range sde Code Newbie News 0 07-26-2004 06:46 AM


All times are GMT -8. The time now is 08:36 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting