🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Paging Owns Me

posted in Rarely Spoken
Published June 05, 2005
Advertisement
So it looks like I've completed all the login/registration stuff and I've implemented member profiles and member profile updating (by the owners and admins). While writing the profile system I've decided that in some later version of the CMS modules will be required to supply member information (if any) so that all the member information can be viewed in a central location in order to more unify the site.

The last part I need to create in the admin module is the ability to view the members. In this version I wont have sorting capabilities but that's for one main reason: paging owns me. I would like to create a PHP class that allows me to implement paging elsewhere but it seems that paging is very dependent on the actual data that is being paged. On top of that, the paging class will need to be rewritten if databases other than MySQL (or perhaps Postgres) are used since it looks like I need to use LIMIT. The most difficult thing I have found is that two different queries are apparently needed to get the number of pages in the data and the actual data for the page.

This is my current class

   /************************************************************************    *    *    Title:   Paging Manager    *    Author:  Colin Jeanne (http://colinjeanne.net)    *    Date:    June 04, 2005    *    *    Description:    *       Defines the PagingManager class    *       Simplifies the paging of data    *    ************************************************************************/   require_once('db-class.php');      // Simplifies the paging of data by modifying an SQL query to only return   // the rows on the current page   class PagingManager {         var $query;      var $render_function;      var $number_function;      var $items_per_page;      var $max_pages;            // Sets up the PagingManager object      function PagingManager($query, $render_function, $number_function,                             $items_per_page, $max_pages) {         $this->query = $query;         $this->render_function = $render_function;         $this->number_function = $number_function;         $this->items_per_page = (int)$items_per_page;         $this->max_pages = (int)$max_pages;         $this->result = false;      }            // Retrieves rows from the data base and sends them as an associated      // array to the rendering function      // $pagenum is a 1-based index      function ShowPage($pagenum) {         $db = new Database;         $db->Connect();                  if ($db->error != '')            return false;                  $result = $db->Query($this->query . ' LIMIT ' .                              $this->items_per_page * ($pagenum - 1) . ', ' .                              $this->items_per_page);                  if ($result == false) {            $db->Disconnect();            return false;         }                  while (($data = $result->FetchAssoc()) != false)            call_user_func($this->render_function, $data);                  $result->FreeResult();         $db->Disconnect();                  return true;      }            // Returns the number of pages in the query      function GetNumPages($query) {         $db = new Database;         $db->Connect();                  if ($db->error != '')            return false;                  $result = $db->Query($query);                  if ($result == false) {            $db->Disconnect();            return false;         }                  $data = $result->FetchRow();                  $result->FreeResult();         $db->Disconnect();                  return ceil($data / $this->items_per_page);      }            // Prints the list of page numbers      function ShowNumberList($query) {         $pages = $this->GetNumPages($query);                  if ($pages == false)            return false;                  for ($i = 1; $i <= $this->max_pages; $i++)            call_user_func($this->number_function, $i);                  return true;      }   }?>


I havent tested it yet so there might be some bugs or syntax errors. I made render_function and number_function callbacks since the point of this class was that it only helps with paging data not how it will be displayed.
Previous Entry The Solution: Part 2
Next Entry The Core CMS Works!
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement

Latest Entries

4E6 PyGame

1381 views

4E5

1116 views

Happy Yesterday!

1071 views

Another Game

1278 views

Merry Christmas!

1052 views

Hello There

1050 views

Yay!

1065 views
Advertisement