Re: Paging script
| From: | Ruben Crespo | Date: | Wed, 30 Sep 2009 13:09:50 +0000 |
| Subject: | Re: Paging script | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-298417@lists.php.net to get a copy of this message | ||
O.K Angelo,
I wish you that you find the code useful for your projects.
Best Regards !!
Rubén Crespo
2009/9/30 Angelo Zanetti <angelo@elemental.co.za>
>
>
>
>
> _____
>
> From: Ruben Crespo [mailto:rumailster@gmail.com]
> Sent: 30 September 2009 01:23 PM
> To: Angelo Zanetti
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] Paging script
>
>
>
> Hi all,
>
> Maybe it is not just what you need but could be helpfull.
>
> Here you have the code:
>
>
> //-------------------paginator.php------------------------------------------
> ----------------------------------
> <?php
> function paginator ($current, $total, $per_page, $link, $limit) {
>
> if ($current == '')
> {
> $current = 1;
> }else{
> $current = $current;
> }
>
> $total_pages = ceil($total/$per_page);
> $previous = $current - 1;
> $next = $current + 1;
>
>
> /*traduction*/
> $toFrom = "<p>Page ".$current." of
> ".$total_pages."</p>";
> $Goto = "Go to Page";
> $go = "Go";
>
>
> $texto = "
> <div id='paginacion'>".$toFrom."
> <ul>";
>
> if ($current > 1)
> $texto .= "<li><a
> href=\"$link$previous\">«</a></li>";
> else
> $texto .= "<li><b>«</b></li>";
>
>
> //Se lista hasta llegar a la posición actual
> if ($current > $limit) {
> $start = ($current - $limit) + 1;
> $texto .= "<li><b>...</b></li>";
> } else {
> $start = 1;
> }
>
> for ($start; $start < $current; $start++) {
> $texto .= "<li><a
> href=\"$link$start\">$start</a></li>";
> }
>
> //mostramos posicion actual
> $texto .= "<li><b>$current</b></li> ";
>
> //mostramos resto de registros
> for ($i=$current+1; $i<=$total_pages; $i++) {
>
> if ($i > $limit)
> {
> $texto .= "<li><b>...</b></li>";
> $i = $total_pages;
> }else{
> $texto .= "<li><a href=\"$link$i\">$i</a></li>
> ";
> }
> }
>
> if ($current < $total_pages)
> {
> $texto .= "<li><a
> href=\"$link$next\">»</a></li>";
> }else{
> $texto .= "<li><b>»</b></li>";
> }
>
> $texto .= "</ul>";
>
>
> //a form to go directly to the page you want
> $texto .= '<!--FORMULARIO PAGINADOR-->
> <div id="form_ir">
> <form action="'.$_SERVER["PHP_SELF"].'"
> method="get">
> <fieldset>
> <p>'.$Goto.' <input type="text" size="3"
> name="page" value="" />
> <input type="submit" name="ir"
> value="'.$go.'" /></p>
> </fieldset>
> </form>
> </div>';
>
>
>
> $texto .= '<br class="break" />
>
> </div>
> ';
>
> return $texto;
>
> }
>
> ?>
>
> //-------------------paginator.php------------------------------------------
> ----------------------------------
>
>
> //-------------------example.php--------------------------------------------
> --------------------------------
> <?php
> include ("paginator.php");
>
> for ($n=0; $n<=100; $n++)
> {
> $myArray [$n] = $n;
> }
>
> $total = count ($myArray); //the total elements in the array
> $per_page = 5; //elements the script show
> $link = "example.php?page="; //link
> $limit = 3; //number of items you show in the paginator list
>
>
>
> if ($_GET["page"])
> {
> $current = $_GET["page"];
> }else{
> $current = $_POST["page"];
> }
>
> $start = ($current-1) * $per_page;
> $end = $start + $per_page;
>
> //call to the paginator function ...
> echo paginator ($current, $total, $per_page, $link, $limit);
>
>
> //Show the results in different pages
> for ($i = $start; $i<=$end ;$i++)
> {
> echo $myArray[$i]."<br />";
> }
>
>
> ?>
>
>
> //-------------------example.php--------------------------------------------
> --------------------------------
>
>
> Thanks ruben very much :-)
>
>
>
> Regards
>
> Angelo
>
>
>
> http://www.wapit.co.za
> http://www.elemental.co.za
>
>
--
http://peachep.wordpress.com