When in your data base you have a lot of records and you want to post them all, a bad idea will be to post them on only one page, this is why I suggest you to use pagination for this records, divide them in this way in many pages. A good example is http://www.google.com if you search something, it will return so many results so is impossible to post them on only one page, in this way they are divided in more pages, leting the possibility for the person who search to see the next or preview page or even to jump from the current page to 5 pages rearward or forward.
For the beginning we will connect to data base and will see how many records are in the table from which we want to extract the values. The value obtained will stock it as a value of variable $total_numbers and we will set how many records per page will be.
$user = 'root';
$pass = '';
$server = 'localhost';
$data_base = 'test';
$table = '_table_';
$connect = mysql_connect($server,
$user, $pass) or die
(mysql_error());
$select_db = mysql_select_db($data_base,
$connect) or
die (mysql_error());
$nr_per_page = 10;
$sql_total = "SELECT
count(*) AS nr FROM ".$table;
$query_total = mysql_query($sql_total,
$connect);
$row = mysql_fetch_object($query_total);
$total_numbers =
$row->nr;
Further will calculate how many pages we'll have dividing total numer of results from data base to number of records per page and takeing the next integer value using function ceil().
If you look carefully at pagination system on Google, you'll see that it is posting 10 results after and before current page if this thing is possible. The same thing we'll do us with $between variable, but we don't set it to 10 pages, but two, because I don't think that we'll have so many results as Google, anyhow the variable's value could be changed. We also calculate the value that we'll send it by GET for the last page, this we'll use it for the link "Last page".
$pages = ceil($total_numbers/$nr_per_page);
$between = 2;
$last = $pages*$nr_per_page-$nr_per_page;
In the moment when a link to another page (next, preview, first or last page) is clicked, we sent by GET method an http variable that we attribute to variable $limit_x. If we enter directly on the page, $_GET['limit_inf'] will not exist so $limit_x will be null that is not a good thing, in this way we attribute it the value 0. This variable we'll use it to select from data base the records that we'll post on the page starting with the record with number equal with the value of $limit_x and ending after $nr_per_page records found.
$limit_x =
$_GET[limit_inf];
if($limit_x
== '')
$limit_x = 0;
$sql = "SELECT
* FROM ".$table."
LIMIT ".$limit_x.",
".$nr_per_page."";
$query = mysql_query($sql,
$connect);
while($row
= mysql_fetch_array($query)){
echo $row[1].'<br
/>';
}
We calculate pages that will be posting as links. For this we need to know what page is the page where we are ($current_page) and what page is the first from which is starting the reckoning ($start) and the last page where the posting is stop ($stop).
$start represent the value that we'll post it for the beginning of pagination. If we have 100 pages for posting we do not post them all but only two ($between) before and after the current page. $start is precisely the beginning of pagination, that is if we are in the 10 th page, $start will be 8.
$stop is just like $start but reverse, this is posting the last value. In our exemple $stop will be 12, if this value exist, else it will be equal with the number of pages ($pages).
$current_page =
$limit_x/$nr_per_page+1;
$start = 1;
$stop = $current_page+$between;
if ($current_page
> $between)
$start = $current_page-$between;
if ($pages
< $stop)
$stop = $pages;
All we have to do is to post the pagination for the results. If the number of results is smaller or equal than/with the variable set to be the maximum number of results per page, we don't post anything because it has no sense.
First time we post a link to the first page which it has an $_GET['limit_inf'] variable set to 0, then we start an for loop starting with the value of $start and stoping with the value of $stop, incrementing the value of $nr ($nr on start has a value equal with $start) on each loop. We want, also, that current page to be an non link. This thing we'll do if the value of http variable assigned to $limit_x is equal with $nr from precedent loop multiplyed with the number of posts per page ($nr_per_page).
For links we set a value for the variable sent by GET method, in accordance with is selecting the records from data base, equal with $nr's value from precedent loop multiply with the number of posts per page ($nr_per_page) and we'll show $nr as being the number of page from which we want to go. After for loop we post an link to last page, named "Last Page" that we'll set the http variable as being equal with the value of $last calculated above.
if($total_numbers
> $nr_per_page){
echo "<a href=\"".$_SERVER['PHP_SELF']."?limit_inf=0\"
>First page</a> ";
for($nr
= $start; $nr <=
$stop; $nr++ ){
if($limit_x
!= ($nr-1)*$nr_per_page)
echo " <a
href=\"".$_SERVER['PHP_SELF']."?limit_inf=".(($nr-1)*$nr_per_page)."\"
>".$nr."</a> ";
else
echo "<span
style=\"background-color:#999999; color: #ffffff;\"> $nr </span>";
}
echo " <a
href=\"".$_SERVER['PHP_SELF']."?limit_inf=".$last."\"
>Last page</a>";
}
Here you have the entire source code for pagination. All you have to remember is that you need to set the values of variables for connecting to data base with yours infos and also to modify the value of $table, with the name of your mysql table.
- <!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Pagination</title>
- <meta http-equiv="Content-Type"
content="text/html;
charset=iso-8859-1" />
- </head>
- <body>
- <?
- $user = 'root';
- $pass = '';
- $server =
'localhost';
- $data_base =
'test';
- $table = '_table_';
- $connect = mysql_connect($server,
$user, $pass) or die
(mysql_error());
- $select_db =
mysql_select_db($data_base,
$connect) or
die (mysql_error());
- $nr_per_page =
10;
- $sql_total =
"SELECT count(*) AS nr FROM ".$table;
- $query_total = mysql_query($sql_total,
$connect);
- $row = mysql_fetch_object($query_total);
- $total_numbers =
$row->nr;
- $pages = ceil($total_numbers/$nr_per_page);
- $between =
2;
- $last = $pages*$nr_per_page-$nr_per_page;
- $limit_x =
$_GET[limit_inf];
- if($limit_x
== '')
$limit_x = 0;
- $sql = "SELECT
* FROM ".$table."
LIMIT ".$limit_x.",
".$nr_per_page."";
- $query = mysql_query($sql,
$connect);
- while($row
= mysql_fetch_array($query)){
- echo $row[1].'<br
/>';
- }
- $current_page =
$limit_x/$nr_per_page+1;
- $start = 1;
- $stop = $current_page+$between;
- if ($current_page
> $between)
$start = $current_page-$between;
- if ($pages
< $stop)
$stop = $pages;
- if($total_numbers
> $nr_per_page){
- echo "<a
href=\"".$_SERVER['PHP_SELF']."?limit_inf=0\"
>First page</a> ";
- for($nr
= $start; $nr
<= $stop; $nr++
){
- if($limit_x
!= ($nr-1)*$nr_per_page)
- echo " <a
href=\"".$_SERVER['PHP_SELF']."?limit_inf=".(($nr-1)*$nr_per_page)."\"
>".$nr."</a> ";
- else
- echo "<span
style=\"background-color:#999999; color: #ffffff;\"> $nr </span>";
- }
- echo " <a
href=\"".$_SERVER['PHP_SELF']."?limit_inf=".$last."\"
>Last page</a>";
- }
- ?>
- </body>
- </html>
 |