mysql - PDO php pagination -
i have been trying add secure pagination page (from mysql database). after looking online 1 think sqj-injection etc free found: http://www.phpro.org/tutorials/pagination-with-php-and-pdo.html
however when use script error: ah01215: php fatal error: call undefined method pager::getpagerdata() in /path....to..etc/index.php on line 62
line 62 is:
$pager = pager::getpagerdata($_session['total_records'], $limit, $page);
full code:
<?php /*** make or break ***/ error_reporting(e_all); /*** begin session ***/ session_start(); /*** include database connection class ***/ include 'db.php'; /*** include pager class ***/ include 'pager.php'; /*** set page name ***/ $page_name = htmlentities($_server['php_self']); /*** set number of results per page ***/ $limit = 20; /*** check session array total_records ***/ if(!isset($_session['total_records'])) { try { /*** first count of records ***/ $sql = "select count(id) total tablename"; $stmt = db::getinstance()->prepare($sql); $stmt->execute(); $_session['total_records'] = $stmt->fetch(pdo::fetch_column); } catch (exception $e) { $_session['total_records'] = 0; } } /*** check page number in ***/ if( filter_has_var(input_get, "page") == false) { /*** no page in ***/ $page = 1; } /*** if page number not int or not within range, assign page 1 ***/ elseif(filter_var($_get['page'], filter_validate_int, array("min_range"=>1, "max_range"=>$_session['total_records'])) == false) { $page = 1; } else { /*** if well, assign ***/ $page = (int)$_get['page']; } /*** if have no results there no point in going on ***/ if($_session['total_records'] == 0) { $content = 'no records available'; } else { /*** feed variables pager class ***/ $pager = pager::getpagerdata($_session['total_records'], $limit, $page); /*** retrieve variables pager class ***/ $offset = $pager->offset; $limit = $pager->limit; $page = $pager->page; /*** begin menu ***/ $menu = ''; /*** if page 1 there no previous link ***/ if($page != 1) { $menu .= '<li><a href="'.$page_name.'?page='.($page - 1).'"><< prev </a></li>'; } /*** loop on pages ***/ ($i = 1; $i <= $pager->num_pages; $i++) { if ($i == $pager->page) { $menu .= '<li class="selected">'.$i.'</li>'; } else { $menu .= '<li><a href="'.$page_name.'?page='.$i.'">'.$i.'</a></li>'."\n"; } } /*** if on last page, not need next link ***/ if ($page < $pager->num_pages) { $menu .= '<li><a href="'.$page_name.'?page='.($page + 1).'"> next >></a></li>'; } /*** our sql statement ***/ $sql ='select * tablename limit :limit offset :offset'; /*** run query ***/ $db = db::getinstance(); $stmt = $db->prepare($sql); $stmt->bindparam(':limit', $limit, pdo::param_int); $stmt->bindparam(':offset', $offset, pdo::param_int); $stmt->execute(); $res = $stmt->fetchall(pdo::fetch_assoc); /*** elements table content ***/ $content = ''; foreach ($res $el) { $content .= ' <tr><td>'.$el['id'].'</td> <td>'.$el['metatitle'].'</td> <td>'.$el['date'].'</td></tr>'; } } ?> <!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>php pagination</title> <style type="text/css"> ul{ margin: 0 auto; width: 395px; list-style-type:none; } li{ display:inline; } li.selected{ float:left; text-decoration:none; color:black; font-weight:bold; background-color: #c0c0c0; padding:10px; padding-bottom: solid 1px red; } a{ float:left; text-decoration:none; color:green; padding:10px; padding-bottom: 1px; } a:hover{ border-bottom: solid 1px red; padding-bottom: 1px; } table { clear:both; margin: 0 auto; } </style> </head> </body> <ul> <?php echo $menu; ?> </ul> <table class="elements"> <thead> <tr> <th>atomic number</th> <th>latin</th> <th>english</th> </tr> </thead> <tbody> <?php echo $content; ?> </tbody> <tfoot> <tr><th colspan="3">table of elements</th> </tr> </tfoot> </table> </body> </html>
pager.php
<?php /* * example usage * $pager = new pager(); * $pager->num_results = $product_count; * $pager->limit = $config->config_values['product']['display_num']; * $pager->page = $page_num; * $pager->menu_link = '/category/electrical'; * $pager->menu_link_suffix = '/foo/bar'; ( optional ) * $pager->css_class = 'fubar'; ( optional ) * $pager->run(); * echo $pager; * */ class pager{ /** * * constructor, duh! * * @access public * @param $num_pages * @param $limit * @param $page * */ public function __construct( $num_results=null, $limit=null, $page=null ) { if( !is_null( $num_results ) && !is_null( $limit ) && !is_null( $page ) ) { $this->num_results = $num_results; $this->limit = $limit; $this->page = $page; $this->run(); } } /** * * settor * * @param string $name * @param mixed $value * */ public function __set( $name, $value ) { switch( $name ) { case 'menu_link_suffix': case 'num_results': case 'menu_link': case 'css_class': case 'num_pages': case 'offset': case 'limit': case 'page': $this->$name = $value; break; default: throw new \exception( "unable set $name" ); } } /** * * gettor * * @param string $name * */ public function __get( $name ) { switch( $name ) { case 'menu_link_suffix': case 'num_results': case 'menu_link': case 'css_class': case 'num_pages': case 'offset': case 'limit': case 'page': return $this->$name; break; default: throw new \exception( "unable $name" ); } } /** * @calculate paging inforomation * * @access public * @param int $num_pages * @param int $limit * @param $page * @return object * **/ public function run() { /*** number of pages ***/ $this->num_pages = ceil( $this->num_results / $this->limit ); $this->page = max( $this->page, 1 ); $this->page = min( $this->page, $this->num_pages ); /*** calculate offset ***/ $this->offset = ( $this->page - 1 ) * $this->limit; } /** * * return html string representation of pager links * links in <ul> css class name * * @access public * @retun string * */ public function __tostring() { $menu = '<ul'; $menu .= isset( $this->css_class ) ? ' class="'.$this->css_class.'"' : ''; $menu .= '>'; /*** if page 1 there no previous link ***/ if($this->page != 1) { $menu .= '<li><a href="'.$this->menu_link.'/'.( $this->page - 1 ); $menu .= isset( $this->menu_link_suffix ) ? $this->menu_link_suffix : ''; $menu .= '">prev</a></li>'; } /*** loop on pages ***/ for( $i = 1; $i <= $this->num_pages; $i++ ) { if( $i == $this->page ) { $menu .= '<li class="active"><a href="'.$this->menu_link.'/'.$i; $menu .= isset( $this->menu_link_suffix ) ? $this->menu_link_suffix : ''; $menu .= '">'.$i.'</a></li>'; } else { $menu .= '<li><a href="'.$this->menu_link.'/'.$i; $menu .= isset( $this->menu_link_suffix ) ? $this->menu_link_suffix : ''; $menu .= '">'.$i.'</a></li>'; } } /*** if on last page, not need next link ***/ if( $this->page < $this->num_pages ) { $menu .= '<li><a href="'.$this->menu_link.'/'.( $this->page + 1 ); $menu .= isset( $this->menu_link_suffix ) ? $this->menu_link_suffix : ''; $menu .= '">next</a></li>'; } return $menu; } } /*** end of class ***/ ?>
dp.php
<?php class db{ /*** declare instance ***/ private static $instance = null; /** * * constructor set private * nobody can create new instance using new * */ private function __construct() { /*** maybe set db name here later ***/ } /** * * return db instance or create intitial connection * * @return object (pdo) * * @access public * */ public static function getinstance() { if (!self::$instance) { self::$instance = new pdo("mysql:host=localhost;dbname=dbname", 'user', 'pass');; self::$instance-> setattribute(pdo::attr_errmode, pdo::errmode_exception); } return self::$instance; } /** * * constructor, make __clone private * nobody can clone instance * */ private function __clone(){ } } /*** end of class ***/ ?>
any ideas? if see code think security weak, please feel free point out :)
thanks in advance
p.s. removed user/pass tablename etc here
try changing line 62 to:
$pager = pager::getpagerdata($_session['total_records'], $limit, $page);
your class defined lower case 'p'.
however, unless there's magic going on, method still hasn't been defined anywhere, can't see how ever work. might best in touch author of article , ask.
note:
it considered best practice start class names upper case.
Comments
Post a Comment