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?
2.892 views