Page 1 of 1
listing
Posted: Fri Jun 28, 2002 11:46 am
by newbie_zak
I've got a very simple php script which lets my visitors add there own links using a mysql database which works great.
Now there are about 60 links in the database the links page is looking full. What I aiming to do is to print about 10 links per page.
I've found this peice of code:
Code: Select all
mysql_query("select * from table ORDER BY id LIMIT 10")
Which prints the first 10 entrys/links.
What code to I need which will work out how meny pages it will need and put something like this "Page:X of:Y" on the bottom of the page.
Hope someone can help.
Posted: Sun Jun 30, 2002 1:56 pm
by martin
Posted: Mon Jul 01, 2002 5:34 am
by Data
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>
multiple pages
Posted: Thu Dec 19, 2002 3:43 pm
by sirTemplar
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