![]() |
shopping cart (0 item) |
||||||
|
|||||||
![]() ![]()
|
PHP / MySQL select data and split on pagesThis tutorial is going to show you how to SELECT data from a MySQL database, split it on multiple pages and display it using page numbers. We have MySQL table called “students” holding 90 records with the following fields: $sql = "SELECT * FROM students ORDER BY name ASC LIMIT 0, 20"; returns 20 records sorted by name starting from the first record. This next query $sql = "SELECT * FROM students ORDER BY name ASC LIMIT 50, 20"; shows 20 records sorted again by name but this time it will start from the 50th record. Next thing to do is to make a PHP file called pagination.php which will show the first 20 records from our table. The code below selects and then prints the data in a table. <?php if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; }; $start_from = ($page-1) * 20; $sql = "SELECT * FROM students ORDER BY name ASC LIMIT $start_from, 20"; $rs_result = mysql_query ($sql, $connection); ?> <table> <tr><td>Name</td><td>Phone</td></tr> <?php while ($row = mysql_fetch_assoc($rs_result)) { ?> <tr> <td><? echo $row["Name"]; ?></td> <td><? echo $row["PhoneNumber"]; ?></td> </tr> <?php }; ?> </table> Now, when you open pagination.php in your web browser you will see table showing the first 20 records from your ‘students’ table. The first 2 lines of the above code if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; }; $start_from = ($page-1) * 20; are used to create a $start_from variable depending on the page that we want to view. Later you will see that we will pass a “page” value using the URL (e.g. pagination.php?page=2) to go to different pages. Next we need to find out the total amount of records in our table and the number of pages that we will need. To do this we run another query using COUNT() function. $sql = "SELECT COUNT(Name) FROM students"; $rs_result = mysql_query($sql,$connection); $row = mysql_fetch_row($rs_result); $total_records = $row[0]; The $total_records is now equal to the number of records that we have in our database, in our case 90. We have 20 records per page so the total number of pages that will be needed is 5 (4 pages with 20 records and last page will have 10 records). Calculating the amount of pages needed using PHP can be done using ceil() function. $total_pages = ceil($total_records / 20);We divide the total number of records by records per page and then the ceil() function will round up the result. Now we have 2 new variables - $total_records equal to 90 and $total_pages equal to 5. To print page numbers and associate URLs to each number we will use for() cycle. <?php for ($i=1; $i<=$total_pages; $i++) { echo "<a href='pagination.php?page=".$i."'>".$i."</a> "; }; ?> Above code will print numbers from 1 to 5 and for each number will create different link. At the end you should have a file like this (remember to add the MySQL connection string): <?php if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; }; $start_from = ($page-1) * 20; $sql = "SELECT * FROM students ORDER BY name ASC LIMIT $start_from, 20"; $rs_result = mysql_query ($sql,$connection); ?> <table> <tr><td>Name</td><td>Phone</td></tr> <?php while ($row = mysql_fetch_assoc($rs_result)) { ?> <tr> <td><? echo $row["Name"]; ?></td> <td><? echo $row["PhoneNumber"]; ?></td> </tr> <?php }; ?> </table> <?php $sql = "SELECT COUNT(Name) FROM students"; $rs_result = mysql_query($sql,$connection); $row = mysql_fetch_row($rs_result); $total_records = $row[0]; $total_pages = ceil($total_records / 20); for ($i=1; $i<=$total_pages; $i++) { echo "<a href='pagination.php?page=".$i."'>".$i."</a> "; }; ?> This pagination.php file will print a table with maximum 20 records per page and at the bottom 5 page numbers each pointing to a page showing different 20 records. Do not forget that for a small fee I can add pagination to all your PHP files. Let me know if you need help with this and I will give you a quote. COMMENTS
Shakeel Hi thanks! this is a nice example explaining very clearly. ANIL Android 1.5 Cupcake 3 0.6% Android 1.6 Donut 4 1.1% Android 2.1 Eclair 7 8.5% Android 2.2 Froyo 8 30.4% Android 2.3 - Android 2.3.2 Gingerbread 9 0.6% Android 2.3.3 - Android 2.3.7 10 54.9% Android 3.0 Honeycomb 11 0.1% Android 3.1 12 1.5% Android 3.2 13 1.7% Android 4.0 - Android 4.0.2 Ice Cream Sandwich 14 0.3% Android 4.0.3 15 0.3% microdz hello thank you for This tutorial How can I find out the user MAC ADDRESS windows && Linux && mac OS TALHA Thanks a lot. The best as compared to the other different Tutorials progrms. amir do you have a similar tutorial but with picturs. I mean displaying pictures from a database. I can't find it any where on the net. Is it too hard to do. It is used a lot on lots of sites, where you do a search fon an item, say a property search and the results shows all the houses with in a town at a certain price. but with all the pictures also showing. kelum Hii dear. I have error occured like this Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in C:\wamp\www\kelum\php\pagenation\page.php on line 35 that's the line 35 code: $row = mysql_fetch_row($rs_result); How can I clear that code. thank you very much. Bunleab Pls help me in my page it shows only first page when click to other pages it gets error like this: Not Found The requested URL /Business_Investment/BusinessList/pagination.php was not found on this server. afsal_cmk Hi, Thanks alot,,, the only one thing I wanted and you helped me is that,, LIMIT from to end. Until now I only used limit 20; or limit 10; but not as limit 5 to 10 or limit $start from $ end or Limit $start from 10; Thanks again. Patrik Thankäs for your reply. How about if i whant to show only some of the fileds in the table. (How do i hide fields?) Rick @Patrik If u want to sort with last entry, you will have to add a field to your database, most people call it id, this should be an INT and it must be the PRIMARY key of your table. Each time you add an entry, this will happen to your table: id name phonenumber -- current entries -- 1 Josh 0612345678 2 Ron 0687654321 -- new entry -- 3 New 0612341234 now to sort out u do this: $sql = "SELECT * FROM table_name ORDER BY id DESC LIMIT $start_from, 20" So if u know echo things this is how it will look: id name phonenumber 3 New 0612341234 2 Ron 0687654321 1 Josh 0612345678 I hope you understand it and this helped you. POST A COMMENT
|