RE: Paging script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



 

 

  _____  

From: Ruben Crespo [mailto:rumailster@xxxxxxxxx] 
Sent: 30 September 2009 01:23 PM
To: Angelo Zanetti
Cc: php-general@xxxxxxxxxxxxx
Subject: Re:  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\">&laquo;</a></li>";
  else
    $texto .= "<li><b>&laquo;</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\">&raquo;</a></li>";
  }else{
    $texto .= "<li><b>&raquo;</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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux