waynew Posted May 23, 2009 Share Posted May 23, 2009 I sincerely want to better myself in regards to OO programming. This is one class that I've made lately so I figured that I'd post it and let you guys rip it to shreds. i.e. tell me if I've done something wrong or if you think there is a better way to do something etc. <?php class Pagination{ protected $db = NULL; protected $self = NULL; protected $rows_per_page = 0; protected $total_pages = 0; protected $page_num = 1; protected $offset = 0; protected $num_results_returned = 0; protected $query_string = NULL; /************************************************************************************************* This sets up the pagination by providing some simple information that it will use db_link = connection link from mysql ($db_link = mysql_connect(etc,etc,etc) self = the FILENAME of the page you are showing the results on - eg index.php, results.php etc rows_per_page = how many rows you want to show on each page **************************************************************************************************/ function __construct($db_link,$self,$rows_per_page){ $this->db = $db_link; //db object $this->self = $self.'?'; //self should be index.php or filename.php, etc... $this->rows_per_page = intval($rows_per_page); //how many rows to show on each page } /************************************************************************************************ To actually get the results, you need to use this function num_query = The query that will COUNT all rows in that table. The query is already half-constructed for you in the function, so all you have to do is provide the rest - FROM tablename (use a WHERE condition if needed) results_query = The query that will actually pluck out the results. This query you have to construct yourself - except for the end LIMIT part, as that is taken care of by the function. So, for example, if the results you're returning are school records - results_query would be SELECT * FROM students (ORDER if neccessary) (LIMIT taken care of automatically - dont add it yourself). *****************************************************************************************************/ function get_results($num_query,$results_query){ //count number of rows in table that apply //this should be a single row - //SELECT COUNT(*) FROM tablename WHERE column = 'condition' $res = mysql_query("SELECT COUNT(*) AS num ".$num_query,$this->db) or trigger_error(mysql_error()); $res = mysql_fetch_row($res,$this->db); mysql_free_result($res); $number_of_rows = $res[0]; //work out total number of pages $this->total_pages = ceil($number_of_rows / $this->rows_per_page); //there should be always at least one page if($this->total_pages < 1){ $total_pages = 1; } //manage page number coming in through $_GET value etc $this->get_page_number(); //work out offset $this->offset = intval(($this->page_num - 1) * $this->rows_per_page); //nothing spectacular $offset = $this->offset; $rows = $this->rows_per_page; //get results relevant to that page number $result = mysql_query($results_query. " LIMIT $offset, $rows",$this->db); //record how many results were returned $this->num_results_returned = mysql_num_rows($result); //return actual result return $result; } /************************************************************************************* You probably wont have to even touch this function. This class assumes that your page numbers are represented by $_GET['page']. If not, please change it here. *************************************************************************************/ protected function get_page_number(){ //make sure page number is set and that it's acceptable if(isset($_GET['page']) && intval($_GET['page']) != 0){ $this->page_num = intval($_GET['page']); } //make sure page number is not larger than the //last page number if($this->page_num > $this->total_pages){ $this->page_num = $this->total_pages; //set as last page } //make sure page number is not less than one if($this->page_num < 1){ $this->page_num = 1; } } /************************************************************************************** Call this to actually print out the links. The range is the number of links that will be shown around the current page number For example look at these example page numbers, 5 6 [7] 8 9 **************************************************************************************/ function print_links($range){ $range = intval($range); //make sure there are more than one pages //otherwise doing all of this is pretty redundant if($this->total_pages > 1){ //NOTE: you should probably wrap all of these links //inside your own div class so that you can style them $x = $this->page_num - $range; $y = ($this->page_num + $range) + 1; $q = $this->query_string; for ($x; $x < $y; $x++) { if(($x > 0) && ($x <= $this->total_pages)){ if($x == $this->page_num){ echo ' <a href="#" class="page_selected">'.$x.'</a> '; } else{ echo ' <a href="'.$this->self.$q.'page='.$x.'" title="Page '.$x.'"> '.$x.' </a> '; } } } } } /****************************************************************************************************** This simply prints a Back link. i.e. a link to the previous page. If no prev page exists, it wont show. The html parameter you give is basically what shows up inside the link. So, if you simply just want to have the link showing as PREV, give html as "PREV". Or whatever. ******************************************************************************************************/ function print_back_link($html){ if($this->page_num != 1){ $prev = $this->page_num - 1; $q = $this->query_string; echo ' <a href="'.$this->self.$q.'page='.$prev.'" title = "Page '.$prev.'">'.$html.'</a> '; } } //Does the same as the back link except it prints the next link function print_next_link($html){ if($this->page_num != $this->total_pages){ $next = $this->page_num + 1; $q = $this->query_string; echo ' <a href="'.$this->self.$q.'page='.$next.'" title = "Page '.$next.'">'.$html.'</a> '; } } //Just incase you want to show how many pages there are function get_total_pages(){ return $this->total_pages; } //Just incase you need to know what the current page number is - i.e. don't rely on $_GET parameters function get_current_page_num(){ return $this->page_num; } /************************************************************************************************* If you have other $_GET values besides the page value, please include them by using this function. Say you have a $_GET value id=23, then you would provide it to this class like so: $pagination_obj->set_query_string("id=23&"); ITS IMPORTANT THAT YOU DO NOT FORGET THE LAST & *************************************************************************************************/ function set_query_string($string){ $this->query_string = $string; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/159403-ear-bashing-time/ 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.