======[PHP] Jump Navigator======
statischer Aufruf
Mit Navigation als Objekt
jumpByGet(".");
?>
Über die Item-Nummer. Dies wird gebraucht, falls man nicht über die PageNummer einsteigt
Die erste Item-ummer der ausgewählten Seite ist getFirstItemNrFromPageNr($currentPageNr) ?>
Die passende Navigation dazu:
jumpWithItemNr($intemNr);?>
jump($currentPageNr, $glue);
}
/**
* constructor
* @access public
* @param Number $itemCount how many items exists
* @param String $targetAdress adress of the link-target
* @param Number $itemsPerPage how many items ar on one page
* @param Number $range how many entries ar laft and right of the currentPageNr
* @param String $varName name of the var with the pageNr
*/
public function __construct($itemCount, $targetAdress = __FILE__, $itemsPerPage=10, $range=4, $varName='pageNr'){
$this->targetAdress = basename($targetAdress);
$this->varName = $varName;
$this->range = $range;
$this->itemsPerPage = $itemsPerPage;
$this->startPageNumbers = range(0, $itemCount-1, $itemsPerPage);
$this->pages=array();
}
/**
* jump to the currentPageNr
* @access public
* @param Number $currentPageNr
* @param String $glue
* @return String html-Code for the Navigation
*/
public function jump($currentPageNr, $glue = ' '){
$this->currentPageNr = $currentPageNr;
//create the list of all pageNr
$pageNrs = array_keys($this->startPageNumbers);
// create the range with all showed pageNr
$rangeStart = ($currentPageNr > $this->range) ? $currentPageNr - $this->range :0 ;
$rangeLength = 2 * $this->range +1;
$range = array_slice($pageNrs, $rangeStart, $rangeLength);
// list of all showed pageNr
$pages = array();
// add first-link '<<'
if ($currentPageNr != $firstPageNr = reset($pageNrs)) {
$pages[] = $this->createLinkInfo($firstPageNr, '<<');
}
// add previous-link '<'
if (reset($range) > $firstPageNr){
$pages[] = $this->createLinkInfo($currentPageNr-1, '<');
}
// add the page-links
foreach ($range as $pageNr){
$pages[] = $this->createLinkInfo($pageNr, $pageNr+1);
}
// add next-link '>'
if (end($range) < $lastPageNr = end($pageNrs)){
$pages[] = $this->createLinkInfo($currentPageNr+1, '>');
}
// add last-link '>>'
if ($currentPageNr != $lastPageNr){
$pages[] = $this->createLinkInfo($lastPageNr, '>>');
}
//return as string, separeted by $glue
return implode($glue, $pages);
}
/**
* jump to the currentPageNr and take the number from $_GET
* @access public
* @param String $glue
* @return String html-Code for the Navigation
*/
public function jumpByGet($glue = ' '){
return $this->jump($_GET[$this->varName], $glue);
}
/**
* jump to the current page with the itemNumber
* @access public
* @param Number $itemNr
* @param String $glue
* @return String html-Code for the Navigation
*/
public function jumpWithItemNr($itemNr, $glue = ' '){
$pageNr = floor($itemNr / $this->itemsPerPage);
return $this->jump($pageNr, $glue);
}
/**
*
* @access public
* @param Number $pageNr
* @return Number $itemNr
*/
public function getFirstItemNrFromPageNr($pageNr){
return $this->startPageNumbers[$pageNr];
}
/**
* create the html-link for the pageNr
* @access protected
* @param Number $pageNr
* @param String $label
* @return String
*/
protected function createLinkInfo($pageNr, $label){
if ($pageNr == $this->currentPageNr) $label = "{$label}";
return "targetAdress}?{$this->varName}={$pageNr}\">{$label}";
}
}
?>
Ein weiterer Testfall
jump($currentPageNr);
//Erste anzuzeigende ID auslesen
$firstId = $nav->getFirstItemNrFromPageNr($currentPageNr);
//Dei entsprechenden Zeilen aussuchen
$selectedRows = array_slice($rows, $firstId, C_ITEM_PER_PAGE);
//Datenausgabe
echo $navBar;
echo "
";
echo implode('
', $selectedRows);
echo "
";
echo $navBar;
?>
----
Kategorien: [[CatPhp]]