smallest pagination class in php
Pagination class in PHP using array functions
Did you search for php training in kolkata? Okay, it might come to your help.
This is probably the smallest pagination class in php, that you can use to paginate. You need four pages, to show first row, previous row, next row and the last row. I keep pagination class in my 'inc' folder and include it in four pages. I used only two array functions. prev() and next(). You may modify this code to add more features.
1) The pagination class code or pagination.php/* * This is pagination class */ class pagination { var $_con=''; var $_db=''; var $_query=''; var $_page; var $_total_rows; function __construct($con, $db) { $this->_con = $con; $this->_db = $db; } function firstpage($query) { $this->_query = mysql_query($query); while ($row = mysql_fetch_array($this->_query)) { echo "Title : ". $row['title']; echo "Description : ". $row['description']; $next=next($row); $next++; } echo "<a href='http://localhost/ClassesAndObjects/page3.php?page={$next}'>NEXT</a>";"; if(isset ($_REQUEST['page'])){ $this->_page = $_REQUEST['page']; } } function paginate() { $this->_page = $_REQUEST['page']; if(isset ($_REQUEST['page'])){ $this->_page = $_REQUEST['page']; } $all_rs = &mysql_query('select * from comments'); $this->_total_rows = mysql_num_rows($all_rs); $this->_query = mysql_query('select * from comments where id="'.$this->_page.'"'); while ($row = mysql_fetch_array($this->_query)) { echo "Title : ". $row['title']; echo "Description : ". $row['description']; $next = next($row); $next++; $prev = prev($row); $prev--; echo "<a href='http://localhost/ClassesAndObjects/page1.php'>FIRST PAGE</a>"; echo '<------>'; echo "<a href='http://localhost/ClassesAndObjects/page2.php?page={$next}'>PREV</a>"; echo '<------>'; echo "<a href='http://localhost/ClassesAndObjects/page3.php?page={$next}'>NEXT</a>"; echo '<------>'; echo "<a href='http://localhost/ClassesAndObjects/page4.php?page={$this->_total_rows}'>LAST PAGE</a>"; } //at the beginning and at the end of the row $prev and $next become NULL //we go back to the first page if($next==NULL || $prev==NULL){ header('Location:http://localhost/ClassesAndObjects/page1.php'); } } } 2) To show the first row i start with page1.php. Here is the code... include_once 'inc/pagination.php'; $con = mysql_connect('localhost', 'root', ''); $db = mysql_select_db('appababa'); $query = 'select * from comments LIMIT 1'; $pagination = new pagination($con, $db); $pagination->firstpage($query); 3) To show the previous post i use page2.php. Here is the code... include_once 'inc/pagination.php'; $con = mysql_connect('localhost', 'root', ''); $db = mysql_select_db('appababa'); $query = 'select * from comments'; $pagination = new pagination($con, $db); $pagination->paginate(); 4) To show the previous post i use page3.php. Here is the code... include_once 'inc/pagination.php'; $con = mysql_connect('localhost', 'root', ''); $db = mysql_select_db('appababa'); $query = 'select * from comments'; $pagination = new pagination($con, $db); $pagination->paginate(); 5) To show the last post i use page4.php. Here is the code... include_once 'inc/pagination.php'; $con = mysql_connect('localhost', 'root', ''); $db = mysql_select_db('appababa'); $query = 'select * from comments'; $pagination = new pagination($con, $db); $pagination->paginate();










