It took me a while to work it out for my own script, but this is what I used.
It works by adding page='a number' to the query string.
$rpp = 10; //results per page
$post_start = (($page*$rpp)-($rpp-1)); //work out which row to start with
$prev_page = $page-1; //next page number
$next_page = $page+1; //prev page number
$sql = ("Your normal select statement with this added: id>=$post_start and id<$post_start+$rpp order by post_no");
$numsql = ("Normal select statement getting list of all results");
$numquery = mysql_query( $numsql );
$num_rows = mysql_num_rows( $numquery ); //how many results?
$pages = ceil($num_rows/$rpp); //gives how many pages you will have
//Print your records out as normal
print ("Currently viewing page $page of $pages<br>");
if( $page <= 1 )
{
print ("< Prev [ "); //if page 1, then you can't go to the previous one
}
else
{
print ("<a href=\"$PHP_SELF?page=$prev_page\">< Prev</a> [ "); //go to $page-1
}
for($i=1; $i<=$pages; $i++) //prints list of page numbers
{
if( $i == $page )
{
print ("$i "); //don't want to link to the page your viewing
}
else
{
print ("<a href=\"$PHP_SELF?page=$i\">$i</a> "); //sets up link to page number
}
}
if( $next_page > $pages )
{
print (" ] Next > <p>"); //if on last page, don't allow next page link
}
else
{
print ("] <a href=\"$PHP_SELF?page=$next_page\">Next ></a><p>"); //go to $page+1
}
Will print out something like this:
Currently viewing page 3 of 5.
<prev [ 1 2 3 4 5 ] next>
Data wrote:It took me a while to work it out for my own script, but this is what I used.
It works by adding page='a number' to the query string.
$rpp = 10; //results per page
$post_start = (($page*$rpp)-($rpp-1)); //work out which row to start with
$prev_page = $page-1; //next page number
$next_page = $page+1; //prev page number
$sql = ("Your normal select statement with this added: id>=$post_start and id<$post_start+$rpp order by post_no");
$numsql = ("Normal select statement getting list of all results");
$numquery = mysql_query( $numsql );
$num_rows = mysql_num_rows( $numquery ); //how many results?
$pages = ceil($num_rows/$rpp); //gives how many pages you will have
//Print your records out as normal
print ("Currently viewing page $page of $pages<br>");
if( $page <= 1 )
{
print ("< Prev [ "); //if page 1, then you can't go to the previous one
}
else
{
print ("<a href="$PHP_SELF?page=$prev_page">< Prev</a> [ "); //go to $page-1
}
for($i=1; $i<=$pages; $i++) //prints list of page numbers
{
if( $i == $page )
{
print ("$i "); //don't want to link to the page your viewing
}
else
{
print ("<a href="$PHP_SELF?page=$i">$i</a> "); //sets up link to page number
}
}
if( $next_page > $pages )
{
print (" ] Next > <p>"); //if on last page, don't allow next page link
}
else
{
print ("] <a href="$PHP_SELF?page=$next_page">Next ></a><p>"); //go to $page+1
}
Will print out something like this:
Currently viewing page 3 of 5.
<prev [ 1 2 3 4 5 ] next>
what do i exactly change and where to make the above code function! thanks