Re: Pagination

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

 



Hi you all,

here is how I am doing in a veeeeeery big class of mine.. Course that uses class's specific terms, but it's very easy to understand.

=============================================================
/*****************************************************
* Fetch paginated results
*****************************************************/
function fetch_paginated($query='',$page=1,$itens=20)
{
$this->query($query.' LIMIT '.(($page*$itens)-$itens).','.$itens); <--------- IMPORTANT IT APPENDS A XXX,XXX LIMIT IN THE END OF QUERY.
if($this->num_rows() > 0)
{
while($this->fetch_array())
{
$results[] = $this->row;
}
}
else
{
return null;
}



$this->query($query.' LIMIT 0,'.(($page*$itens)-$itens)); $this->page_prev = $this->num_rows(); <--------- HOW MANY ROWS BEFORE

$this->query($query.' LIMIT '.(($page*$itens)-$itens).',1000000000000');
$this->page_next = $this->num_rows(); <--------- HOW MANY ROWS AHEAD

$this->page_pages_before = ($this->page_prev/$itens); <--------- HOW MANY PAGES BEFORE
$this->page_pages_after = ($this->page_next/$itens); <--------- HOW MANY PAGES AHEAD

$this->query($query);
$this->page_total = $this->num_rows(); <--------- TOTAL RECORDS (INCLUDING CURRENT, BEFORE AND AFTER PAGES)

$this->page_from = (($page*$itens)-$itens); <--------- NEXT PAGE
$this->page_to = ($page*$itens); <--------- PAGE AFTER

return $results;
}
=============================================================


The important thing here is how you build the limit statement: " LIMIT (($page_number*$itens_per_page)-$itens_per_page),$itens_per_page"

Where page number MUST start at 1, NOT 0.

Regards,
Bruno B B Magalhaes


On Dec 16, 2004, at 8:38 PM, David Ziggy Lubowa wrote:



Hey guys,

I am working on an internal db and i have a script[below] which does some
searching for me, now i am no expert in php but atleast i can always read up,
i want to add some kind of pagination because just incase i search for
somethign in particular i dont want it to display 100 entries on my form, how
can i incorperate a pagination script in the script below,


all help is highly appreciated

[snip]


<?php

  $var = @$_GET['q'] ;
  $trimmed = trim($var);

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }


$link = mysql_connect("localhost", "beef","b33f"); mysql_select_db("ip", $link); $qry = mysql_query("SELECT * FROM IP_Addresses where free like '%". $_GET['q']."%'", $link);

?>
<table border="1" width="100%"><tr><?php

if (mysql_num_rows($qry)==0 ) {

print " Oops No records found ";
?>
<br>
<a href="http://localhost/ipsearch2.html";>Back</a>
</br>
<?
exit();
}
if (mysql_num_rows($qry) > 0) {
for ($i = 0; $i<mysql_num_fields($qry); $i++) {
echo "<td align=center><strong>" . mysql_field_name($qry, $i) .
"</td>"; echo "<td align=center><strong>" . mysql_field_name($qry,
$i) . "</td>";
}
}


?>

</tr><?php

if (mysql_num_rows($qry) > 0) {
   for ($j = 0; $j<mysql_num_rows($qry); $j++) {

       ?><tr><?php

for ($k = 0; $k<mysql_num_fields($qry); $k++) {
echo "<td align=center>" . mysql_result($qry,$j, $k) . "</td>";
}


       ?></tr><?php

   }
}

?>

[/snip]

cheers

-Z

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



-- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux