faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

20 of 23 people (87%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

how can i add pagging in php ??? is there any object like in asp ????

Mar 19th, 2008 22:05
ha mo, Louis Brown, sunil rana,


I don't think PHP does have a built in function for pagenation, if 
there is then I would love to know about it, I use the following, hope 
this helps, if you have any problems then mail me.
Thanks
<?
// SET UP PAGENATION CHANGE THE LIMIT TO HOWEVER 
// MANY ROWS YOU WANT TO SHOW ON A PAGE
$limit = 10;
if (($pg == "") || ($pg == "1"))
{
  $pg = 1;
  $offset=0;
}
else
{
  $offset = ($pg * $limit) - $limit;
}
// QUERYS
// USE THIS QUERY TO GET THE ACTUAL RECORDS YOU WANT TO SHOW, 
// $offset AND $limit RESTRICT WHICH RECORDS TO GET
$BROWSE_Q = "SELECT * FROM aTable LIMIT $offset, $limit";
$BROWSE_R = mysql($dbase, $BROWSE_Q, $sock);
$BROWSE_N = mysql_num_rows($BROWSE_R);
// USE THIS QUERY TO GET THE TOTAL COUNT OF THE RECORDS IN THE 
// RESULT WITHOUT LIMIT
$COUNT_Q = "SELECT * FROM aTable";
$COUNT_R = mysql($dbase, $COUNT_Q, $sock);
LIST($record_count) = mysql_fetch_row($COUNT_R);
// CALCULATE WHICH RECS ARE SHOWING
// $record_count = TOTAL NUMBER OF RESULTS FOR THIS SEARCH 
// $from = the starting number of the result set 
// $to = the final number of the result set
$from = ($offset+1);
$to = ($offset + $limit);
$TMP_num = $limit * $pg;
if ($record_count < $TMP_num)
{
  $difference = $TMP_num - $record_count;
  $to = ($to - $difference);
}	
if ($BROWSE_N)
{
  // SAY WHICH RECORD ARE SHOWING, AND HOW MANY THERE ARE IN TOTAL
  ?>
  <?=$record_count?> items | displaying <?=$from?> - <?=$to?>
  <?
  // PRINT OUT YOUR RECORDS
  while($BROWSE_MANUFACTURERES_ROW=mysql_fetch_array
($BROWSE_MANUFACTURERES_R))
  {
    ?>
    Write out you lines here..........
    <?
  }
  //*******************************************
  // SHOW THE PAGE NUMBERS
  //*******************************************
  // GET NUMBER OF PAGES
  $num_pages = intval($record_count/$limit);
  // USE MODULUS TO SEE IF THERE IS A REMAINDER, 
  // IF THERE IS ADD ANOTHER PAGE
  if ($record_count%$limit) 
  {
    $num_pages++;
  }
  if ($num_pages >1)
  {
    for ($loop = 1; $loop <=$num_pages; $loop++) 
    {
      ?>
       <A HREF="<?=$PHP_SELF?>?pg=<?=$loop?>"><?=$loop?></A> 
      <?
    }	
  }				
}
else
{
  ?>
  No data
  <?
}
?>
http://www.tantofa.com
http://www.fantofa.com
http://www.mantofa.com
http://www.tanpola.com
http://www.tampola.com
http://www.yamot.com
http://www.mozmar.com
http://www.templatestemp.com