Pagination is one of the best way to show large amount of data very easily and effectively. You can easily get & display thousands of results from database and show it khổng lồ the visitor one by one.Pagination technique is used to fetch small amount data stored in a database inspite of fetching all the data at once from the database by simply querring the databse again và again using some methods.This is a very useful technique it decreases the webpage loading time by only displaying some results in front of the user.In this tutorial we will show you how khổng lồ create và implement PHP pagination in three simple steps.
Bạn đang xem: Php pagination
Join With 27,000+ Members In Our Google Group & Get Latest Tutorials
Get our latest tutorials, How-To guides on web development every day right into your inbox.
Steps For Creating PHP Pagination With MySQLConnect to Database.Query The MySQL Database.Get The Results.
Step 1. Connect lớn Database
In this step we have to create a sample database and table for our pagination & connect to database table so that we can implement pagination technique. You can connect khổng lồ any existing database from which you want khổng lồ get the data. In this step we use sample database named "demo".
$host = "localhost";$user = "root";$pass = "";mysql_connect($host, $user, $pass);mysql_select_db("demo");?>
$offset = 0;$page_result = 5; if($_GET<"pageno">) $page_value = $_GET<"pageno">; if($page_value > 1) $offset = ($page_value - 1) * $page_result; $select_results = " select * from student_info limit $offset, $page_result ";?>
In second step we use three php variables $offset, $page_result, $page_value. $offset = 0 because we want khổng lồ select the data from the first that is 0th position from the table, $page_result = 5 because we want to display 5 results at a time.$page_value = $_GET<"pageno"> it is the value of the page when the user wants khổng lồ change the page và click the pagination page number.Then if the $page_value is greater than 1 we change the $offset value to lớn the value of this code ($page_value - 1) * $page_result.Then we query the database we select details from student_info table and limit the results returned by the query for more details of limit clause visit our MySQL clause tutorial.You may also lượt thích load more results from database using ajax.
$result = mysql_query( $select_results );while($row = mysql_fetch_array($result)) echo $row<" student_name ">; echo $row<" student_rollno ">; echo $row<" student_course ">;$pagecount = 50; // Total number of rows$num = $pagecount / $page_result ;if($_GET<"pageno"> > 1) echo " Prev ";for($i = 1 ; $i ". $i ."";}if($num! = 1) echo " Next ";?>
In this step we get the results from student_info table, in student_info table we have three columns student_name, student_rollno, student_course.Then, we use $pagecount = 50 because there are 50 rows in student_info table. Then, we use $num variable & set the value returned by $pagecount / $page_result.The first if statement display the Prev page liên kết if the pageno is greater than 1 and then we display all the pagination links using for loop upto the $num value.The second if statement display the Next page liên kết if the pageno is not equal khổng lồ 1. Remember always vì proper form validation before và after submitting the form.
That"s all, this is how to lớn create and implete the basic PHP Pagination with MySQL. You can customize this code further as per your requirement. And please feel không lấy phí to give comments on this tutorial.And if you like this tutorials please nói qua it with your friends via e-mail or Social Media.
ABOUT darkedeneurope.com ❯
darkedeneurope.com is one of the best và biggest trang web for web developers in India. We have tutorials, demos, products review & offers for website developers và designers
AFFILIATE DISCLOSURE ❯
We as darkedeneurope.com may receive compensation from some of the companies whose products we review.