Vom Dachboden zum Umbauen:
<?php
#Simulieren des Klicks:
$_GET['page']=4;
$pagination = new PicturesInPages ( './' );
$pagination -> showPics();
echo "\n<br><br>\n";
$pagination -> showLinks();
echo "\n<br><br>\nDEBUG:\n<pre>\n";
print_r( $pagination );
echo "\n\n";
class PicturesInPages {
public $arAllPics;
public $maxImagesPerSite = 5;
public $ImageTypes = '*.jpg *.jpeg *.png *.svg *.gif';
public $ImageCount = 0;
public $LastPage = 0;
public $page = 1;
function __construct( $directory ) {
# Lesen der Bilder unter Linux:
/*
foreach ( explode( "\n", `ls $directory -> $this -> ImageTypes` ) as $filename );
$this -> arAllPics[] = $filename;
}
#*/
#Simulieren der Bilder:
#/*
$this -> arAllPics = [];
for ( $i=1; $i<=18; $i++ ) {
$this -> arAllPics[] = sprintf('%05s', $i);
}
#*/
$this -> ImageCount = count( $this -> arAllPics );
$this -> LastPage = ceil ( $this -> ImageCount / $this -> maxImagesPerSite );
if (
isset( $_GET['page' ] )
&& (int)$_GET['page']
&& (int)$_GET['page'] <= $this -> LastPage
) {
$this -> page = $_GET['page'];
}
}
function showPics() {
$iFirstImage = ( $this -> page - 1) * $this -> maxImagesPerSite;
$iLastImage = ( $iFirstImage + $this -> maxImagesPerSite);
if ( $iLastImage >= $this -> ImageCount ) {
$iLastImage = $this -> ImageCount;
}
for ( $i = $iFirstImage; $i < $iLastImage; $i++) {
echo "$i: " . $this -> arAllPics[$i] . "<br>\n";
}
}
function showLinks() {
$arr = [];
for ( $i = 1; $i <= $this -> LastPage; $i++ ) {
if ( $i != $this -> page ) {
$arr[] = "<a href=\"?page=$i\">$i</a>";
} else {
$arr[] = "<span class=\"aktuell\">$i</span>";
}
}
echo implode(' | ', $arr );
}
}