Page Navigation Using PDO Query in PHP/MySQL
Submitted by argie on Thursday, November 21, 2013 - 12:38.
This tutorial will teach you on how to create a content pager using PHP/MySQL. I uses PDO query to retrieve the data from database table. This tutorial is useful when you have multiple data from the database. This code divide the content and display by page.
To start this tutorial fallow the steps bellow:
That's it, you've been successfully created a PHP content pager using PDO Query, thank you.
Creating Our Database
First we are going to create our database which stores our data. To create a database: 1. Open phpmyadmin 2. Then create database and name it as "pdo_ret". 3. After creating a database name, click the SQL and paste the following code.- CREATE TABLE IF NOT EXISTS `members` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `fname` varchar(100) NOT NULL,
- `lname` varchar(100) NOT NULL,
- `age` int(5) NOT NULL,
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
Creating our Database Connection
Next step is to create a database connection and save it as "connect.php". In this Step, we will write our connection script in PDO format.- <?php
- /* Database config */
- $db_host = 'localhost';
- $db_user = 'root';
- $db_pass = '';
- $db_database = 'pdo_ret';
- /* End config */
- $db = new PDO('mysql:host='.$db_host.';dbname='.$db_database, $db_user, $db_pass);
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- ?>
Creting our Display Page
In this steps we will write our script that retrieve our data from database table and display the data by page, we will do that by using the PDO query. Save this page as "index.php".Create a table that display the data
Copy the code bellow and paste on your index.php file- <table cellspacing="0" cellpadding="2" >
- <thead>
- <tr>
- <th> First Name </th>
- <th> Last Name </th>
- <th> Age </th>
- </tr>
- </thead>
- <tbody>
- <?php
- include('connect.php');
- $start_from = ($page-1) * 3;
- $result = $db->prepare("SELECT * FROM members ORDER BY id ASC LIMIT $start_from, 3");
- $result->execute();
- for($i=0; $row = $result->fetch(); $i++){
- ?>
- <tr class="record">
- <td><?php echo $row['fname']; ?></td>
- <td><?php echo $row['lname']; ?></td>
- <td><?php echo $row['age']; ?></td>
- </tr>
- <?php
- }
- ?>
- </tbody>
- </table>
Write the script that divide the content and display by page
Copy the code bellow and paste under the closing tag of your table.- <div id="pagination">
- <?php
- $result = $db->prepare("SELECT COUNT(id) FROM members");
- $result->execute();
- $row = $result->fetch();
- $total_records = $row[0];
- for ($i=1; $i<=$total_pages; $i++) {
- echo "<a href='index.php?page=".$i."'";
- if($page==$i)
- {
- echo "id=active";
- }
- echo ">";
- echo "".$i."</a> ";
- };
- ?>
- </div>
Create Our Style
this part of code will make our display more organize and beautifull to look at. paste this before the opening tag of the table.- <style>
- table {
- border-collapse: separate;
- background-color: #FFFFFF;
- border-spacing: 0;
- width: 50%;
- color: #666666;
- text-shadow: 0 1px 0 #FFFFFF;
- border: 1px solid #CCCCCC;
- box-shadow: 0 5px 5px -5px rgba(0, 0, 0, 0.3);
- margin: 0 auto;
- font-family: arial;
- margin-top: 20px;
- }
- table thead tr th {
- background: none repeat scroll 0 0 #EEEEEE;
- color: #222222;
- padding: 10px 14px;
- text-align: left;
- border-top: 0 none;
- font-size: 12px;
- }
- table tbody tr td{
- background-color: #FFFFFF;
- font-size: 11px;
- text-align: left;
- padding: 10px 14px;
- border-top: 1px solid #DDDDDD;
- }
- #pagination {
- text-align: center;
- margin-top: 20px;
- }
- #pagination a {
- border: 1px solid #CCCCCC;
- padding: 5px 10px;
- font-family: arial;
- text-decoration: none;
- background: none repeat scroll 0 0 #EEEEEE;
- color: #222222;
- }
- #pagination a:hover {
- background-color: #FFFFFF;
- }
- a#active{
- background-color: #FFFFFF;
- }
- </style>
Summary of display page
the display page will look like the code bellow.- <style>
- table {
- border-collapse: separate;
- background-color: #FFFFFF;
- border-spacing: 0;
- width: 50%;
- color: #666666;
- text-shadow: 0 1px 0 #FFFFFF;
- border: 1px solid #CCCCCC;
- box-shadow: 0 5px 5px -5px rgba(0, 0, 0, 0.3);
- margin: 0 auto;
- font-family: arial;
- margin-top: 20px;
- }
- table thead tr th {
- background: none repeat scroll 0 0 #EEEEEE;
- color: #222222;
- padding: 10px 14px;
- text-align: left;
- border-top: 0 none;
- font-size: 12px;
- }
- table tbody tr td{
- background-color: #FFFFFF;
- font-size: 11px;
- text-align: left;
- padding: 10px 14px;
- border-top: 1px solid #DDDDDD;
- }
- #pagination {
- text-align: center;
- margin-top: 20px;
- }
- #pagination a {
- border: 1px solid #CCCCCC;
- padding: 5px 10px;
- font-family: arial;
- text-decoration: none;
- background: none repeat scroll 0 0 #EEEEEE;
- color: #222222;
- }
- #pagination a:hover {
- background-color: #FFFFFF;
- }
- a#active{
- background-color: #FFFFFF;
- }
- </style>
- <table cellspacing="0" cellpadding="2" >
- <thead>
- <tr>
- <th> First Name </th>
- <th> Last Name </th>
- <th> Age </th>
- </tr>
- </thead>
- <tbody>
- <?php
- include('connect.php');
- $start_from = ($page-1) * 3;
- $result = $db->prepare("SELECT * FROM members ORDER BY id ASC LIMIT $start_from, 3");
- $result->execute();
- for($i=0; $row = $result->fetch(); $i++){
- ?>
- <tr class="record">
- <td><?php echo $row['fname']; ?></td>
- <td><?php echo $row['lname']; ?></td>
- <td><?php echo $row['age']; ?></td>
- </tr>
- <?php
- }
- ?>
- </tbody>
- </table>
- <div id="pagination">
- <?php
- $result = $db->prepare("SELECT COUNT(id) FROM members");
- $result->execute();
- $row = $result->fetch();
- $total_records = $row[0];
- for ($i=1; $i<=$total_pages; $i++) {
- echo "<a href='index.php?page=".$i."'";
- if($page==$i)
- {
- echo "id=active";
- }
- echo ">";
- echo "".$i."</a> ";
- };
- ?>
- </div>
Comments
thanks, but...
First off, great tutorial. I was just curious as whether there is a way to add a text search?
Thank you !
You safe a part of my own Project. Your Tutorial was very helpful and easy to understand.
Add new comment
- Add new comment
- 306 views