tags: apache   archive   business   charity   climbing   comic   communication   database   email   exchange   family   fm2008   hack   hardware   humour   linux   liverpool   microsoft   money   mysql   network   oes   opensource   outlook   php   pictures   process   project   quote   real_life   review   rss   science   security   software   thought   tsm   updates   webdev   website   windows  

Pagination

Tue, 04 Dec 2007 04:18:06
Pagination is where you have your query broke down into sections and then add "Next/Previous" buttons to your page. We're going to look at some PHP code which runs through the basics of this operation. If you'd like to learn PHP from scratch check out the 5 part introduction to PHP here: http://ryanpartington.com/tags/php/ (down by the bottom). Now lets get started...

I've just pasted some code I've recently used without comments. It's pretty self explanatory and basic but if you need any help, leave me a comment. First things first, lets look at our variables;
  1. $page = $_GET['page'];
  2. IF(empty($page)){
  3. $page = 1;
  4. }
  5. $limit = $_GET['limit'];
  6. IF(empty($limit)){
  7. $limit = 25;
  8. }
  9. $limitvalue = $page * $limit - ($limit);
  10. $pageplus=$page; $pageplus++; $pageprev = $page; $pageprev--;

Now our MySQL query
  1. SELECT *
  2. FROM blg_article_art
  3. ORDER BY id_art DESC LIMIT $limit, $limitvalue

Finally our hyperlinks;

Cheers
Ryan Partington