NameSilo

Pagination in Php I've done the start

Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
Watch

liam_d

The original NP Emo KidEstablished Member
Impact
25
So far i have this;

PHP:
// Pagination
// $table - what table to use
// $where - any WHERE statements to add
// $order_by - any ORDER BY statements to add
// $limit - amount per page
function pagination_start($table, $where, $order_by, $limit)
{
	// make it global so we can use it outside of function
	global $pagination_query;
	
	$pg = $_GET['pg'];
	
	if(!ctype_digit($pg)) 
	{
		$pg = '1';
	}
	
	$start = ($pg - 1) * $limit;
	
	$pagination_query = mysql_query("SELECT * FROM `" . $table . "` " . $where . " " . $order_by . " LIMIT " . $start . ", " . $limit);
}

Tested it and it works fine can anyone possibly do a function for me to generate the pagination links? Ill give $100NP to whoever can.
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
i'll take you up on this offer

i'l be back with the code in a little bit...
 
0
•••
ok... here goes
PHP:
<?
// Pagination
// $table - what table to use
// $where - any WHERE statements to add
// $order_by - any ORDER BY statements to add
// $limit - amount per page
function pagination_start($table, $where, $order_by, $limit)
{
    // make it global so we can use it outside of function
    global $pagination_query;
    
    $pg = $_GET['pg'];
    
    if(!ctype_digit($pg))
    {
        $pg = '1';
    }
    
    $start = ($pg - 1) * $limit;
    
    $pagination_query = mysql_query("SELECT * FROM `" . $table . "` " . $where . " " . $order_by . " LIMIT " . $start . ", " . $limit);
} 
// end of your code

// this is the start of my code

// begin to show results set
echo "Results";
$count = 1 + $s ;

// now you can display the results returned
  while ($row= mysql_fetch_array($pagination_query)) {
  $title = $row["1st_field"];

  echo "$count.) $title" ;
  $count++ ;
  }

//break before paging
  echo "<br />";

  // next we need to do the links to other results
  if ($pg>=1) { // exclude previous link
  $prevs=($pg-$limit);
  print " <a href=\"$PHP_SELF?s=$prevs&q=$pg\"><< 
  Prev 10</a>&nbsp ";
  }

// calculate number of pages needing links
  $pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }

// check to see if last page
  if (!((($numrows+$limit)/$limit)==$pages) && $pages!=1) {

  // not last page so give NEXT link
  $news=$numrows+$limit;

  echo " <a href=\"$PHP_SELF?s=$news&q=$pg\">Next 10 >></a>";
  }

$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";

// end my php code
// end php doc
?>

Not guaranteeing that this code works, I wrote/modded this a while ago. Should work, maybe a few quirks.

-Steve
 
0
•••
Thanks but what i needed was for the links to be a seperate function that works with pagination_start function, could you possibly do this?
 
0
•••
heres a paging class that i use that i go at codewalkers

PHP:
<?php
/**************************************************************************************
* Class: Pager
* Author: Tsigo <[email protected]>
* Methods:
*         findStart
*         findPages
*         pageList
*         nextPrev
* Redistribute as you see fit.
**************************************************************************************/
class Pager
  {
  /***********************************************************************************
   * int findStart (int limit)
   * Returns the start offset based on $_GET['page'] and $limit
   ***********************************************************************************/
   function findStart($limit)
    {
     if ((!isset($_GET['page'])) || ($_GET['page'] == "1"))
      {
       $start = 0;
       $_GET['page'] = 1;
      }
     else
      {
       $start = ($_GET['page']-1) * $limit;
      }

     return $start;
    }
  /***********************************************************************************
   * int findPages (int count, int limit)
   * Returns the number of pages needed based on a count and a limit
   ***********************************************************************************/
   function findPages($count, $limit)
    {
     $pages = (($count % $limit) == 0) ? $count / $limit : floor($count / $limit) + 1;

     return $pages;
    }
  /***********************************************************************************
   * string pageList (int curpage, int pages)
   * Returns a list of pages in the format of "« < [pages] > »"
   ***********************************************************************************/
   function pageList($curpage, $pages)
    {
     $page_list  = "";

     /* Print the first and previous page links if necessary */
     if (($curpage != 1) && ($curpage))
      {
       $page_list .= "  <a href=\"".$_SERVER['PHP_SELF']."?page=1\" title=\"First Page\">«</a> ";
      }

     if (($curpage-1) > 0)
      {
       $page_list .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage-1)."\" title=\"Previous Page\"><</a> ";
      }

     /* Print the numeric page list; make the current page unlinked and bold */
     for ($i=1; $i<=$pages; $i++)
      {
       if ($i == $curpage)
        {
         $page_list .= "<b>".$i."</b>";
        }
       else
        {
         $page_list .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".$i."\" title=\"Page ".$i."\">".$i."</a>";
        }
       $page_list .= " ";
      }

     /* Print the Next and Last page links if necessary */
     if (($curpage+1) <= $pages)
      {
       $page_list .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage+1)."\" title=\"Next Page\">></a> ";
      }

     if (($curpage != $pages) && ($pages != 0))
      {
       $page_list .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".$pages."\" title=\"Last Page\">»</a> ";
      }
     $page_list .= "</td>\n";

     return $page_list;
    }
  /***********************************************************************************
   * string nextPrev (int curpage, int pages)
   * Returns "Previous | Next" string for individual pagination (it's a word!)
   ***********************************************************************************/
   function nextPrev($curpage, $pages)
    {
     $next_prev  = "";

     if (($curpage-1) <= 0)
      {
       $next_prev .= "Previous";
      }
     else
      {
       $next_prev .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage-1)."\">Previous</a>";
      }

     $next_prev .= " | ";

     if (($curpage+1) > $pages)
      {
       $next_prev .= "Next";
      }
     else
      {
       $next_prev .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage+1)."\">Next</a>";
      }

     return $next_prev;
    }
  }
?>


Example on how to use:


<?php
/* Instantiate class */
require_once("class.pager.php");
$p = new Pager;

/* Show many results per page? */
$limit = 100;

/* Find the start depending on $_GET['page'] (declared if it's null) */
$start = $p->findStart($limit);

/* Find the number of rows returned from a query; Note: Do NOT use a LIMIT clause in this query */
$count = mysql_num_rows(mysql_query("SELECT field FROM table"));

/* Find the number of pages based on $count and $limit */
$pages = $p->findPages($count, $limit);

/* Now we use the LIMIT clause to grab a range of rows */
$result = mysql_query("SELECT * FROM table LIMIT ".$start.", ".$limit);

/* Now get the page list and echo it */
$pagelist = $p->pageList($_GET['page'], $pages);
echo $pagelist;

/* Or you can use a simple "Previous | Next" listing if you don't want the numeric page listing */
//$next_prev = $p->nextPrev($_GET['page'], $pages);
//echo $next_prev;

/* From here you can do whatever you want with the data from the $result link. */
?>

I love this class ;)
 
0
•••
Appraise.net

We're social

Escrow.com
Spaceship
Escrowly
CryptoExchange.com
Domain Recover

NamePros updates

New in Parking: updated way to act on many domains or inquiries at once
Your domain list and inquiry list now have a bar above and below the list. Check the items you want, then choose an action in the bar...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back