Tuesday, August 26, 2014

PHP Pagination

12:44 AM Posted by Zayn Ali No comments
Pagination script by zayn


Setting up the database. Save file as "Pagination.sql" and then import it in PHPMyAdmin Pagination Database created.
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

CREATE DATABASE IF NOT EXISTS `pagination` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `pagination`;

CREATE TABLE IF NOT EXISTS `names` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;

INSERT INTO `names` (`id`, `name`) VALUES
(1, 'ali'),
(2, 'mohsin'),
(3, 'yasir'),
(4, 'zain'),
(5, 'umar'),
(6, 'hassan'),
(7, 'imtiaz'),
(8, 'nadir'),
(9, 'usman'),
(10, 'farooq'),
(11, 'jameel'),
(12, 'nouman'),
(13, 'faisal'),
(14, 'kashif'),
(15, 'adnan');
Now the basic pagination Script. Save the file as "Pagination.php"
/**
 * @author: Zain Ali
 *
 */

 // Setting up Query string variable (page) for page number
 $page = isset($_GET['page']) ? (int) $_GET['page'] : 1; 

 // Setting up DB connection
 $sqli = new mysqli('localhost', 'root', '', 'pagination');

 // Storing total numbers of rows in the variable
 $result = $sqli->query("SELECT * FROM `names`");
 $total_rows = $result->num_rows;

 // Setting up Per Page Results
 $per_page_rows = 4;

 // Calculating the Last Page Number
 $last_page = ceil($total_rows / $per_page_rows);

 // Making sure that page number isn't below 1 or more than our Last page 
 if ($page < 1) { 
  $page = 1;
 } elseif ($page > $last_page) { 
  $page = $last_page;
 }

 // Setting up the limit to display results Per Page
 $limit = "LIMIT " . ($page - 1) * $per_page_rows . ", " . $per_page_rows;

 $result = $sqli->query("SELECT * FROM `names` " . $limit);
Our Basic Mark Up for our Demo Pagination page "Index.php"



 
 Pagination Script By Zayn
 


 
Pagination Script By Zayn

In the ID Results div. Displaying the results by iterating over the results array which we've fetched from Database.
// Fetching & displaying Results
while ($row = $result->fetch_object()) {
 echo $row->name . "
"; }
Last in ID links. Displaying the Pagination links.
// First Page Link
if ($page != 1) {
 echo "First  ";
}

// Page Indexes
for ($i = 1; $i <= $last_page; $i++) {
 if ($page == $i) {
  echo "" . $i . "  ";
 } else {
  echo "" . $i . "  ";   
 }
}

// Last Page link
if ($page != $last_page) {
 echo "Last  ";
}
A little CSS for styling our pagination.
* {
 font-size: 1.1em;
 font-family: Trebuchet MS;
 font-weight: lighter;
}

#wrap {
 width: 600px;
 margin: auto;
 padding: 30px;
 background-color: #f4f4f4;
 border: 1px solid black;
}

#links {
 margin-top: 10px;
 text-align: center;
}

#results {
 height: 130px;
 border: 1px dashed black;
 padding: 10px;
 background-color: white;
}

.page-links {
 padding-left: 15px;
 padding-right: 15px;
 border: 1px solid black;
 background-color: #ededed;
 text-decoration: none;
 color: black;
}

.page-links:hover {
 color: white;
 background-color: #4e5c8e;
}

.page-links:active {
 background-color: black;
}

0 comments:

Post a Comment