![]() ![]() $total_pages = ceil($total_records / $results_per_page) 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).Ĭalculating the amount of pages needed using PHP can be done using ceil() function. The $total_records is now equal to the number of records that we have in our database, in our case 100. $total_pages = ceil($row / $results_per_page) $sql = "SELECT COUNT(ID) AS total FROM ".$datatable To do this we run another query using COUNT() function. Next, we need to find out the total amount of records in our table and the number of pages that we will need. index.php?page=2) to go to different pages. Later you will see that we will pass a "page" value using the URL (e.g. $start_from = ($page-1) * $results_per_page Īre used to create a $start_from variable depending on the page that we want to view. Now, when you open index.php in your web browser you will see table showing the first 20 records from your "students" table. The code below selects and then prints the data in a table. Next thing to do is to make a PHP file called index.php which will show the first 20 records from our table. ![]() We will first define MySQL connection variables and connnect to the databaseĭie("Connection failed: ". So basically in this clause (LIMIT start, count) "start" specify the starting record and "count" specifies how many records to show. Shows 20 records sorted again by name but this time it will start from the 50th record. ![]() $sql = "SELECT * FROM students ORDER BY name ASC LIMIT 50, 20" Returns 20 records sorted by name starting from the first record. $sql = "SELECT * FROM students ORDER BY name ASC LIMIT 0, 20" The LIMIT clause also allows you to specify which record to start from. To do this we will need to use the LIMIT clause for SELECT command so we can limit the query to show only 20 records. Instead of doing a single SELECT query and display all the 100 records on a single page we can have 5 pages each containing maximum 20 records. We have MySQL table called "students" holding 100 records with the following fields: This 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. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |