listing

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
newbie_zak
Forum Newbie
Posts: 1
Joined: Fri Jun 28, 2002 11:46 am

listing

Post 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.
User avatar
martin
Forum Commoner
Posts: 33
Joined: Fri Jun 28, 2002 12:59 pm
Location: Cambridgeshire

Post by martin »

The following url has an example code for this:
http://www.phpworld.com/faq/faq_001.htm ... al%20Pages
Data
Forum Newbie
Posts: 15
Joined: Tue Jun 25, 2002 4:14 am
Location: Aberdeen, Scotland

Post 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&nbsp;"); //don't want to link to the page your viewing
}
else
{
print ("<a href=\"$PHP_SELF?page=$i\">$i</a>&nbsp;"); //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>
sirTemplar
Forum Commoner
Posts: 65
Joined: Wed Dec 18, 2002 1:57 am

multiple pages

Post 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&nbsp;"); //don't want to link to the page your viewing
}
else
{
print ("<a href="$PHP_SELF?page=$i">$i</a>&nbsp;"); //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
Post Reply