Lecture 10 - Paginating Query Results
Lecture 10 - Paginating Query Results
OUTLINE
Make use of
LIMIT SQL clause
LIMIT restricts which subset of the
matched records is actually
returned.
PAGINATING QUERY RESULTS
Objective
Display the results as a series of pages and not as one
long list
Make use of
LIMIT SQL clause
LIMIT restricts which subset of the matched records is
actually returned.
Previous 1 2 3 Next
PAGINATING QUERY RESULTS
Limiting Query Results
LIMIT states how many records to return
Examples.
1. SELECT * FROM tablename LIMIT 5
only the first 5 records will be returned
2. SELECT * FROM tablename LIMIT 10, 5
returns 5 records starting from record 11
returns records 11 till 15
PAGINATING QUERY RESULTS
Paginating Query Results
1. Identify the number of pages to use
Define the number of records to display per page
e.g. $nbr_display = 5;
Previous 1 2 3 Next
PAGINATING QUERY RESULTS
Paginating Query Results
nbr_display =5
3. Create the links for different pages records per page
Define the starting index for each page
o Page 1: $p_start=0;
o Page i: $p_start=$nbr_display *($i-1);
o e.g. i=4: $p_start=5 *(4-1)= 15 (means page 4 has the records 15 – 20).
o e.g. i=1: $p_start=5 *(1-1)= 0 (means page 1 has the records 0 – 5).
For each link, add the starting index + the total number of pages
paginated_view_users.php?s=0&p=3
Previous 1 2 3 Next
PAGINATING QUERY RESULTS
Paginating Query Results
4. If it's not the first page, make a Previous link:
finding the current page number
Previous 1 2 3 Next
PAGINATING QUERY RESULTS
Paginating Query Results
5. Create the links for different pages
If not 1st page, add “Previous” link
if ($current_page != 1) {
$previous_start==$start - $nbr_display;
echo "<a href='paginated_view_users.php?s=$previous_start
&p=$nbr_pages'> Previous</a>";
}
Starting index of previous
page
Previous 1 2 3 Next
PAGINATING QUERY RESULTS
Paginating Query Results
6. Create the links for different pages
If not last page, add “Next” link
if ($current_page != $nbr_pages) {
$next_start =$start + $nbr_display;
echo "<a href='paginated_view_users.php?s=next_start
&p=$nbr_pages'> Next</a>";
}
Previous 1 2 3 Next
GUESSING THE NO OF PAGES
Paginating_view_user.php
$display = 10; // Number of records to show per page:
// Determine how many pages there are...
if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined.
$pages = $_GET['p'];
} else { // Need to determine.
// Count the number of records:
$q = "SELECT COUNT(user_id) FROM users";
$r = @mysqli_query($dbc, $q);
$row = @mysqli_fetch_array($r, MYSQLI_NUM);
$records = $row[0]; $r is only one column~ number represents No of
records
// Calculate the number of pages...
if ($records > $display) { // More than 1 page.
$pages = ceil ($records/$display); //e.g. 33/10=4
} else {
$pages = 1;
}
} // End of p IF.
DETERMINE WHERE IN THE DB TO
START RETURNING RESULTS...
Paginating_view_user.php
// Determine where in the database to start returning results...
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
15
REFERENCES AND MORE READING
• Book
• Chapter 10: Common Programming Techniques
• Chapter 5: Limiting Query Results
• W3School
• https://www.w3schools.com/php/func_math_ceil.asp
• http://php.net/manual/en/class.mysqli-stmt.php
16