|
Hex Colours :: Page 1 of 23 |
|
1»2»3»4»5»6»Last |
|
|
#F0F8FF #FAEBD7 #00FFFF #7FFFD4 #F0FFFF #F5F5DC
|
»
|
Results 1 to 6 of 137
|
view source
« « In case you were not sure .. this is what I mean by pagination...
The class
pager.class.php Is here ,
This class could easily be translated to PHP5 OO but would gain little from the journey. As is, it works in PHP >= 4.2.3
Guilt & non-zen OO
This class uses $_SERVER['PHP_SELF'] && $_SERVER['QUERY_STRING'] superglobals to help mung the urls and keep any existing GET data intact , this may or may not work in certain CGI situations or certain webservers , to combat this (and to keep it real) you can pass such data as optional args Also the get_limit() function assumes your DB accepts `LIMIT 0,10` type commands , however see get_limit_offset()
Usage
OK, being as we are trying to keep database and HTML interests out of the class this does make the calls to the class a little heavier , but not that much.
Ideally you would extend or aggregate this class to make life even easier but for now a straightforward procedural implementation..
The important bits
The constructor
$max = the maximum number of results in the database, you must use the same WHERE conditions here as in your main query, you could of course just set a number.
$num = the number of results to show per page.
$_GET['_p'] , this class uses GET to pass data around , it only needs 1 variable of data and we are calling it '_p' , if in some surreal circumstances you already use _p in your controller logic then set it to something else via the optional args, if you have a CGI installation or are using a webserver that does not provide QUERY_STRING and PHP_SELF variables correctly you may need to pass optional args,
After that's done you simply make your real query and remember to tack on the $pager->get_limit() bit ( see also get_limit_offset() )
Things to look at
That's it , the pager is ready .. of course as of yet you have nothing to show for your troubles, so lets show something.
The main display we want is the clickable links to each page , to keep markup out of the class we pass the class a format and it does a simple replacement , {LINK_HREF}, {LINK_LINK} are the only replacers required (or parsed) and you really need to use both of them, the rest (the markup) is up to you , the second argument is the separator which could be whitespace , an image, HTML.... whatever.
$pager->get_range($format, $separator, [$option_first, $option_last]);
OK now for some fluffy bits , you may also want to show the number of pages .. which results are currently showing etc, for this we use get_title(). the following shows all the possible replacements for get_title()
$pager->get_title($format);
So {CURRENT},{MAX},{FROM},{TO} and {TOTAL} all get replaced with the relevant values (I will let you guess what stands for what ;))
Optional bits
That's almost it , there are some optional things you can do.
If your result set has say 1000 pages and unless you are a show off... you probably don't really want 1000 clickable links showing all at once on your page, so you can use set_range() to show the maximum number of links to show at one time , the class automagically works out the range of links to show dependant on your current position in the result set. So ...
would only show 1 - 20 , with an added 'next' link to move to the 21 to 40 result set, at this point a previous link also turns up.
If you go this route you probably want to decide what the 'next' and 'previous' links look like , you can do this by adding them to the end of get_range() ..
$pager->get_range($format, $separator, $first, $last);
`First` and `Last` above could of course be <img src="/images/first.jpg" alt="" /> etc
Lastly you may want a 'prev' and 'next' link , the functions get_prev() and get_next() will return these links using the format you supply for the actual link be it text or an image.
OK that's your lot , I like this class which is why I posted it , I use it in dozens of different situations and to date it has caused me few headaches , I hope it has analgesic properties for you as well !
PS
LIMIT
The pager class returns '$offset,$num' for use in the LIMIT clause which works for example in MySQL but may not work in your DB , if not then you can use get_limit_offset() to grab the offset and $num that you used in the constructor for the actual limit relative to the offset , so back to MySQL we could use
etc
Optional constructor argument
If you are using a CGI installation or your webserver does not pass PHP_SELF or QUERY_STRING correctly you can pass this information in the constructor.... , the pager requires PHP_SELF regardless , the QUERY_STRING bit is only needed if you want to preserve other GET data when moving around your resultset
Of course you simply might wish to keep the superglobals out of it altogether for reasons of java-oo-zen , or like me for when they may not be relevant. For instance I use this class in a PHP-GTK application where QUERY_STRING is not relevant but $extras[$_SESSION['get_string']] is.
So set up the variables...
Then pass $extras as the fourth parameter...
Also if by some misfortune you manage to already use $_GET['_p'] in your scripts .. you can change the default control variable as well (just remember to also use it as the 3rd parameter in your constructor!)
Bugs
If you use an odd number for the number of results to show per page then the numbers do not quite add up , I could fix this , but that adds more code & I don't see the issue with using even numbers ???
Not a bug as such but it should be noted that while this class preserves any existing GET data , it does no such thing for POST , so if a form brings you to your page and you require POST variables to keep things real, then manually pass $options['query_string'] as noted above
There are some dodgy method and variable naming issues in the class ... I can live with that.
|