DB Pager Documentation by Matthew McNaney ------------------------------------------------------------------ Before version 1.0.0, PhpWebSite had some great paging tools. There were just two little details that were troublesome. 1) Often you would need to session the class. This created a bit of overhead and refresh problems. 2) The pager class would need ALL the results of your data before displaying a portion of the results. Either you repeatedly pulled all the data at one time or you sessioned the results. 3) The mechanism for employing the pager was complicated. After working with our Pager class and viewing PEAR's pager class, we have tried to combine the best of both worlds. 1) You don't need to session the DB Pager object. It is constructed per page hit and gets its data from the previous view. 2) The DB Pager pulls ONLY the data information needed per page view. 3) The process is much simpler. There are a few downsides: 1) If a user leaves the pager to go to another page, it will be reset to its original settings. However, if you must have it remember the settings from the previous view, you _could_ session the settings. 2) It expects you to use OOP. 3) It expects a specific object/table synchronizing. 4) The pager can only order items with the information it obtains from the database. Getting Started -------------------------------------------------------------------- You will need to require the DB Pager class: PHPWS_Core::initCoreClass('DBPager.php'); or require_once PHPWS_SOURCE_DIR . 'core/class/DBPager.php'; You will also need to require the 'working class'. This is the object that is going to hold your row data. I will be using File Cabinet as an example: PHPWS_Core::initCoreClass('Image.php'); The File Cabinet uses the image and document class extensively. Now I need to make my pager object: $pager = new DBPager('images', 'PHPWS_Image'); The 'images' variable refers to what table to access. The second parameter 'PHPWS_Image' is the class to construct from the table information. Setting Main Parameters -------------------------------------------------------------------- There are three main parameters that need to be set for the pager to function properly: the module, the template, and the link. Module: The module that is using the pager. Use the setModule function: $pager->setModule('filecabinet'); Template: This is the template that the pager will use to display your data (see below for template setup information). Use the the setTemplate function and supply the path below your module's template directory (just like using PHPWS_Template::process): $pager->setTemplate('imageList.tpl'); Link: Pager will check the current url for the correct link. If for some reason it is having problems, you can set the link manually. $pager->setLink('index.php?module=filecabinet&action=main&tab=image'); With these parameters set, the pager is ready to get started. It will pull a number of rows (based on the page limit) from the specified table. Each of these rows will then be put into an object (1). Each variable will be put into a template array. This template array will be run against the row tags in your template. Remember that all the variables from your object will be in the template but it is up to you to create the appropriate tags. Displaying the Page -------------------------------------------------------------------- To get the data, just call the get function: $content = $pager->get(); Layout::add($content); Read on to configure options and get information on setting up your pager template. Where Options -------------------------------------------------------------------- If you want to set restrictions on the table data, you can use the addWhere function. This function works just like the Database class function of the same name. $pager->addWhere('type', 'image/jpeg'); If I entered these parameters, then the pager would only pull the rows where the 'type' was equal to 'image/jpeg'. The same rules apply to this function as to Database. You can set groups, set AND or OR, and use less/greater-than. Setting the Order -------------------------------------------------------------------- If you wish to set an listing order, use the setOrder function. $pager->setOrder('column_name', 'direction', $only_if_empty); For example if you wanted to order by last name from A to Z: $pager->setOrder('last_name', 'asc'); From Z to A $pager->setOrder('last_name', 'desc'); If $only_if_empty is set to 'true' (it is false by default), then the order will ONLY be in effect if a sort option has not been selected. Plugging in Page Tags -------------------------------------------------------------------- If you want to add extra dynamic information to your list template, you will need to use the addPageTags function. In the below example, I need to translate the column headers, so I am plugging them into addTags. $tags['PAGE_LABEL'] = _('Page'); $tags['TITLE'] = _('Title'); $tags['FILENAME'] = _('Filename'); $tags['MODULE'] = _('Module'); $tags['SIZE'] = _('Size'); $tags['ACTION'] = _('Action'); $pager->addPageTags($tags); Plugging in Special Row Tags -------------------------------------------------------------------- If you want to plug in special tags per row, then you can do so using the addRowTags function. This function works differently than addPageTags which just required an array of tags. Row tags have the potential to be different every iteration. Therefore you need to call them via the object. $pager->addRowTags('get_row_tags'); This would call a method named 'get_row_tags' on each new object. $object->get_row_tags(); This function must return an associative array. class my_object { function get_row_tags() { $template['TITLE'] = $this->getFormatedTitle(); return $template; } function getFormatedTitle() { return strtoupper($this->title); } } So if the 'title' column is pulled from the table as 'my pets', your addition of the new row tag would replace it with 'MY PETS'. Note that you can also add static row tags and conditionals using this function. One last note. You can add parameters to addRowTags as well: $pager->addRowTags('get_row_tags', TRUE, 21, 'clown'); function get_row_tags($likes_chocolate, $age, $occupation) { ... } If you are not using a class objects, another way of changing the row output is to pipe the information through a function or static method call. To do so, call addRowFunction: $pager->addRowFunction('row_func'); This would pass your row variables to the "row_func" function and echo the resultant array. function row_func($values) { $tpl['TITLE'] = process_title($values['title']); return $tpl; } You may also call a static method by sending an class and method name array like so: $pager->addRowFunction(array('class_name', 'method_name')); The result from the method should, again, be an associative array corresponding to your pager template. Converting the date -------------------------------------------------------------------- If one of your table columns is in unix time, you can have DBPager format it for you: $pager->convertDate($column_name, $format[%c]); The $column_name is the title of the datetime column. The format matches the parameters used in the strftime function. The default value is '%c'. Keep in mind that dates are formatted BEFORE what is called in addRowTags. Don't format it twice. Adding Toggles -------------------------------------------------------------------- Non-colored rows are not only boring, they make it more difficult to match information across the page. To combat this, you need to use the addToggle function. Here is how you could add red, white, and blue stripes to your list. $pager->addToggle('style="background-color : red"'); $pager->addToggle('style="background-color : white"'); $pager->addToggle('style="background-color : blue"'); The pager will alternate between each toggle color as it displays the rows (2). I recommend, however, that you use toggle css classes. $pager->addToggle('class="toggle1"'); $pager->addToggle('class="toggle2"'); These are supported in several default themes. Adding Search -------------------------------------------------------------------- You can easily add a search text box to your pager. $pager->setSearch('search_column'); So if I wanted to search among the db table column 'filename' I would add: $pager->setSearch('filename'); Each extra parameter sent to setSearch will search an addition column: $pager->setSearch('filename', 'first_name'); The search form will be visable after you add the SEARCH tag into your template. You may also send an array of column names to setSearch instead of breaking them into separate parameters. Setting up your template -------------------------------------------------------------------- Before you continue, make sure you know how templates work in phpWebSite(3). It is easy to create a custom sort header for a column. Say you wanted to sort by the table column pet_name: $pager->addSortHeader('pet_name', 'My Pet Names'); This would create a button/title combo that would sort the pet_name column. It would look something like this: [] My Pet Names You would just need to add "PET_NAME_SORT" to your template. If you don't use addSortHeader, DBPager will still create a SORT button without the header text for every column returned from the database. After the headers come the actual rows. Notice that the column tag names are identical to the table column names. The ACTION tag is from the Special Row Tags section.