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.
0 comments:
Post a Comment