/* Enjoy!!! Dino Ciuffetti - dam2k@users.sourceforge.net
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program (COPYING); if not, go to http://www.fsf.org/ or write
* to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*
*/
/* PAGINATOR */
/**
* Each CMS offers the user the ability to split an array in more pieces in a paging fashion.
* This is useful, for example, if you have to render the result of the search engine, or another
* array of your choice, to the page. Obviously, the output may become (very) long. This is not
* professional, so the output very often is divided in pages. This class do that.
* Class instance: $cms->paginator
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
class cmsdam_paginator
{
/**
* This method takes the array "$array" and splits it into n page arrays of "$maxrows" element max.
* Returns a piece of array "$array", using "$maxrows" as a number of rows (or columns) per page.
* "$page" is the page number to start with.
* @param int maxrows The max number of rows per page
* @param int page The page you want the array of
* @param array array The array you want to split
* @return array The "$page"th page of array "$array", where each page is made of "$maxrows" rows
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function paginator_array ($maxrows, $page, $array)
{ /* Returns a piece of array $array, using $maxrows as a number
of rows (or columns) per page. $page is the page number to start with.
If $page is <= 0 then this method returns null.
If $array is null then this method returns null.
If $maxrows <= 0 then this method returns $array
If "count ($array) <= $maxrows" then this method returns $array.
*/
$keys = count ($array);
// Some check
if ($page <= 0) { return null; }
if ($array == null) { return null; }
if ($maxrows <= 0) { return $array; }
if ($keys <= $maxrows) { return $array; }
// Min margin
$Kmin = ($page - 1) * $maxrows;
if ($Kmin > ($maxrows * $page)) { // We exceeded the last index in array for Kmin
$Kmin = 0;
}
// Max margin
$Kmax = $Kmin + $maxrows;
if ($Kmax > $keys) { // We exceeded the last index in array for Kmax
$Kmax = $keys;
}
$j = 0;
$array2 = null;
for ($i=$Kmin; $i<$Kmax; $i++)
{ // Paginator!!
// echo 'DEBUG: paginator: ' . $i . "
\n";
$array2[$j] = $array[$i];
$j++;
}
return $array2;
}
/**
* This method takes the array "$array" and calculates the number of pages for "$maxrows" elements per page.
* @param int maxrows The max number of rows per page
* @param array array The array you want the number of pages
* @return int The number of pages of array "$array", where each page is made of "$maxrows" rows
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function paginator_pages ($maxrows, $array)
{ /* This method returns the $array max pages for the paginator.
It uses $maxrows to calc the max number of the pages.
It returns the number of the pages.
If $maxrows <= 0 then this method returns 1
If $array is null then this method returns 0.
*/
// Some check!!
if ($maxrows <= 0) { return 1; }
if ($array == null) { return 0; }
// This is the number of the pages
return ceil (count($array) / $maxrows);
}
}
?>