ch1326 Posted May 23, 2009 Share Posted May 23, 2009 Hi everyone, I am getting with clicks of "next" and "previous". when I click eigther next or previous it does not seem to be working correctly. Please help! Thank u everyone <html> <head> <title>Page</title> <style type = 'text/css'> A:link {font-family:Arial, Helvetica, sans-serif; font-size:10pt; text-decoration:none; color:blue} A:visited {font-family:Arial, Helvetica, sans-serif; font-size:10pt; text-decoration:none; color:blue} A:hover {font-family:Arial, Helvetica, sans-serif; 10pt; font-size:text-decoration:none; color:red} .small{font-family:Arial, Helvetica, sans-serif; font-size:10pt; text-decoration:none} </style> </head> <body bgcolor = '#F1F1F1'> <p class = 'small'> <?php include "phpConfig.php"; $conn = mysql_connect($ServerName, $UserName, $UserPassword); if (!$conn) { die("Cannot connect to server<BR>"); } mysql_select_db ($DatabaseName, $conn) or die ("$DatabaseName database not found<BR>"); $strSQL = "select * from tbl_page"; $Per_Page = 20; if (!$Page) { $Page = 1; } $Prev_Page = $Page - 1; $Next_Page = $Page + 1; $result = mysql_query($strSQL); $Page_start = ($Per_Page*$Page)- $Per_Page; $Num_Rows = mysql_num_rows($result); if ($Num_Rows<=$Per_Page) { $Num_Pages=1; } elseif ( ($Num_Rows%$Per_Page) == 0 ) { $Num_Pages = ($Num_Rows/$Per_Page); } else { $Num_Pages = ($Num_Rows/$Per_Page)+1; } $Num_Pages = (int) $Num_Pages; if ( ($Page>$Num_Pages) || ($Page<0) ) { print "Number of $Page is more than $Num_Pages"; } $strSQL = "select * from tbl_page limit $Page_start, $Per_Page"; $result = mysql_query($strSQL); while ( $rs = mysql_fetch_array ($result) ) { echo "$rs[iD]<br>\n"; } ?> <p class = 'small' > Total <b><?php echo $Num_Rows;?></b> numbers and <b><?php echo $Num_Pages; ?></b> pages: <?php if (isset ($Prev_Page) ) { echo "<a href='$PHP_SELF?Page=$Prev_Page'><<previous</a>"; } for ($i=1; $i<=$Num_Pages;$i++) { if (!$Page) { echo "[<a href = '$PHP_SELF?Page=$i'>$i</a>]"; } else { echo "<b>[$i]</b>"; } } if ($Page != $Num_Pages) { echo "<a href = '$PHP_SELF?Page=$Next_Page'>Next>></a>"; } mysql_close($conn); ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/159364-solved-next-and-previous-page/ Share on other sites More sharing options...
jkewlo Posted May 23, 2009 Share Posted May 23, 2009 What is it not doing? you said after the page=8 it will not work? Link to comment https://forums.phpfreaks.com/topic/159364-solved-next-and-previous-page/#findComment-840566 Share on other sites More sharing options...
ch1326 Posted May 23, 2009 Author Share Posted May 23, 2009 I have 100 data in tbl_page table. I would like to display 20 data on each page and when I click next, it supposes to display next 20 data. if I click previous, it supposes to go back to the last 20 data. but it does not work. Cheers Link to comment https://forums.phpfreaks.com/topic/159364-solved-next-and-previous-page/#findComment-840576 Share on other sites More sharing options...
MasterACE14 Posted May 23, 2009 Share Posted May 23, 2009 it's always going to show page 1 because you aren't changing the page. use $Page = $_GET['page']; Link to comment https://forums.phpfreaks.com/topic/159364-solved-next-and-previous-page/#findComment-840580 Share on other sites More sharing options...
waynew Posted May 23, 2009 Share Posted May 23, 2009 I use this class that I made. Might be of some help. It works and saves me a LOT of time. <?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; } } ?> Link to comment https://forums.phpfreaks.com/topic/159364-solved-next-and-previous-page/#findComment-840634 Share on other sites More sharing options...
ch1326 Posted May 23, 2009 Author Share Posted May 23, 2009 Thank so much MasterACE14 Problem solved now Link to comment https://forums.phpfreaks.com/topic/159364-solved-next-and-previous-page/#findComment-840635 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.