Sunday, January 16, 2011

Pagination- marking the web pages

Definition for pagination


- page numbers: the sequential numbers given to pages in a book or document
- act of numbering pages: the process or work of numbering pages
Encarta World English Dictionary




On the Internet, pagination is used for such things as displaying
 a limited number of results on search engine results pages, or 
showing a limited number of posts when viewing a forum thread. 
Pagination is used in some form in almost every web application
 to divide returned data and display it on multiple pages. Pagination 
also includes the logic of preparing and displaying the links to the 
various pages.
Pagination can be handled client-side or server-side. Server-side pagination is more common. Client-side pagination can be used when there are very few records to be accessed, in which case all records can be returned, and the client can use JavaScript to view the separate pages. By using AJAX, hybrid server/client-side pagination can be used, in which Java script is used to 
request the subsequent page which is loaded and inserted into the Document 
Object Model via AJAX.
Server-side pagination is appropriate for large data sets providing faster 
initial page load, accessibility for those not running Javascript, and 
complex view business logic 
Correctly implementing pagination can be difficult. There are many different usability questions such as should "previous" and "next" links be included, how many links to pages should be displayed, and should there be a link to the first and last pages.






The source code to be just copied and pasted is




<?php
//connecting to the database$error "Could not connect to the database";mysql_connect('localhost','root','') or die($error);mysql_select_db('pagination') or die($error);
//max displayed per page$per_page 36;
//get start variable$start $_GET['start'];
//count records$record_count mysql_num_rows(mysql_query("SELECT * FROM data"));
//count max pages$max_pages $record_count $per_page//may come out as decimal
if (!$start)
   
$start 0;
   
//display data$get mysql_query("SELECT * FROM data LIMIT $start, $per_page");
while (
$row mysql_fetch_assoc($get))
{
 
// get data
 
$name $row['name'];
 
$age $row['age'];
 
 echo 
$name." (".$age.")<br />";

}
//setup prev and next variables$prev $start $per_page;$next $start $per_page;
//show prev buttonif (!($start<=0))
       echo 
"<a href='index.php?start=$prev'>Prev</a> ";
//show page numbers

//set variable for first page
$i=1;

for (
$x=0;$x<$record_count;$x=$x+$per_page)
{
 if (
$start!=$x)
    echo 
" <a href='index.php?start=$x'>$i</a> ";
 else
    echo 
" <a href='index.php?start=$x'><b>$i</b></a> ";
 
$i++;
}
//show next buttonif (!($start>=$record_count-$per_page))
       echo 
" <a href='index.php?start=$next'>Next</a>";
?>

5 comments:

  1. Awesome code ankit.really very helpful.
    Thanks dude,

    ReplyDelete
  2. hi,

    This code is really awesome.
    my using the same navigation is working fine.
    but in this line $start = $_GET['start']; its showing error like undefined index: start
    that it's showing only in first time once navigating it will not show.
    In this line also i defined $get = mysql_query("SELECT * FROM info LIMIT $start, $per_page");
    please look into this and give some suggestion to resolve this.

    Thanku

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. put "error_reporting(0)" just at the top of your code within php code.

      Delete
  3. Thank you for this code. It works very well. It is also simple to understand.

    ReplyDelete