Location: PHPKode > scripts > Impleo Music Collection Script > impleo/classes/Paging.class.php
<?php
/* ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <hide@address.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return Christoffer Kjeldgaard Petersen
 * ----------------------------------------------------------------------------
 */

class Paging
{
    const pageName = 'page';

  /**
   * Creates a paging list with links.
   *
   * @param int $numRecords Total number of items
   * @param int $limit Number of items on each page
   * @param int $display Current page number
   * @param string $sort Current query string of the page with sorting parameters
   * @return array Array with generated paging information
   */
    public static function createPagingList($numRecords, $limit, $display, $sort)
    {
        $arrSort = self::parseUrlSortString($sort);
        $sortBaseLink = "";
        foreach($arrSort as $k => $v)
        $sortBaseLink.= "{$k}={$v}&amp;";

        $paging = ceil($numRecords/$limit);
        $arrPagingOutput = array(null, array(), null);

        if ($display > 1)
        {
            $previous = $display - 1;
            $arrPagingOutput[0] = "?{$sortBaseLink}" . self::pageName . "={$previous}";
        }
        if ($numRecords != $limit)
        {
            for ($i = 1; $i <= $paging; $i++)
            {
                if ($display == $i)
                $arrPagingOutput[1][] = null;
                else
                $arrPagingOutput[1][] = "?{$sortBaseLink}" . self::pageName . "={$i}";
            }
        }
        if ($display < $paging)
        {
            $next = $display + 1;
            $arrPagingOutput[2] = "?{$sortBaseLink}" . self::pageName . "={$next}";
        }
        return $arrPagingOutput;
    }

  /**
   * Parses a query string to an array
   *
   * @param string $strSort A query string with sorting parameters
   * @return array Array of query string values
   */
    private function parseUrlSortString($strSort)
    {
        $arrParsed = array();
        $urlParts = explode("&", $strSort);
        foreach($urlParts as $vars)
        {
            $varParts = explode("=", $vars);
            if ($varParts[0] != self::pageName && $varParts[1] != null)
            $arrParsed[$varParts[0]] = $varParts[1];
        }
        return $arrParsed;
    }
}

?>
Return current item: Impleo Music Collection Script