php sample script for pagination [closed]

Solution 1:

Following link can help you, its has easy to use description too

http://www.strangerstudios.com/sandbox/pagination/diggstyle.php

If you would like to have a tutorial then you should read on

Solution 2:

A plain and simple one in purpose to learn from

<?
$per_page=10;

//put FROM and WHERE parts into separate  variable
$from_where="FROM table WHERE filter=1";
//getting total number of records 
$res=mysql_query("SELECT count(id) ".$from_where);
$row=mysql_fetch_row($res);
$total_rows=$row[0];

//Process GET variables to get $start value for LIMIT
if (isset($_GET['page'])) $CUR_PAGE=($_GET['page']); else $CUR_PAGE=1;
$start=abs(($CUR_PAGE-1)*$per_page);

//getting records from database into array
$query="SELECT * $from_where ORDER BY date DESC LIMIT $start,$per_page";
$res=mysql_query($query);
while ($row=mysql_fetch_array($res)) $DATA[++$start]=$row;

//Getting page URL without query string
$uri=strtok($_SERVER['REQUEST_URI'],"?")."?";

//create a new query string without a page variable
if (isset($_GET['page'])) unset($_GET['page']);
if (count($_GET)) {
  foreach ($_GET as $k => $v) {
    if ($k != "page") $uri.=urlencode($k)."=".urlencode($v)."&";
  }
}

//getting total number of pages and filling an array with links
$num_pages=ceil($total_rows/$per_page);
for($i=1;$i<=$num_pages;$i++) $PAGES[$i]=$uri.'page='.$i;

//and here goes a simple template
?>
Total records: <b><?=$total_rows?></b><br><br>
<? foreach ($DATA as $i => $row): ?>
<?=$i?>. <a href="?id=<?=$row['id']?>"><?=$row['title']?></a><br>
<? endforeach ?> 

<br>
Pages: 
<? foreach ($PAGES as $i => $link): ?>
<? if ($i == $CUR_PAGE): ?>
<b><?=$i?></b>
<? else: ?> 
<a href="<?=$link?>"><?=$i?></a>
<? endif ?> 
<? endforeach ?> 

Solution 3:

The secret is in SQL's SELECT ... LIMIT x OFFSET y clauses.