Ik ben bezig naar de gekregen kritiek te luisteren, maar dat lukt nog niet helemaal.

Ik heb m'n klasse nu opgesplitst in 3 klassen, echter doet dat ding nu weer 's helemaal niks. Hij geeft zelfs geen fout...

Ik heb al wat dingen geprobeerd om erachter te komen waar iets fout moet zitten. Maar volgens die methodes moet overal een fout zitten? :-S

<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors');

// <title>Navigatie Klasse</title>

include("phphulp/config.php");


class Navigation {
// current page, total pages and pagination pages amount.
protected $iCurrent;
protected $iTotal;

// limitation vars
protected $iStart;
protected $iShowRecords;
protected $iShowNumbers;

// query vars by using setQuery and setQueryCountField()
protected $sCountQuery;
protected $sQueryCountField = 'aantal';

// query vars by using setQueryTable and setQueryWhereStatement()
protected $sTable;
protected $sWhereStatement;

// parameter var
protected $sGetParameter;

// output vars
protected $aPagination = array();
protected $aToStringReplacements = array();
protected $aWords = array('first'=>'First','prev'=>'Previous','next'=>'Next','last'=>'Last');
protected $sToStringRegex;
protected $sSpace = '...';

// construction
public function __construct($iCurrent, $iShowRecords = 20, $iShowNumbers = 6) {
$this->iCurrent = ($iCurrent >= 0) ? (int)$iCurrent : 0;
$this->iShowRecords = ($iShowRecords > 0) ? (int)$iShowRecords : 20;
$this->iShowNumbers = ($iShowRecords > 0 && $iShowRecords % 2 == 0) ? (int)$iShowNumbers : 6;
}

// set style for the toString function.
public function setToStringRegex($sRegex, $aReplacements) {
$this->sToStringRegex = (string)trim($sRegex);
$this->aToStringReplacements = $aReplacements;
}

// set $_GET parameter
public function setGetParameter($sString) {
$this->sGetParameter = (string)trim($sString);
}

// set ...
public function setSpace($sString) {
$this->sSpace = (string)trim($sString);
}

// set words, first, next, previous, last.
public function setWord($sEnglishWord, $sWord) {
if($sEnglishWord == 'first') {
$sArrayKey = 'first';
} else {
$sArrayKey = substr($sWord, 0, 4);
}
}

// fill array and vars
public function do() {
if(empty($this->sCountQuery)) {
if(!empty($this->sTable)) {
$this->createQuery();
} else {
throw new Exception('First enter a table to be searched in.');
}
}

$this->calculateTotalPages();
$this->setNavigation();
}

// set next page in the pagination array.
protected function setNext() {
if($this->iCurrent == $this->iTotal) {
$this->aPagination['next'] = false;
} else {
$this->aPagination['next'] = $this->iCurrent+1;
}
}

// set last page in the pagination array.
protected function setLast() {
if($this->iCurrent == $this->iTotal) {
$this->aPagination['last'] = false;
} else {
$this->aPagination['last'] = $this->iTotal;
}
}

// set previous page in the pagination array.
protected function setPrevious() {
if($this->iCurrent == 0) {
$this->aPagination['prev'] = false;
} else {
$this->aPagination['prev'] = $this->iCurrent-1;
}
}

// set first page in the pagination array.
protected function setFirst() {
if($this->iCurrent == 0) {
$this->aPagination['first'] = false;
} else {
$this->aPagination['first'] = 0;
}
}

// set points in the pagination array.
protected function setSpaces() {
$iNumbersRound = ceil($this->iShowNumbers / 2);

if(($this->iCurrent - $iNumbersRound) <= 0) {
$this->aPagination['space_before'] = false;
} else {
$this->aPagination['space_before'] = $this->sSpace;
}

if(($this->iCurrent + $iNumbersRound) >= $this->iTotal) {
$this->aPagination['space_after'] = false;
} else {
$this->aPagination['space_after'] = $this->sSpace;
}
}

// set the pagination numbers in the pagination array.
protected function setNumbers() {
$iNumbersRound = ceil($this->iShowNumbers / 2);

$iStart = ($this->iCurrent - $iNumbersRound) <= 0 ? 0 : ($this->iCurrent - $iNumbersRound);
$iEnd = ($this->iCurrent + $iNumbersRound) >= $this->iTotal ? $this->iTotal : ($this->iCurrent + $iNumbersRound);

for($i=$iStart;$i<=$iEnd;$i++) {
if($i == $this->iCurrent) $this->aPagination['current'] = $i;

$this->aPagination['numbers'][] = $i;
}
}

// set everything by using the six functions above.
protected function setNavigation() {
$this->setFirst();
$this->setLast();
$this->setNext();
$this->setPrevious();
$this->setSpaces();
$this->setNumbers();
}

// zip everything to a string, can be used by the inexperienced programmer.
// there is also an expert version; use the stringReplacement methods to set up your own style.
public function __toString() {
if(!empty($this->sToStringRegex)) {
$sString = $this->sToStringRegex;
foreach($this->aToStringReplacements as $sRegex => $sReplace) {
$sString = str_replace($sRegex, $sReplace, $sString);
}
} else {
$aPags = NavigationOutput::getNavigation();
$sString = '';

if(is_bool(NavigationOutput::getFirst()))
$sString .= $aWords['first'];
else
$sString .= '<a href="?'.$this->sGetParameter.'='.$aPags['first'].'">'.$aWords['first'].'</a> | ';

if(is_bool(NavigationOutput::getPrevious()))
$sString .= $aWords['prev'];
else
$sString .= '<a href="?'.$this->sGetParameter.'='.$aPags['prev'].'">'.$aWords['prev'].'</a> | ';

$sString .= NavigationOutput::getSpaceBefore();

foreach($aPags['numbers'] as $iNum) {
if($iNum == NavigationOutput::getCurrent()) {
$sString .= ' ['.($iNum+1).'] ';
} else {
$sString .= ' <a href="?'.$this->sGetParameter.'='.$iNum.'">'.($iNum+1).'</a> ';
}
}

$sString .= NavigationOutput::getSpaceAfter();

if(is_bool(NavigationOutput::getNext()))
$sString .= $aWords['next'];
else
$sString .= ' | <a href="?'.$this->sGetParameter.'='.$aPags['next'].'">'.$aWords['next'].'</a>';

if(is_bool(NavigationOutput::getLast()))
$sString .= $aWords['last'];
else
$sString .= ' | <a href="?'.$this->sGetParameter.'='.$aPags['last'].'">'.$aWords['last'].'</a>';
}

return $sString;
}
}

class NavigationDatabase extends Navigation {
// only use setQuery() and setQueryCountField()...
static public function setQuery($sQuery) {
parent::sCountQuery = (string)trim($sQuery);
}

// set fieldname which contains the query count result, standard value: aantal.
static public function setQueryCountField($sField) {
parent::sQueryCountField = (string)trim($sField);
}

// ...or setQueryTable() and setQueryWhereStatement()
static public function setQueryTable($sTable) {
parent::sTable = (string)trim($sTable);
}

// set where statement for the query to be created with createQuery. If there's anything elses like JOINs used in the query, please use setQuery instead of these, cause setQuery is much more flexible to use with that kinds of queries.
static public function setQueryWhereStatement($sWhere) {
parent::sWhereStatement = (string)trim($sWhere);
}

// only execute when setQuery() isn't used.
static protected function createQuery() {
parent::sCountQuery = "SELECT COUNT(*) AS ".parent::sQueryCountField." FROM ".parent::sTable;

if(!empty($this->sWhereStatement)) {
parent::sCountQuery .= " WHERE ".parent::sWhereStatement;
}
}

// calculate total pages amount.
static protected function calculateTotalPages() {
$sResult = mysql_query(parent::sCountQuery);
if($sResult) {
if(mysql_num_rows($sResult) > 0) {
$sRij = mysql_fetch_assoc($sResult);

parent::iTotal = ceil($sRij[parent::sQueryCountField]/parent::iShowRecords)-1;
} else {
parent::iTotal = 0;
}
} else {
parent::iTotal = 0;
}
}

// return the array, if everything completes, containing the results of the query of the showed page.
static public function getQueryResult($sQuery) {
$sResult = mysql_query($sQuery);
if($sResult) {
$aReturnArray = array();
if(mysql_num_rows($sResult) > 0) {
while($sRij = mysql_fetch_assoc($sResult)) {
$aReturnArray[] = $sRij;
}
}
} else {
throw new Exception(mysql_error().' in query: '.$sQuery);
}

return $aReturnArray;
}

// when you create the query by your own, but not knowing how to use the LIMIT-statement, we make it for you.
static public function getLimit() {
return ' LIMIT '.parent::iStart.','.parent::iShowRecords;
}
}

class NavigationOutput extends Navigation {
// get the pagination array. this can also be used by the user of this class.
static public function getNavigation() {
return parent::aPagination;
}

// get next page in the array.
static public function getNext() {
return parent::aPagination['next'];
}

// get last page in the array.
static public function getLast() {
return parent::aPagination['last'];
}

// get previous page in the array.
static public function getPrevious() {
return parent::aPagination['prev'];
}

// get first page in the array.
static public function getFirst() {
return parent::aPagination['first'];
}

// get the pagination numbers in the array.
static public function getNumbers() {
return parent::aPagination['numbers'];
}

// get the points before the numbers in the array.
static public function getSpaceBefore() {
return parent::aPagination['space_before'];
}

// get the points after the numbers in the array.
static public function getSpaceAfter() {
return parent::aPagination['space_after'];
}
}

if(isset($_GET['pag'])) {
$iPage = (int)$_GET['pag'];
} else {
$iPage = 0;
}

$oPagination = new Navigation($iPage, 10, 2);
NavigationDatabase::setQueryTable('alfabet');

try {
$oPagination->do();
$bContinue = true;
} catch(Exception $e) {
echo $e->getMessage();
$bContinue = false;
}

if($bContinue === true) {
$oPagination->setGetParameter('pag');
$oPagination->setToStringRegex('*f | *p | *sb *n *sa | *x | *l',
array(
'*f'=>NavigationOutput::getFirst(),
'*p'=>NavigationOutput::getPrevious(),
'*sb'=>NavigationOutput::getSpaceBefore(),
'*n'=>NavigationOutput::getNumbers(),
'*sa'=>NavigationOutput::getSpaceAfter(),
'*x'=>NavigationOutput::getNext(),
'*l'=>NavigationOutput::getLast()
)
);

try {
$sQuery = "SELECT * FROM alfabet".NavigationDatabase::getLimit();
$sArray = NavigationDatabase::getQueryResult($sQuery);
} catch(Exception $e) {
$sArray = '';
echo $e->getMessage();
}

if(is_array($sArray)) {
if(!empty($sArray)) {
foreach($sArray as $iKey => $aValue) {
echo $aValue['id'].'. '.$aValue['letter'].'<br />';
}

echo '<div>'.$oPagination.'</div>';
} else {
echo 'Geen records gevonden.';
}
}
}

?>

Iemand een idee wat er aan de hand zou kunnen zijn?
Mag ik vragen waarom je de NavigationDatabase static hebt gemaakt? Ik heb het even vluchtig bekeken en het lijkt erop alsof het nu niet mogelijk is om 2 verschillende pagination's binnen één pagina te hebben.
Je moet jezelf eens goed gaan afvragen wanneer je een variabele of een methode als static of gewoon wilt hebben.

Ik denk dat je het onderscheid tussen die 2 nog niet goed begrijpt. En waarschijnlijk zeker niet als ik je er nog bij vertel dat er nog iets bestaat als const ook :p
$this-> werkt als je object-georiënteerd werkt.

Als je echt objecten maakt van de klassen. Als je statisch werkt, werk je best met self::

Ik gebruik parent maar voor 1 ding: het uitvoeren van de constructor van de parent.
een voorbeeld:
Ik heb een klasse pagina. Dat is dus een html pagina, met content, titel, javascript data; methodes voor output, converten van gegevens, ...

Een pagina met een gastenboek wordt dan een eigen klasse, als extend van die klasse pagina.

Een extend kan zomaar alle methodes van de parent gebruiken, behalve de contructor (van de parent dus). Die wordt nooit uitgevoerd. Wel, daarvoor laat ik in de constructor van de extended pagina klasse ook de constructor van de parent uitvoeren, omdat ik daar een hoop initialiseer.

Dat is dus met

<?php
...
class gastenboek extends pagina
public function __construct()
{
parent::_construct();
// ... rest van de functie
...
?>

Achteraf gezien, had ik die initialisering beter in een aparte methode init() gestoken. Kijk, dan heb je parent:: nergens meer voor nodig.
Hm, ik denk dat ik static nu beter snap inderdaad. Is dat niet een compleet losstaande functie die verder niets met de klasse te maken heeft? En dat ie daarom aangeeft dat je je niet in een object-omgeving bevindt, omdat die functie op zichzelf staat, maar verzamelt is in een klasse die verder geen gezamelijk punt kent.

Hm.. Ik heb de static dingen nu maar weggehaald, want die kloppen niet. En nu kan $this wel :-), omdat ik me nu in een klasse-omgeving bevind, gok ik?

Het is nu alleen, hij geeft aan dat ik geen querytabel heb gedefinieerd, wat ik wel doe. Maar ik gok dat dit iets te maken heeft met het feit dat ik een nieuw object aanmaak, waardoor hij de gegevens niet bij de 'hoofdklasse' Navigation opslaat, maar bij een aparte?

Dit is nu mijn code:

<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);

// <title>Navigatie Klasse</title>

include("phphulp/config.php");


class Navigation {
// current page, total pages and pagination pages amount.
protected $iCurrent;
protected $iTotal;

// limitation vars
protected $iStart;
protected $iShowRecords;
protected $iShowNumbers;

// query vars by using setQuery and setQueryCountField()
protected $sCountQuery;
protected $sQueryCountField = 'aantal';

// query vars by using setQueryTable and setQueryWhereStatement()
protected $sTable;
protected $sWhereStatement;

// parameter var
protected $sGetParameter;

// output vars
protected $aPagination = array();
protected $aToStringReplacements = array();
protected $aWords = array('first'=>'First','prev'=>'Previous','next'=>'Next','last'=>'Last');
protected $sToStringRegex;
protected $sSpace = '...';

// construction
public function __construct($iCurrent, $iShowRecords = 20, $iShowNumbers = 6) {
$this->iCurrent = ($iCurrent >= 0) ? (int)$iCurrent : 0;
$this->iShowRecords = ($iShowRecords > 0) ? (int)$iShowRecords : 20;
$this->iShowNumbers = ($iShowRecords > 0 && $iShowRecords % 2 == 0) ? (int)$iShowNumbers : 6;
}

// set style for the toString function.
public function setToStringRegex($sRegex, $aReplacements) {
$this->sToStringRegex = (string)trim($sRegex);
$this->aToStringReplacements = $aReplacements;
}

// set $_GET parameter
public function setGetParameter($sString) {
$this->sGetParameter = (string)trim($sString);
}

// set ...
public function setSpace($sString) {
$this->sSpace = (string)trim($sString);
}

// set words, first, next, previous, last.
public function setWord($sEnglishWord, $sWord) {
if($sEnglishWord == 'first') {
$sArrayKey = 'first';
} else {
$sArrayKey = substr($sWord, 0, 4);
}
}

// fill array and vars
public function doExecute() {
if(empty($this->sCountQuery)) {
if(!empty($this->sTable)) {
$this->createQuery();
} else {
throw new Exception('First enter a table to be searched in.');
}
}

$this->calculateTotalPages();
$this->setNavigation();
}

// set next page in the pagination array.
protected function setNext() {
if($this->iCurrent == $this->iTotal) {
$this->aPagination['next'] = false;
} else {
$this->aPagination['next'] = $this->iCurrent+1;
}
}

// set last page in the pagination array.
protected function setLast() {
if($this->iCurrent == $this->iTotal) {
$this->aPagination['last'] = false;
} else {
$this->aPagination['last'] = $this->iTotal;
}
}

// set previous page in the pagination array.
protected function setPrevious() {
if($this->iCurrent == 0) {
$this->aPagination['prev'] = false;
} else {
$this->aPagination['prev'] = $this->iCurrent-1;
}
}

// set first page in the pagination array.
protected function setFirst() {
if($this->iCurrent == 0) {
$this->aPagination['first'] = false;
} else {
$this->aPagination['first'] = 0;
}
}

// set points in the pagination array.
protected function setSpaces() {
$iNumbersRound = ceil($this->iShowNumbers / 2);

if(($this->iCurrent - $iNumbersRound) <= 0) {
$this->aPagination['space_before'] = false;
} else {
$this->aPagination['space_before'] = $this->sSpace;
}

if(($this->iCurrent + $iNumbersRound) >= $this->iTotal) {
$this->aPagination['space_after'] = false;
} else {
$this->aPagination['space_after'] = $this->sSpace;
}
}

// set the pagination numbers in the pagination array.
protected function setNumbers() {
$iNumbersRound = ceil($this->iShowNumbers / 2);

$iStart = ($this->iCurrent - $iNumbersRound) <= 0 ? 0 : ($this->iCurrent - $iNumbersRound);
$iEnd = ($this->iCurrent + $iNumbersRound) >= $this->iTotal ? $this->iTotal : ($this->iCurrent + $iNumbersRound);

for($i=$iStart;$i<=$iEnd;$i++) {
if($i == $this->iCurrent) $this->aPagination['current'] = $i;

$this->aPagination['numbers'][] = $i;
}
}

// set everything by using the six functions above.
protected function setNavigation() {
$this->setFirst();
$this->setLast();
$this->setNext();
$this->setPrevious();
$this->setSpaces();
$this->setNumbers();
}

// zip everything to a string, can be used by the inexperienced programmer.
// there is also an expert version; use the stringReplacement methods to set up your own style.
public function __toString() {
if(!empty($this->sToStringRegex)) {
$sString = $this->sToStringRegex;
foreach($this->aToStringReplacements as $sRegex => $sReplace) {
$sString = str_replace($sRegex, $sReplace, $sString);
}
} else {
$aPags = NavigationOutput::getNavigation();
$sString = '';

if(is_bool(NavigationOutput::getFirst()))
$sString .= $aWords['first'];
else
$sString .= '<a href="?'.$this->sGetParameter.'='.$aPags['first'].'">'.$aWords['first'].'</a> | ';

if(is_bool(NavigationOutput::getPrevious()))
$sString .= $aWords['prev'];
else
$sString .= '<a href="?'.$this->sGetParameter.'='.$aPags['prev'].'">'.$aWords['prev'].'</a> | ';

$sString .= NavigationOutput::getSpaceBefore();

foreach($aPags['numbers'] as $iNum) {
if($iNum == NavigationOutput::getCurrent()) {
$sString .= ' ['.($iNum+1).'] ';
} else {
$sString .= ' <a href="?'.$this->sGetParameter.'='.$iNum.'">'.($iNum+1).'</a> ';
}
}

$sString .= NavigationOutput::getSpaceAfter();

if(is_bool(NavigationOutput::getNext()))
$sString .= $aWords['next'];
else
$sString .= ' | <a href="?'.$this->sGetParameter.'='.$aPags['next'].'">'.$aWords['next'].'</a>';

if(is_bool(NavigationOutput::getLast()))
$sString .= $aWords['last'];
else
$sString .= ' | <a href="?'.$this->sGetParameter.'='.$aPags['last'].'">'.$aWords['last'].'</a>';
}

return $sString;
}
}

class NavigationDatabase extends Navigation {
public function __construct() { }

// only use setQuery() and setQueryCountField()...
public function setQuery($sQuery) {
$this->sCountQuery = trim($sQuery);
}

// set fieldname which contains the query count result, standard value: aantal.
public function setQueryCountField($sField) {
$this->sQueryCountField = trim($sField);
}

// ...or setQueryTable() and setQueryWhereStatement()
public function setQueryTable($sTable) {
$this->sTable = trim($sTable);
}

// set where statement for the query to be created with createQuery. If there's anything elses like JOINs used in the query, please use setQuery instead of these, cause setQuery is much more flexible to use with that kinds of queries.
public function setQueryWhereStatement($sWhere) {
$this->sWhereStatement = trim($sWhere);
}

// only execute when setQuery() isn't used.
protected function createQuery() {
$this->sCountQuery = "SELECT COUNT(*) AS ".$this->sQueryCountField." FROM ".$this->sTable;

if(!empty($this->$sWhereStatement)) {
$this->sCountQuery .= " WHERE ".$this->sWhereStatement;
}
}

// calculate total pages amount.
protected function calculateTotalPages() {
$sResult = mysql_query($this->sCountQuery);
if($sResult) {
if(mysql_num_rows($sResult) > 0) {
$sRij = mysql_fetch_assoc($sResult);

$this->iTotal = ceil($sRij[$this->sQueryCountField]/$this->iShowRecords)-1;
} else {
$this->iTotal = 0;
}
} else {
$this->iTotal = 0;
}
}

// return the array, if everything completes, containing the results of the query of the showed page.
public function getQueryResult($sQuery) {
$sResult = mysql_query($sQuery);
if($sResult) {
$aReturnArray = array();
if(mysql_num_rows($sResult) > 0) {
while($sRij = mysql_fetch_assoc($sResult)) {
$aReturnArray[] = $sRij;
}
}
} else {
throw new Exception(mysql_error().' in query: '.$sQuery);
}

return $aReturnArray;
}

// when you create the query by your own, but not knowing how to use the LIMIT-statement, we make it for you.
public function getLimit() {
return ' LIMIT '.$this->iStart.','.$this->iShowRecords;
}
}

class NavigationOutput extends Navigation {
public function __construct() { }

// get the pagination array. this can also be used by the user of this class.
public function getNavigation() {
return $this->aPagination;
}

// get next page in the array.
public function getNext() {
return $this->aPagination['next'];
}

// get last page in the array.
public function getLast() {
return $this->aPagination['last'];
}

// get previous page in the array.
public function getPrevious() {
return $this->aPagination['prev'];
}

// get first page in the array.
public function getFirst() {
return $this->aPagination['first'];
}

// get the pagination numbers in the array.
public function getNumbers() {
return $this->aPagination['numbers'];
}

// get the points before the numbers in the array.
public function getSpaceBefore() {
return $this->aPagination['space_before'];
}

// get the points after the numbers in the array.
public function getSpaceAfter() {
return $this->aPagination['space_after'];
}
}

if(isset($_GET['pag'])) {
$iPage = (int)$_GET['pag'];
} else {
$iPage = 0;
}

$oPagination = new Navigation($iPage, 10, 2);
$oDatabase = new NavigationDatabase();
$oOutput = new NavigationOutput();

$oDatabase->setQueryTable('alfabet');

try {
$oPagination->doExecute();
$bContinue = true;
} catch(Exception $e) {
echo $e->getMessage();
$bContinue = false;
}

if($bContinue === true) {
$oPagination->setGetParameter('pag');
$oPagination->setToStringRegex('*f | *p | *sb *n *sa | *x | *l',
array(
'*f'=>$oOutput->getFirst(),
'*p'=>$oOutput->getPrevious(),
'*sb'=>$oOutput->getSpaceBefore(),
'*n'=>$oOutput->getNumbers(),
'*sa'=>$oOutput->getSpaceAfter(),
'*x'=>$oOutput->getNext(),
'*l'=>$oOutput->getLast()
)
);

try {
$sQuery = "SELECT * FROM alfabet".$oDatabase->getLimit();
$sArray = $oDatabase->getQueryResult($sQuery);
} catch(Exception $e) {
$sArray = '';
echo $e->getMessage();
}

if(is_array($sArray)) {
if(!empty($sArray)) {
foreach($sArray as $iKey => $aValue) {
echo $aValue['id'].'. '.$aValue['letter'].'<br />';
}

echo '<div>'.$oPagination.'</div>';
} else {
echo 'Geen records gevonden.';
}
}
}
?>
Toon vooral eens hoe je je klassen gebruikt.

Daaraan zie je of je klasse goed in mekaar steekt.

Je zou kunnen zeggen: een klasse groepeert functies; een object groepeert variabelen met hun waarde.

Een statische class kan nuttig zijn wanneer je een aantal functies wil groeperen. Bv. een statistiek class waar je gemiddelde(array $gegevens), mod(array $gegevens), med(array $gegevens), ...

Wat je bij een object doet, is de eigenschappen stap voor stap een juiste waarde geven.

Je maakt een nieuw object aan bv. $m_formulier = new form();
Dan geef je het object de juiste waarden, bv.
$m_formulier->addInput('titel, ''text', $_POST['titel']); $m_formulier->addInput('bericht', 'textarea', $_POST['bericht']);
$m_formulier->addInput('tijd', 'hidden', 'now'); ...

Op het einde kan je het object, dat nu helemaal opgebouwd is, laten doen wat het moet doen.

Bv. $m_formulier->insertIntoDB();
of

echo $m_formulier->getOutput();
Ja. Ik heb nog flink zitten stoeien, maar het is gelukt. Nu werkt 'ie helemaal volledig, misschien nog wel beter dan voorheen :-). Het enige is dat $iShowNumbers even moet zijn, dat kon je toch doen door $iShowNumbers % 2 == 0? Dat dacht ik in ieder geval, maar dat werkt dus niet. Dat is nog wel het enige wat er bij moet, de rest zit er in:

<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);

// <title>Navigatie Klasse</title>

include("phphulp/config.php");


class Navigation {
// current page, total pages and pagination pages amount.
protected $iCurrent;
protected $iTotal;

// limitation vars
protected $iStart;
protected $iShowRecords;
protected $iShowNumbers;

// query vars by using setQuery and setQueryCountField()
protected $sCountQuery;
protected $sQueryCountField = 'aantal';

// query vars by using setQueryTable and setQueryWhereStatement()
protected $sTable;
protected $sWhereStatement;

// parameter var
protected $sGetParameter;

// output vars
protected $aPagination = array();
protected $aToStringReplacements = array();
protected $aWords = array('first'=>'First','prev'=>'Previous','next'=>'Next','last'=>'Last');
protected $sToStringRegex;
protected $sCurrentStringRegex;
protected $sSpace = '...';

// construction
public function __construct($iCurrent, $iShowRecords = 20, $iShowNumbers = 6) {
$this->iCurrent = ($iCurrent >= 0) ? (int)$iCurrent : 0;
$this->iShowRecords = ($iShowRecords > 0) ? (int)$iShowRecords : 20;
$this->iShowNumbers = ($iShowRecords > 0) ? (int)$iShowNumbers : 6;
$this->iStart = $iCurrent * $iShowRecords;
}

public function setCurrentStringRegex($sCurrent) {
$this->sCurrentStringRegex = trim($sCurrent);
}

// set style for the toString function.
public function setToStringRegex($sRegex, $aReplacements) {
$this->sToStringRegex = (string)trim($sRegex);
$this->aToStringReplacements = $aReplacements;
}

// set $_GET parameter
public function setGetParameter($sString) {
$this->sGetParameter = (string)trim($sString);
}

// set ...
public function setSpace($sString) {
$this->sSpace = (string)trim($sString);
}

// set words, first, next, previous, last.
public function setWord($sEnglishWord, $sWord) {
if($sEnglishWord == 'first') {
$sArrayKey = 'first';
} else {
$sArrayKey = substr($sEnglishWord, 0, 4);
}

$this->aWords[$sArrayKey] = trim($sWord);
}

// fill array and vars
public function doExecute() {
$oDatabase = new NavigationDatabase($this);

if(empty($this->sCountQuery)) {
if(!empty($this->sTable)) {
$oDatabase->createQuery();
} else {
throw new Exception('First enter a table to be searched in.');
}
}

$oDatabase->calculateTotalPages();
$this->setNavigation();
}

// set next page in the pagination array.
protected function setNext() {
if($this->iCurrent == $this->iTotal) {
$this->aPagination['next'] = false;
} else {
$this->aPagination['next'] = $this->iCurrent+1;
}
}

// set last page in the pagination array.
protected function setLast() {
if($this->iCurrent == $this->iTotal) {
$this->aPagination['last'] = false;
} else {
$this->aPagination['last'] = $this->iTotal;
}
}

// set previous page in the pagination array.
protected function setPrevious() {
if($this->iCurrent == 0) {
$this->aPagination['prev'] = false;
} else {
$this->aPagination['prev'] = $this->iCurrent-1;
}
}

// set first page in the pagination array.
protected function setFirst() {
if($this->iCurrent == 0) {
$this->aPagination['first'] = false;
} else {
$this->aPagination['first'] = 0;
}
}

// set points in the pagination array.
protected function setSpaces() {
$iNumbersRound = ceil($this->iShowNumbers / 2);

if(($this->iCurrent - $iNumbersRound) <= 0) {
$this->aPagination['space_before'] = false;
} else {
$this->aPagination['space_before'] = $this->sSpace;
}

if(($this->iCurrent + $iNumbersRound) >= $this->iTotal) {
$this->aPagination['space_after'] = false;
} else {
$this->aPagination['space_after'] = $this->sSpace;
}
}

// set the pagination numbers in the pagination array.
protected function setNumbers() {
$iNumbersRound = floor($this->iShowNumbers / 2);

$iStart = ($this->iCurrent - $iNumbersRound) <= 0 ? 0 : ($this->iCurrent - $iNumbersRound);
$iEnd = ($this->iCurrent + $iNumbersRound) >= $this->iTotal ? $this->iTotal : ($this->iCurrent + $iNumbersRound);

for($i=$iStart;$i<=$iEnd;$i++) {
if($i == $this->iCurrent)
$this->aPagination['current'] = $i;

$this->aPagination['numbers'][] = $i;
}
}

// set everything by using the six functions above.
protected function setNavigation() {
$this->setFirst();
$this->setLast();
$this->setNext();
$this->setPrevious();
$this->setSpaces();
$this->setNumbers();
}

// zip everything to a string, can be used by the inexperienced programmer.
// there is also an expert version; use the stringReplacement methods to set up your own style.
public function __toString() {
if(!empty($this->sToStringRegex)) {
$sString = $this->sToStringRegex;
foreach($this->aToStringReplacements as $sRegex => $sReplace) {
$sString = str_replace($sRegex, $sReplace, $sString);
}
} else {
$aPags = NavigationOutput::getNavigation();
$sString = '';

if(is_bool(NavigationOutput::getFirst()))
$sString .= $aWords['first'];
else
$sString .= '<a href="?'.$this->sGetParameter.'='.$aPags['first'].'">'.$aWords['first'].'</a> | ';

if(is_bool(NavigationOutput::getPrevious()))
$sString .= $aWords['prev'];
else
$sString .= '<a href="?'.$this->sGetParameter.'='.$aPags['prev'].'">'.$aWords['prev'].'</a> | ';

$sString .= NavigationOutput::getSpaceBefore();

foreach($aPags['numbers'] as $iNum) {
if($iNum == NavigationOutput::getCurrent()) {
$sString .= ' ['.($iNum+1).'] ';
} else {
$sString .= ' <a href="?'.$this->sGetParameter.'='.$iNum.'">'.($iNum+1).'</a> ';
}
}

$sString .= NavigationOutput::getSpaceAfter();

if(is_bool(NavigationOutput::getNext()))
$sString .= $aWords['next'];
else
$sString .= ' | <a href="?'.$this->sGetParameter.'='.$aPags['next'].'">'.$aWords['next'].'</a>';

if(is_bool(NavigationOutput::getLast()))
$sString .= $aWords['last'];
else
$sString .= ' | <a href="?'.$this->sGetParameter.'='.$aPags['last'].'">'.$aWords['last'].'</a>';
}

return $sString;
}
}

class NavigationDatabase extends Navigation {
protected $oParent;

public function __construct($oNavigation) {
$this->oParent = $oNavigation;
}

// only use setQuery() and setQueryCountField()...
public function setQuery($sQuery) {
$this->oParent->sCountQuery = trim($sQuery);
}

// set fieldname which contains the query count result, standard value: aantal.
public function setQueryCountField($sField) {
$this->oParent->sQueryCountField = trim($sField);
}

// ...or setQueryTable() and setQueryWhereStatement()
public function setQueryTable($sTable) {
$this->oParent->sTable = trim($sTable);
}

// set where statement for the query to be created with createQuery. If there's anything elses like JOINs used in the query, please use setQuery instead of these, cause setQuery is much more flexible to use with that kinds of queries.
public function setQueryWhereStatement($sWhere) {
$this->oParent->sWhereStatement = trim($sWhere);
}

// only execute when setQuery() isn't used.
protected function createQuery() {
$this->oParent->sCountQuery = "SELECT COUNT(*) AS ".$this->oParent->sQueryCountField." FROM ".$this->oParent->sTable;

if(!empty($this->oParent->sWhereStatement)) {
$this->oParent->sCountQuery .= " WHERE ".$this->oParent->sWhereStatement;
}
}

// calculate total pages amount.
protected function calculateTotalPages() {
$sResult = mysql_query($this->oParent->sCountQuery);
if($sResult) {
if(mysql_num_rows($sResult) > 0) {
$sRij = mysql_fetch_assoc($sResult);

$this->oParent->iTotal = ceil($sRij[$this->oParent->sQueryCountField]/$this->oParent->iShowRecords)-1;
} else {
$this->oParent->iTotal = 0;
}
} else {
$this->oParent->iTotal = 0;
}
}

// return the array, if everything completes, containing the results of the query of the showed page.
public function getQueryResult($sQuery) {
$sResult = mysql_query($sQuery);
if($sResult) {
$aReturnArray = array();
if(mysql_num_rows($sResult) > 0) {
while($sRij = mysql_fetch_assoc($sResult)) {
$aReturnArray[] = $sRij;
}
}
} else {
throw new Exception(mysql_error().' in query: '.$sQuery);
}

return $aReturnArray;
}

// when you create the query by your own, but not knowing how to use the LIMIT-statement, we make it for you.
public function getLimit() {
return ' LIMIT '.$this->oParent->iStart.','.$this->oParent->iShowRecords;
}
}

class NavigationOutput extends Navigation {
protected $oParent;

public function __construct($oNavigation) {
$this->oParent = $oNavigation;
}

// get the pagination array. this can also be used by the user of this class.
public function getNavigation() {
return $this->oParent->aPagination;
}

public function getCurrent() {
return str_replace('*c', ($this->oParent->iCurrent+1), $this->oParent->sCurrentStringRegex);
}

// get next page in the array.
public function getNext() {
if($this->oParent->aPagination['next'] === false) {
return $this->oParent->aWords['next'];
} else {
return '<a href="?'.$this->oParent->sGetParameter.'='.$this->oParent->aPagination['next'].'">'.$this->oParent->aWords['next'].'</a>';
}
}

// get last page in the array.
public function getLast() {
if($this->oParent->aPagination['last'] === false) {
return $this->oParent->aWords['last'];
} else {
return '<a href="?'.$this->oParent->sGetParameter.'='.$this->oParent->aPagination['last'].'">'.$this->oParent->aWords['last'].'</a>';
}
}

// get previous page in the array.
public function getPrevious() {
if($this->oParent->aPagination['prev'] === false) {
return $this->oParent->aWords['prev'];
} else {
return '<a href="?'.$this->oParent->sGetParameter.'='.$this->oParent->aPagination['prev'].'">'.$this->oParent->aWords['prev'].'</a>';
}
}

// get first page in the array.
public function getFirst() {
if($this->oParent->aPagination['first'] === false) {
return $this->oParent->aWords['first'];
} else {
return '<a href="?'.$this->oParent->sGetParameter.'='.$this->oParent->aPagination['first'].'">'.$this->oParent->aWords['first'].'</a>';
}
}

// get the pagination numbers in the array.
public function getNumbers() {
if(empty($this->oParent->aPagination['numbers'])) {
return '';
} else {
$sString = '';
foreach($this->oParent->aPagination['numbers'] as $sKey => $sVal) {
if($this->oParent->iCurrent == $sVal)
$sString .= $this->getCurrent().' ';
else
$sString .= '<a href="?'.$this->oParent->sGetParameter.'='.$sVal.'">'.($sVal+1).'</a> ';
}
return trim($sString);
}
}

// get the points before the numbers in the array.
public function getSpaceBefore() {
return $this->oParent->aPagination['space_before'];
}

// get the points after the numbers in the array.
public function getSpaceAfter() {
return $this->oParent->aPagination['space_after'];
}
}

try {
if(isset($_GET['pag'])) {
$iPage = (int)$_GET['pag'];
} else {
$iPage = 0;
}

$oPagination = new Navigation($iPage, 5, 4);
$oDatabase = new NavigationDatabase($oPagination);
$oOutput = new NavigationOutput($oPagination);

$oDatabase->setQueryTable('alfabet');
$oPagination->doExecute();
$oPagination->setGetParameter('pag');

$oPagination->setWord('first', 'eerste');
$oPagination->setWord('previous', 'vorige');
$oPagination->setWord('next', 'volgende');
$oPagination->setWord('last', 'laatste');

$oPagination->setCurrentStringRegex('[*c]');
$oPagination->setToStringRegex('*f | *p | *sb *n *sa | *x | *l',
array(
'*f'=>$oOutput->getFirst(),
'*p'=>$oOutput->getPrevious(),
'*sb'=>$oOutput->getSpaceBefore(),
'*n'=>$oOutput->getNumbers(),
'*sa'=>$oOutput->getSpaceAfter(),
'*x'=>$oOutput->getNext(),
'*l'=>$oOutput->getLast(),
'*c'=>$oOutput->getCurrent()
)
);

$sQuery = "SELECT * FROM alfabet".$oDatabase->getLimit();
$sArray = $oDatabase->getQueryResult($sQuery);

if(is_array($sArray)) {
if(!empty($sArray)) {
foreach($sArray as $iKey => $aValue) {
echo $aValue['id'].'. '.$aValue['letter'].'<br />';
}

echo '<div>'.$oPagination.'</div>';
} else {
echo 'Geen records gevonden.';
}
}
} catch(Exception $e) {
echo $e->getMessage() . $e->getLine();
}
?>

En voorbeeldje: http://www.dzjemo.nl/test-classnavigation.php :-).
Nummering is verwarrend, op de eerste pagina is $_GET['pag'] 0 en bij de laatste (zesde) pagina 5. Het lijkt mij logisch om bij één te beginnen en bij zes te eindigen.
Eigenlijk wel. Maar in mijn oorspronkelijke script had ik dit al, daarom heb ik dat zo doorgewerkt. Desalniettemin: 't is aangepast.

Klik.

Reageren