![]() |
|
||||||
|
|||||||
|
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
Sean Thanks for the tutorial -- it got me started and I just modified it accordingly. Also, for anyone following this tutorial there is a spelling mistake at line 10 of the first code snippet: while ($row = mysql_fecth_assoc($rs_result)) { should be while ($row = mysql_FETCH_assoc($rs_result)) { Thanks for the tutorial, it\'s much appreciated. ------------------------- Veselin: thanks for the spelling mistake report. I've fixed it ! Frank Thank you it helps me a lot ac thank you, the logic you put here was simple and allowed me to fix a script from another tutorial, Thanx piad hi all, im beginner for php program.. table_1 bil main_menu sub_menu status 1 class_A 2 class_B 3 class_B william sub_menu 4 class_B ramjit sub_menu ok, how can i coding if i want the output referring the data from table_1 like this:- class_A class_B william ramjit TQ Tharan Ratnam This artlcle is realy good.I was using this in my all the projects. Ravi I have One form in which i am fatching all the data from databse and don\'t want to display all but only 10 per pages and page no only should be display after 10 records to go anothe page like Page:1,2,3 --------------------- Veselin: you can use the example above and just change it to show 10 and not 20 database records per page judi thanks for this sample script - really helped me out this morning!! Adnan thank you sir, but i am very new to php, i did not understand it. but i want to do same thing in my program, what should i do??? --------- Veselin: for a small fee we can make it work on your website samonn amra selecte data in table and link to other page Fungai I have tried using rand() and it does select the questions randomly but the answers are no longer coming up as A,B,C,D they are also coming out radomly and even mixing answers from different questions, am i missing something? ------------------- Veselin: email me your database table structure and query you are using POST A COMMENT
|