view/helpers/Pagination.php
CODE
<?php
class My_View_Helper_Pagination
{
private $intRows = 0;
private $intLimit = 0;
private $intPage = 0;
private $blnFailure = false;
private $strController;
private $strAction;
private $_view;
public function setView( $view ) {
$this -> _view = $view;
}
public function pagination()
{
return $this;
}
public function SetOptions( $intPage, $intLimit, $intRows, $strController, $strAction )
{
if( isset( $intPage ) && isset( $intLimit ) && isset( $intRows ) )
{
$this -> intRows = $intRows;
$this -> intLimit = $intLimit;
$this -> intPage = $intPage;
$this -> strController = $strController;
$this -> strAction = $strAction;
}
else
{
$this -> blnFailure = true;
}
}
public function Render()
{
if( $this -> blnFailure === true )
{
return 'Pagination: Upewnij sie, ze zdefiniowales wszystkie opcje w Twojej akcji kontrolera';
}
else
{
$this -> _view -> pag_intPages = ceil( $this -> intRows / $this -> intLimit );
$this -> _view -> pag_intPage = $this -> intPage;
$this -> _view -> pag_strContoller = $this -> strController;
$this -> _view -> pag_strAction = $this -> strAction;
return $this -> _view -> render( 'pagination.phtml' );
}
}
}
view/scripts/pagination.phtml:
<div class="PaginationParent"> <?php if( $this -> pag_intPage < 3 ): ?>
<img src="<?= $this -> baseUrl ?>/public/images/icons/first_.gif" alt="" />
<?php else: ?>
<a href="<?= $this -> baseUrl ?>/
<?= $this -> pag_strController ?>/
<?= $this -> pag_strAction ?>/p/1">
<img src="<?= $this -> baseUrl ?>/public/images/icons/first.gif" alt="" />
</a> <?php endif; ?>
<?php if( $this -> pag_intPage == 1 ): ?>
<img src="<?= $this -> baseUrl ?>/public/images/icons/previous_.gif" alt="" />
<?php else: ?>
<a href="<?= $this -> baseUrl ?>/
<?= $this -> pag_strController ?>/
<?= $this -> pag_strAction ?>/p/
<?= $this -> pag_intPage - 1 ?>">
<img src="<?= $this -> baseUrl ?>/public/images/icons/previous.gif" alt="" />
</a> <?php endif; ?>
<?php
if( $this -> pag_intPages < 9 ):
for( $intI = 1; $intI <= $this -> pag_intPages; $intI++ ):
?>
<li<?php if( $this -> pag_intPage == $intI ): ?> class="PaginationActual"<?php endif; ?>>
<?php
if( $this -> pag_intPage != $intI ):
?>
<a href="<?= $this -> baseUrl ?>/
<?= $this -> pag_strController ?>/
<?= $this -> pag_strAction ?>/p/
<?= $intI ?>">
<?= $intI ?></a> <?php
else:
echo( $intI );
endif;
?>
<?php
endfor;
else:
if( $this -> pag_intPage < 5 ):
$intShow = ( $this -> pag_intPage > 2 ) ? $this -> pag_intPage + 1 : 3;
for( $intI = 1; $intI <= $intShow; $intI++ ):
?>
<li<?php if( $this -> pag_intPage == $intI ): ?> class="PaginationActual"<?php endif; ?>>
<?php
if( $this -> pag_intPage != $intI ):
?>
<a href="<?= $this -> baseUrl ?>/
<?= $this -> pag_strController ?>/
<?= $this -> pag_strAction ?>/p/
<?= $intI ?>">
<?= $intI ?></a> <?php
else:
echo( $intI );
endif;
?>
<?php
endfor;
?>
<li><a href="<?= $this -> baseUrl ?>/
<?= $this -> pag_strController ?>/
<?= $this -> pag_strAction ?>/p/
<?= $this -> pag_intPages ?>">
<?= $this -> pag_intPages ?>
</a></li> <?php
elseif( $this -> pag_intPage >= 5 && $this -> pag_intPage <= ( $this -> pag_intPages - 4 ) ):
?>
<li><a href="<?= $this -> baseUrl ?>/<?= $this -> pag_strController ?>/<?= $this -> pag_strAction ?>/p/1">1</a></li>
<li>...</li>
<?php
for( $intI = ( $this -> pag_intPage - 2 ); $intI <= ( $this -> pag_intPage + 2 ); $intI ++ ):
?>
<li<?php if( $this -> pag_intPage == $intI ): ?> class="PaginationActual"<?php endif; ?>>
<?php
if( $this -> pag_intPage != $intI ):
?>
<a href="<?= $this -> baseUrl ?>/
<?= $this -> pag_strController ?>/
<?= $this -> pag_strAction ?>/p/
<?= $intI ?>">
<?= $intI ?></a> <?php
else:
echo( $intI );
endif;
?>
<?php
endfor;
?>
<li><a href="<?= $this -> baseUrl ?>/
<?= $this -> pag_strController ?>/
<?= $this -> pag_strAction ?>/p/
<?= $this -> pag_intPages ?>">
<?= $this -> pag_intPages ?>
</a></li> <?php
elseif( $this -> pag_intPage > ( $this -> pag_intPages - 4 ) ):
?>
<li><a href="<?= $this -> baseUrl ?>/
<?= $this -> pag_strController ?>/
<?= $this -> pag_strAction ?>/p/1">1
</a></li> <?php
$intShow = ( $this -> pag_intPage < ( $this -> pag_intPages - 1 ) ) ? $this -> pag_intPage - 1 : $this -> pag_intPages - 2;
for( $intI = $intShow; $intI <= $this -> pag_intPages; $intI ++ ):
?>
<li<?php if( $this -> pag_intPage == $intI ): ?> class="PaginationActual"<?php endif; ?>>
<?php
if( $this -> pag_intPage != $intI ):
?>
<a href="<?= $this -> baseUrl ?>/
<?= $this -> pag_strController ?>/
<?= $this -> pag_strAction ?>/p/
<?= $intI ?>">
<?= $intI ?></a> <?php
else:
echo( $intI );
endif;
?>
<?php
endfor;
endif;
endif;
?>
<?php if( ( $this -> pag_intPages - $this -> pag_intPage ) == 0 ): ?>
<img src="<?= $this -> baseUrl ?>/public/images/icons/next_.gif" alt="" />
<?php else: ?>
<a href="<?= $this -> baseUrl ?>/
<?= $this -> pag_strController ?>/
<?= $this -> pag_strAction ?>/p/
<?= $this -> pag_intPage + 1 ?>">
<img src="<?= $this -> baseUrl ?>/public/images/icons/next.gif" alt="" />
</a> <?php endif; ?>
<?php if( ( $this -> pag_intPages - $this -> pag_intPage ) < 2 ): ?>
<img src="<?= $this -> baseUrl ?>/public/images/icons/last_.gif" alt="" />
<?php else: ?>
<a href="<?= $this -> baseUrl ?>/
<?= $this -> pag_strController ?>/
<?= $this -> pag_strAction ?>/p/
<?= $this -> pag_intPages ?>">
<img src="<?= $this -> baseUrl ?>/public/images/icons/last.gif" alt="" />
</a> <?php endif; ?>
plik kontrolera:
CODE
<?php
class TwojController extends Front_Controller_Action
{
// ...
function indexAction()
{
$intPage = $this -> _request -> getParam( 'p' ); // numer strony znajduje sie w parametrze "p"
$intPage = ( !empty( $intPage ) ) ? $intPage : 1;
$intLimit = ... // tutaj wyciagnij sobie limit rekordow na stronie z configa czy cos
$this -> view -> pag_intRows = (int)$this -> CountRows(); // zliczenie wszystkich rekordów
$this -> view -> pag_intLimit = $intLimit;
$this -> view -> pag_intPage = $intPage;
$this -> view -> pag_strController = 'twoj'; //nazwa kontrolera
$this -> view -> pag_strAction = 'index'; //nazwa akcji
}
index.phtml: w miejscu gdzie chcesz wyswietlic stronnicowanie
CODE
<?php
$this -> pagination() -> SetOptions( $this -> pag_intPage, $this -> pag_intLimit, $this -> pag_intRows, $this -> pag_strController, $this -> pag_strAction );
print( $this -> pagination() -> Render() );
?>