PMA\libraries\Util::pageselector PHP Method

pageselector() public static method

Generate a pagination selector for browsing resultsets
public static pageselector ( string $name, integer $rows, integer $pageNow = 1, integer $nbTotalPage = 1, integer $showAll = 200, integer $sliceStart = 5, integer $sliceEnd = 5, integer $percent = 20, integer $range = 10, string $prompt = '' ) : string
$name string The name for the request parameter
$rows integer Number of rows in the pagination set
$pageNow integer current page number
$nbTotalPage integer number of total pages
$showAll integer If the number of pages is lower than this variable, no pages will be omitted in pagination
$sliceStart integer How many rows at the beginning should always be shown?
$sliceEnd integer How many rows at the end should always be shown?
$percent integer Percentage of calculation page offsets to hop to a next page
$range integer Near the current page, how many pages should be considered "nearby" and displayed as well?
$prompt string The prompt to display (sometimes empty)
return string
    public static function pageselector($name, $rows, $pageNow = 1, $nbTotalPage = 1, $showAll = 200, $sliceStart = 5, $sliceEnd = 5, $percent = 20, $range = 10, $prompt = '')
    {
        $increment = floor($nbTotalPage / $percent);
        $pageNowMinusRange = $pageNow - $range;
        $pageNowPlusRange = $pageNow + $range;
        $gotopage = $prompt . ' <select class="pageselector ajax"';
        $gotopage .= ' name="' . $name . '" >';
        if ($nbTotalPage < $showAll) {
            $pages = range(1, $nbTotalPage);
        } else {
            $pages = array();
            // Always show first X pages
            for ($i = 1; $i <= $sliceStart; $i++) {
                $pages[] = $i;
            }
            // Always show last X pages
            for ($i = $nbTotalPage - $sliceEnd; $i <= $nbTotalPage; $i++) {
                $pages[] = $i;
            }
            // Based on the number of results we add the specified
            // $percent percentage to each page number,
            // so that we have a representing page number every now and then to
            // immediately jump to specific pages.
            // As soon as we get near our currently chosen page ($pageNow -
            // $range), every page number will be shown.
            $i = $sliceStart;
            $x = $nbTotalPage - $sliceEnd;
            $met_boundary = false;
            while ($i <= $x) {
                if ($i >= $pageNowMinusRange && $i <= $pageNowPlusRange) {
                    // If our pageselector comes near the current page, we use 1
                    // counter increments
                    $i++;
                    $met_boundary = true;
                } else {
                    // We add the percentage increment to our current page to
                    // hop to the next one in range
                    $i += $increment;
                    // Make sure that we do not cross our boundaries.
                    if ($i > $pageNowMinusRange && !$met_boundary) {
                        $i = $pageNowMinusRange;
                    }
                }
                if ($i > 0 && $i <= $x) {
                    $pages[] = $i;
                }
            }
            /*
            Add page numbers with "geometrically increasing" distances.
            
            This helps me a lot when navigating through giant tables.
            
            Test case: table with 2.28 million sets, 76190 pages. Page of interest
            is between 72376 and 76190.
            Selecting page 72376.
            Now, old version enumerated only +/- 10 pages around 72376 and the
            percentage increment produced steps of about 3000.
            
            The following code adds page numbers +/- 2,4,8,16,32,64,128,256 etc.
            around the current page.
            */
            $i = $pageNow;
            $dist = 1;
            while ($i < $x) {
                $dist = 2 * $dist;
                $i = $pageNow + $dist;
                if ($i > 0 && $i <= $x) {
                    $pages[] = $i;
                }
            }
            $i = $pageNow;
            $dist = 1;
            while ($i > 0) {
                $dist = 2 * $dist;
                $i = $pageNow - $dist;
                if ($i > 0 && $i <= $x) {
                    $pages[] = $i;
                }
            }
            // Since because of ellipsing of the current page some numbers may be
            // double, we unify our array:
            sort($pages);
            $pages = array_unique($pages);
        }
        foreach ($pages as $i) {
            if ($i == $pageNow) {
                $selected = 'selected="selected" style="font-weight: bold"';
            } else {
                $selected = '';
            }
            $gotopage .= '                <option ' . $selected . ' value="' . ($i - 1) * $rows . '">' . $i . '</option>' . "\n";
        }
        $gotopage .= ' </select>';
        return $gotopage;
    }

Usage Example

Esempio n. 1
0
 /**
  * Possibly return a page selector for table navigation
  *
  * @param string $table_navigation_html the current navigation HTML
  *
  * @return array ($table_navigation_html, $nbTotalPage)
  *
  * @access  private
  *
  */
 private function _getHtmlPageSelector($table_navigation_html)
 {
     $pageNow = @floor($_SESSION['tmpval']['pos'] / $_SESSION['tmpval']['max_rows']) + 1;
     $nbTotalPage = @ceil($this->__get('unlim_num_rows') / $_SESSION['tmpval']['max_rows']);
     if ($nbTotalPage > 1) {
         $table_navigation_html .= '<td>';
         $_url_params = array('db' => $this->__get('db'), 'table' => $this->__get('table'), 'sql_query' => $this->__get('sql_query'), 'goto' => $this->__get('goto'), 'is_browse_distinct' => $this->__get('is_browse_distinct'));
         //<form> to keep the form alignment of button < and <<
         // and also to know what to execute when the selector changes
         $table_navigation_html .= '<form action="sql.php' . PMA_URL_getCommon($_url_params) . '" method="post">';
         $table_navigation_html .= Util::pageselector('pos', $_SESSION['tmpval']['max_rows'], $pageNow, $nbTotalPage, 200, 5, 5, 20, 10);
         $table_navigation_html .= '</form>' . '</td>';
     }
     return array($table_navigation_html, $nbTotalPage);
 }
All Usage Examples Of PMA\libraries\Util::pageselector
Util