0% found this document useful (0 votes)
8 views16 pages

Lecture 10 - Paginating Query Results

The document outlines the process of paginating query results in dynamic web development using SQL's LIMIT clause. It explains how to display results in pages, calculate the number of pages based on records, and create navigation links for previous and next pages. Additionally, it provides examples of SQL queries and PHP code to implement pagination effectively.

Uploaded by

Pele Mpele
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views16 pages

Lecture 10 - Paginating Query Results

The document outlines the process of paginating query results in dynamic web development using SQL's LIMIT clause. It explains how to display results in pages, calculate the number of pages based on records, and create navigation links for previous and next pages. Additionally, it provides examples of SQL queries and PHP code to implement pagination effectively.

Uploaded by

Pele Mpele
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

DYNAMIC WEB DEVELOPMENT

PAGINATING QUERY RESULTS

Dr. Basel Almourad


GOALS
Display the results as a series of pages and not as
one long list

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;

Count the number of records in the database.


e.g. ($nbr_records : SELECT COUNT(user_id) FROM user;

Calculate the total number of pages needed


$nbr_pages = ceil($nbr_records / $nbr_diaplay);
PAGINATING QUERY RESULTS
 Paginating Query Results
2. Create the links for different pages. Always the link carry
pointer to the next DB records and the No of Pages

for ($i = 1; $i <= $nbr_pages; $i++) {


if ($i != $current_page) {
$p_start=$nbr_display *($i-1); //e.g. $i=4 => 5* (4-1) =15
echo "<a href='paginated_view_users.php?s=$p_start
&p=$nbr_pages'> $i</a>";
} else {
echo " $i "; // no link because it is the current page
}
}

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

$current_page = ($start / $nbr_display) + 1;

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

Starting index of current page

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>";
}

Starting index of next page

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;
}

//$start will be used in the select statement to limit the number of


retrieved records
DEFINE THE QUERY AND PRINT
CURRENT RCORDS
Paginating_view_user.php
// Define the query:
$q = "SELECT last_name, first_name, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr,
user_id FROM users ORDER BY registration_date ASC LIMIT $start, $display";
$r = @mysqli_query($dbc, $q);
echo '<table width="60%"> // Table header
<tr>
<th align="left"><strong>Edit</strong></th>
<th align="left"><strong>Delete</strong></th>
<th align="left"><strong>Last Name</strong></th>
……</tr>';
// Fetch and print all the records....
$bg = '#eeeeee'; // Set the initial background color.
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the BG color.
echo '<tr bgcolor="' . $bg . '">
<td align="left"><a href="edit_user.php?id=' . $row['user_id'] . '">Edit</a></td>
<td align="left"><a href="delete_user.php?id=' . $row['user_id'] . '">Delete</a></td>
<td align="left">' . $row['last_name'] . '</td>
…..</tr>';
} // End of WHILE loop.
echo '</table>’;
mysqli_free_result($r);
mysqli_close($dbc);
MAKE THE LINKS TO OTHER
PAGES, IF NECESSARY
if ($pages > 1) { Paginating_view_user.php
// Add some spacing and start a paragraph:
echo '<br><p>';
// Determine what page the script is on:
$current_page = ($start/$display) + 1;
// If it's not the first page, make a Previous link:
if ($current_page != 1) {
echo '<a href="paginating_view_users.php?s=' . ($start - $display) . '&p=' . $pages .
'">Previous</a> ';
}
// Make all the numbered pages:
for ($i = 1; $i <= $pages; $i++) {
if ($i != $current_page) {
echo '<a href="paginating_view_users.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i .
'</a> ‘;
} else {
echo $i . ' ‘;
}
} // End of FOR loop.
// If it's not the last page, make a Next button:
if ($current_page != $pages) {
echo '<a href="paginating_view_users.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>';
}
echo '</p>'; // Close the paragraph.
} // End of links section.
ANY QUESTIONS?

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

You might also like