pagination anzahl button begrenzung

Wie erstellt man einen kompletten Pagination Code

Der hier verwendete Code

<?php class Pagination{ protected $page_offset_current, $lines_count_all, $lines_per_page, $n, $last_page_lines_count, $pages_max_num, $n_left, $n_right, $page_previous, $page_next, $page_jump_backwards, $page_jump_forwards, $left_side_nav_num, $right_side_nav_num; public function __construct($page_offset_current, $lines_count_all, $lines_per_page=25, $n=5){ $this->page_offset_current = (!$page_offset_current) ? 1 : (double)($page_offset_current); $this->lines_count_all = (double)$lines_count_all; $this->lines_per_page = (int)$lines_per_page; $this->n = (int)$n; $this->calc(); return; } public function __destruct(){ return; } final protected function calc(){ $this->pages_max_num = floor($this->lines_count_all/$this->lines_per_page); (($this->last_page_lines_count=$this->lines_count_all-$this->pages_max_num*$this->lines_per_page) < 1) or (++$this->pages_max_num); ($this->page_offset_current <= $this->pages_max_num) or ($this->page_offset_current=$this->pages_max_num); ($this->page_offset_current >= 1) or ($this->page_offset_current=1); ($this->last_page_lines_count > 0) or ($this->last_page_lines_count=$this->lines_per_page); (($this->n_left=$this->page_offset_current-$this->n) > 0) or ($this->n_left=1); (($this->n_right=($this->n_left === 1) ? $this->n*2+1 : $this->page_offset_current+$this->n) < $this->pages_max_num) or (($this->n_right=$this->pages_max_num)and ($this->n_left-=$this->n-($this->pages_max_num-$this->page_offset_current))); ($this->n_left > 0) or ($this->n_left=1); (($this->page_previous=$this->page_offset_current-1) >= 1) or ($this->page_previous=1); (($this->page_next=$this->page_offset_current+1) <= $this->pages_max_num) or ($this->page_next=$this->pages_max_num); for( $i = $this->n_left, $this->left_side_nav_num = array(); $i < $this->page_offset_current; $this->left_side_nav_num[$i] = $this->f($i), $i++ ); for( $i = $this->n_right, $this->right_side_nav_num = array(); $i > $this->page_offset_current; $this->right_side_nav_num[$i] = $this->f($i), $i-- ); asort($this->right_side_nav_num); $page_jump = ($this->pages_max_num/$this->lines_per_page)*$this->n; ( ($this->page_jump_forwards=$this->page_offset_current+$page_jump) < $this->pages_max_num and $this->pages_max_num-$this->page_jump_forwards > $this->n*2 ) or ($this->page_jump_forwards=$this->pages_max_num); ( ($this->page_jump_backwards=$this->page_offset_current-$page_jump) > 1 and $this->page_jump_backwards > $this->n*2 ) or ($this->page_jump_backwards=1); return; } final protected function f($n){ return sprintf("%.0f", $n); } final public function getPageFirst(){ return '1'; } final public function getPageLast(){ return $this->getPagesMaxNum(); } final public function getPagePrevious(){ return $this->f($this->page_previous); } final public function getPageNext(){ return $this->f($this->page_next); } final public function getPageJumpBackward(){ return $this->f($this->page_jump_backwards); } final public function getPageJumpForward(){ return $this->f($this->page_jump_forwards); } final public function getMiddle(){ return $this->getPageCurrent(); } final public function getLeft(){ return $this->left_side_nav_num; } final public function getRight(){ return $this->right_side_nav_num; } final public function getPageCurrent(){ return $this->f($this->page_offset_current); } final public function getPagesMaxNum(){ return $this->f($this->pages_max_num); } final public function getLinesCountAll(){ return $this->f($this->lines_count_all); } final public function getLinesPerPage(){ return $this->f($this->lines_per_page); } final public function getLinesLastPage(){ return $this->f($this->last_page_lines_count); } final public function getQueryOffset(){ if(($offset=$this->getPageCurrent()-1) > 0){ return $offset*$this->getLinesPerPage(); } return 0; } final public function getQueryLimit(){ return $this->getLinesPerPage(); } } include('config.php'); $lines_per_page = 25;// Anzahl der Datensätze die je Seite geladen werden sollen $n = 3; // wie viele Zahlen sollen links und rects neben der mitte sichtbar sein $tabellen_name='pfade'; $anzahl_gesamt='true';// anzahl aller Datein angeben , oder geben sie 'true' an um die Anzahl aus der DB auszulesen if($anzahl_gesamt=='true'){ $query="SELECT COUNT(*) as total FROM `$tabellen_name`"; $result = mysqli_query($mysqlineu, $query); if($result){ $lines_count_all=mysqli_fetch_assoc($result)['total']; } }else{ $lines_count_all=$anzahl_gesamt; } $page_offset_current = isset($_GET['page']) ? $_GET['page'] : 1; if($lines_count_all > $page_offset_current){ $Pagination = new Pagination($page_offset_current, $lines_count_all, $lines_per_page, $n); }else{ echo "Deine Seiten auswahl ist größer als wie Daten in der Datenbank"; return; } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Pagination</title> <style> table{ font-size:15px; color:#666; white-space:pre; font-family:monospace; border-collapse:collapse; border:1px solid black; } th,td{ border:1px solid black; } th{ background:black; color:white; font-size:20px; font-weight:600; } .pagni{ display: flex; flex-direction: row; flex-wrap: nowrap; justify-content: space-around; align-content: space-around; align-items: flex-start; border:2px solid black; padding:5px; } .buttons{ border:1px solid black; color:white; min-width:20px; text-align:center; background:black; } .buttons:hover{ border:1px solid black; box-shadow:2px 2px 2px 2px green; } .next,.weiter{ border:2px solid green; background:orange; color:black; font-weight:600; } .active{ border:3px solid green; background:green; color:white; } .pfeil{ color:#abcdef; font-weight:500; border:2px solid yellow; } </style> </head> <body> <h1>Pagination Beispiel</h1> <div> Aktuelle Seite <?php echo $Pagination->getPageCurrent(); ?> von <?php echo $Pagination->getPagesMaxNum(); ?><br> <?php echo $Pagination->getLinesPerPage(); ?> Datensätze je Seite<br> <?php echo $Pagination->getLinesLastPage(); ?> Datensätze auf der letzten Seite<br> <?php echo $Pagination->getLinesCountAll(); ?> Datensätze gesamt<br> <?php if(isset($_GET['page']) and $_GET['page']>$Pagination->getPagesMaxNum()){ echo ' Seite : '.htmlspecialchars($_GET['page']).' Gibt es nicht, zeige Daten aus letze Seite an '.$Pagination->getPagesMaxNum(); } ?> </div> <form action="" method="GET"> <h3>Springe durch angabe einer Zahl auf der Seite</h3> <input type="number" name="page" max="<?php echo $Pagination->getLinesCountAll(); ?>" value="<?php echo $Pagination->getPageCurrent(); ?>"> <input type="submit" value="go" /> </form> <table> <tr><th>Id</th><th>Erstellt am</th><th>Thema</th><th>Link zur Lösung</th><th>Link zum Forum</th></tr> <tbody> <?php $query="SELECT * FROM `$tabellen_name` ORDER BY id ASC LIMIT ".$Pagination->getQueryOffset().", ".$Pagination->getQueryLimit().""; $result = mysqli_query($mysqlineu, $query); if($result){ if (mysqli_num_rows($result) >= 1) { while ($row = mysqli_fetch_assoc($result)){ if(!empty($row['forum'])){ $for=" <td><a href='".$row['forum']."' target='_blank' rel='noopener'>Zum Forum</a></td>"; }else{ $for='<td></td>'; } echo "<tr> <td>".$row['id']."</td> <td>".date("Y-m-d H:i:s",strtotime($row['created_at']))."</td> <td>".$row['filename']."</td> <td><a href='index.php?ordner=".$row['ordner']."&id=".$row['id']."'>Zur Lösung</a></td> $for</tr>"; } }else{ echo "<tr><td colspan='4'><h3 class='no_data'>Keine Daten in Datenbank</h3></td></tr>"; } } ?> </tbody> </table> <div class="pagni"> <a class="buttons first" href="?page=<?php echo $Pagination->getPageFirst(); ?>">erste Seite</a> <a class="buttons pfeil" href="?page=<?php echo $Pagination->getPageJumpBackward(); ?>"><<</a> <a class="buttons weiter" href="?page=<?php echo $Pagination->getPagePrevious(); ?>">zurück</a> <?php foreach($Pagination->getLeft() as $number){ echo '<a class="buttons left" href="?page='.$number.'">'.$number.'</a>'; } echo '<span class="buttons active">'.$Pagination->getMiddle().'</span>'; foreach($Pagination->getRight() as $number){ echo '<a class="buttons right" href="?page='.$number.'">'.$number.'</a>'; } ?> <a class="buttons next" href="?page=<?php echo $Pagination->getPageNext(); ?>">vor</a> <a class="buttons pfeil" href="?page=<?php echo $Pagination->getPageJumpForward(); ?>">>></a> <a class="buttons last" href="?page=<?php echo $Pagination->getPageLast(); ?>">letzte Seite</a> </div> </body> </html>