fifin04 Posted May 19, 2010 Share Posted May 19, 2010 aii guys....really stuck up here...my attempt paginating my recordset no to avail...so far..I manage to get display the numbering for the total record as u can see here... somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 my desire just wanna display previous and next and next 10 record till reach total records as u can see here.. somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord somerecord previous 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 next after user click next it will display another 20 records previous 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 next and so forth.. Hope someone will shed me some light...here's the mockup illustration waht I after.. testpaging.php include("DataBase.class.php"); include("Pagination.class.php"); $db = DataBase::getInstance(); $row = $db->executeGrab("SELECT * FROM ".PADU_NOTICE." WHERE DEPARTMENT_CODE='04'"); if($row){ $numRows = $db->getNumRow(); if(isset($_GET['p'])){ $pg = $_GET['p']; }else{ $pg = 1; } $targetPage = $_SERVER['PHP_SELF']; $pager = Pagination::getInstance($row, $pg, $targetPage, 20,$numRows); if(is_object($pager)){ foreach($pager->__getPage($pg) as $item) { echo 'NOTICE NO :: '.$item['NOTICE_NO'].'<br>'; } echo "<br/>\n"; echo "<br/>\n"; echo "<br/>\n"; echo "<div align='center'>".$pager->__getNav()."</div>"; } } here's the paginationclass class Pagination{ static private $instance = false; public $_properties; public $pages; public $data = array(); public $_pageNum; public $_targetPage; public $_perPage; public $_nav; private $_numrows; private function __construct($data, $pageNum, $targetPage, $perPage, $numRows){ $this->_data = $data; $this->_pageNum = $pageNum; $this->_targetPage = $targetPage; $this->_perPage = $perPage; $this->_numrows = $numRows; $this->pages = array(); $this->buildPaging($this->_data); $this->buildNav(); } public static function getInstance($data=NULL, $pageNum=NULL, $targetPage=NULL, $perPage=NULL, $numRows=NULL){ if(!Pagination::$instance) { Pagination::$instance = new Pagination($data, $pageNum, $targetPage, $perPage, $numRows); } return Pagination::$instance; } public function __set($name, $value) { if(isset($name)) { $this->_properties[$name] = $value; } } public function __get($name){ if(isset($this->_properties[$name])){ return $this->_properties[$name]; }else{ return NULL; } } public function buildPaging($dataArr=NULL){ if(is_array($dataArr)){ $numberPages = count($dataArr)/$this->_perPage; if (round($numberPages)<$numberPages) { $numberPages = round($numberPages) + 1; } $k=0; for ($i=0;$i<$numberPages;$i++){ for($j=0;$j<$this->_perPage;$j++){ if(isset($dataArr[$k])){ $this->pages[$i][$j] = $dataArr[$k]; } $k++; } } } } /////////////////////////////portions printing the numbering////////////////////////////// //need help here.. public function buildNav(){ $this->_nav = ''; foreach($this->pages as $key => $page){ if($key == $this->_pageNum){ $style = ' style="font-weight:bold;text-decoration:none;color:#000;"'; } else { $style = ''; } $this->_nav .= '<a href="' .$this->_targetPage. '?page=' .$key. '"' . $style . '>' .$key. '</a> '; } } /////////////////////////////////////////////////////////////// public function __getNav() { return $this->_nav; } public function __getPage($num) { return $this->pages[$num-1]; } public function __clone(){ trigger_error("Clone is not allowed.", E_USER_ERROR); } } Hope someone will shed me some light..thanks you in advanced Quote Link to comment https://forums.phpfreaks.com/topic/202237-need-help-with-simple-paging-class/ Share on other sites More sharing options...
JAY6390 Posted May 19, 2010 Share Posted May 19, 2010 My pagination class will do what you want, however you'll need to swap the procedural mysql functions into your db functions to get it to work via the $db object you've got Quote Link to comment https://forums.phpfreaks.com/topic/202237-need-help-with-simple-paging-class/#findComment-1060445 Share on other sites More sharing options...
ignace Posted May 19, 2010 Share Posted May 19, 2010 Your design is suffering from the illness Singletonitis. Try my Paginator class: class Paginator implements Iterator { private $key = 0; private $sizeOf = 0; private $itemsPerPage; private $data = array(); private $activePage = 1; public function __construct($array, $itemsPerPage) { $this->data = $array; $this->sizeOf = sizeof($array); $this->itemsPerPage = abs((int)$itemsPerPage); } public function getItemsForPage($pageNumber) { $pageNumber = $pageNumber > 0 ? $pageNumber : 1; $pageNumber = $pageNumber <= $this->getNumberOfPages() ? $pageNumber : $this->getNumberOfPages(); $this->activePage = $pageNumber; $this->key = $this->calculateOffset($pageNumber); return $this; } public function getActivePage() { return $this->activePage; } public function getNumberOfPages() { return ceil($this->sizeOf / $this->itemsPerPage); } public function valid() { return $this->key < $this->calculateOffset($this->getActivePage() + 1) && $this->key < $this->sizeOf; } public function key() { return $this->key; } public function next() { $this->key++; } public function rewind() { //$this->key = 0; } public function current() { return new ArrayObject($this->data[$this->key], ArrayObject::ARRAY_AS_PROPS); } private function calculateOffset($pageNumber) { return ($pageNumber - 1) * $this->itemsPerPage; } } Use it like: $rows = $db->execute('SELECT * FROM table')->fetchAll(); $paginator = new Paginator($rows, 20); $items = $paginator->getItemsForPage(1); foreach ($items as $item) { //whatever } @Jay Why did you limit your Paginator to MySQL? What if you were to use PostGre or Oracle? Quote Link to comment https://forums.phpfreaks.com/topic/202237-need-help-with-simple-paging-class/#findComment-1060457 Share on other sites More sharing options...
fifin04 Posted May 21, 2010 Author Share Posted May 21, 2010 JAY6390-> Thanks for the class but FYI I'm using MSSQL Server as my database so as i can see in ur class it just for Mysql databases only...correct me if Im wrong.. ignace-> I've been playing with ur class but it seems the paging numbering not appear..or is it I missing something..the result looks like this..the 1st 20 records... B117646 B123851 B123940 B123939 B123938 B123937 B123936 B123920 B123935 B123934 B123933 B123932 B123919 B123931 B123930 B123918 B123929 B123917 B123916 B123913 paging numbering???????not appear... So hope u will shed me some light on this...sorry for silly questions..my code include("DataBase.class.php"); include("Paginator.class.php"); $db = DataBase::getInstance(); if($db){ $row = $db->executeGrab("SELECT * FROM ".PADU_NOTICE." WHERE DEPARTMENT_CODE='04'"); if($row){ $paginator = new Paginator($row, 20); $items = $paginator->getItemsForPage(1); foreach ($items as $item) { echo $item["NOTICE_NO"]."<br/>\n"; } } } Tq in advanced Quote Link to comment https://forums.phpfreaks.com/topic/202237-need-help-with-simple-paging-class/#findComment-1061446 Share on other sites More sharing options...
ignace Posted May 21, 2010 Share Posted May 21, 2010 To get the active page and the number of pages use the corresponding methods. include("DataBase.class.php"); include("Paginator.class.php"); $db = DataBase::getInstance(); $row = $db->executeGrab("SELECT * FROM ".PADU_NOTICE." WHERE DEPARTMENT_CODE='04'"); if($row){ $paginator = new Paginator($row, 20); $items = $paginator->getItemsForPage(1); echo $paginator->getActivePage(), ' of ', $paginator->getNumberOfPages(), ' pages.', "<br/>\n<br/>\n"; foreach ($items as $item) { echo $item["NOTICE_NO"], "<br/>\n"; } } Quote Link to comment https://forums.phpfreaks.com/topic/202237-need-help-with-simple-paging-class/#findComment-1061505 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.