snrecords Posted November 15, 2008 Share Posted November 15, 2008 Hi! My pagination won't work for pages 2,3, and so forth. It only works on page 1. Please help! Here's my code session_start(); include("dbclass/database.php"); $myClass = new database(); $count = 0; if(isset($_POST['cboIsland'])) { $island = $_POST['cboIsland']; } if(isset($_POST['cbocuisine'])) { $cuisine = $_POST['cbocuisine']; } if(isset($_POST['txtKeyword'])) { $Keyword = $_POST['txtKeyword']; } if($island == '') { $island = 'Select'; $island = $island; } if ($cuisine == '') { $cuisine = 'Select'; $cuisine = $cuisine; } include('ps_pagination.php'); if ($island != 'Select') { $condition .= " where rest_neighborhoods_info.rest_island_info_id = $island"; $icount = 1; } if ($cuisine!='Select' && $island == 'Select') { $condition .= " where rest_has_cuisines.rest_cuisines_info_id = $cuisine"; $count = 1; //----------------- $query_select_cuisine = "select * from cuisine_page_details where rest_cuisines_info_id=$cuisine and cui_type_page=1"; $result_select_cuisine = $myClass->query($query_select_cuisine); while($row_cuisine=$myClass->fetch_array($result_select_cuisine)) { $page_title = $row_cuisine['page_title']; $meta_description = $row_cuisine['meta_description']; $meta_keyword = $row_cuisine['meta_keyword']; } //----------------- } else if($cuisine!='Select' && $island != 'Select') { $count = 1; $condition .= " and rest_has_cuisines.rest_cuisines_info_id = $cuisine"; $query_select_cuisine = "select * from cuisine_page_details where rest_cuisines_info_id=$cuisine and cui_type_page=".$island; $result_select_cuisine = $myClass->query($query_select_cuisine); while($row_cuisine=$myClass->fetch_array($result_select_cuisine)) { $page_title = $row_cuisine['page_title']; $meta_description = $row_cuisine['meta_description']; $meta_keyword = $row_cuisine['meta_keyword']; } } if ($Keyword!='' && $cuisine=='Select' && $island == 'Select') { $condition .= " where restaurent_info_details.rest_name like '%$Keyword%'"; } else { $condition .= " and restaurent_info_details.rest_name like '%$Keyword%'"; } $query_select = "SELECT distinct restaurent_info_details.rest_name, restaurent_info_details.rest_page_title, restaurent_info_details.rest_address, restaurent_info_details.rest_city, "; $query_select .= " restaurent_info_details.rest_zipcode, restaurent_info_details.rest_phone, rest_pricerange.pricerange, "; $query_select .= " rest_neighborhoods_info.neighborhood_name, restaurent_info_details.restaurent_info_details_id, "; $query_select .= " rest_neighborhoods_info.rest_neighborhoods_info_id FROM restaurent_info_details Left Join "; $query_select .= " rest_has_neighborhoods ON restaurent_info_details.restaurent_info_details_id = "; $query_select .= " rest_has_neighborhoods.restaurent_info_details_id Left Join rest_neighborhoods_info ON "; $query_select .= " rest_has_neighborhoods.rest_neighborhoods_info_id = rest_neighborhoods_info.rest_neighborhoods_info_id "; $query_select .= " Left Join rest_pricerange ON restaurent_info_details.rest_pricerangeid = rest_pricerange.rest_pricerangeid "; $query_select .= " Left Join rest_has_cuisines ON restaurent_info_details.restaurent_info_details_id = "; $query_select .= " rest_has_cuisines.restaurent_info_details_id"; $query_select .= $condition." order by restaurent_info_details.rest_name"; $pager = new PS_Pagination($myClass,$query_select,10,10); Now here's my pagination code that I got from a tutorial: class PS_Pagination { var $php_self; var $rows_per_page; //Number of records to display per page var $total_rows; //Total number of rows returned by the query var $links_per_page; //Number of links to display per page var $sql; var $debug = false; var $conn; var $page; var $max_pages; var $offset; /** * Constructor * * @param resource $connection Mysql connection link * @param string $sql SQL query to paginate. Example : SELECT * FROM users * @param integer $rows_per_page Number of records to display per page. Defaults to 10 * @param integer $links_per_page Number of links to display per page. Defaults to 5 */ function PS_Pagination($connection, $sql, $rows_per_page = 10, $links_per_page = 5) { $this->conn = $connection; $this->sql = $sql; $this->rows_per_page = $rows_per_page; $this->links_per_page = $links_per_page; $this->php_self = htmlspecialchars($_SERVER['PHP_SELF']); if(isset($_GET['page'])) { $this->page = intval($_GET['page']); } } /** * Executes the SQL query and initializes internal variables * * @access public * @return resource */ function paginate() { if(!$this->conn) { if($this->debug) echo "MySQL connection missing<br />"; return false; } $all_rs = @mysql_query($this->sql); if(!$all_rs) { if($this->debug) echo "SQL query failed. Check your query.<br />"; return false; } $this->total_rows = mysql_num_rows($all_rs); @mysql_close($all_rs); $this->max_pages = ceil($this->total_rows/$this->rows_per_page); //Check the page value just in case someone is trying to input an aribitrary value if($this->page > $this->max_pages || $this->page <= 0) { $this->page = 1; } //Calculate Offset $this->offset = $this->rows_per_page * ($this->page-1); //Fetch the required result set $rs = @mysql_query($this->sql." LIMIT {$this->offset}, {$this->rows_per_page}"); if(!$rs) { if($this->debug) echo "Pagination query failed. Check your query.<br />"; return false; } return $rs; } /** * Display the link to the first page * * @access public * @param string $tag Text string to be displayed as the link. Defaults to 'First' * @return string */ function renderFirst($tag=' First ') { if($this->page == 1) { } else { return '<a href="'.$this->php_self.'&page=1">'.$tag.'</a>'; } } /** * Display the link to the last page * * @access public * @param string $tag Text string to be displayed as the link. Defaults to 'Last' * @return string */ function renderLast($tag=' Last ') { if($this->page == $this->max_pages) { } else { return '<a href="'.$this->php_self.'&page='.$this->max_pages.'">'.$tag.'</a>'; } } /** * Display the next link * * @access public * @param string $tag Text string to be displayed as the link. Defaults to '>>' * @return string */ function renderNext($tag=' Next ') { if($this->page < $this->max_pages) { return '<a href="'.$this->php_self.'&page='.($this->page+1).'">'.$tag.'</a>'; } else { } } /** * Display the previous link * * @access public * @param string $tag Text string to be displayed as the link. Defaults to '<<' * @return string */ function renderPrev($tag=' Previous ') { if($this->page > 1) { return '<a href="'.$this->php_self.'&page='.($this->page-1).'">'.$tag.'</a>'; } else { } } /** * Display the page links * * @access public * @return string */ function renderNav() { for($i=1;$i<=$this->max_pages;$i+=$this->links_per_page) { if($this->page >= $i) { $start = $i; } } if($this->max_pages > $this->links_per_page) { $end = $start+$this->links_per_page; if($end > $this->max_pages) $end = $this->max_pages+1; } else { $end = $this->max_pages; } $links = ''; for( $i=$start ; $i<$end ; $i++) { if($i == $this->page) { $links .= " $i "; } else { $links .= ' <a href="'.$this->php_self.'?page='.$i.'">'.$i.'</a> '; } } return $links; } /** * Display full pagination navigation * * @access public * @return string */ function renderFullNav() { return $this->renderFirst().' '.$this->renderPrev().' '.$this->renderNav().' '.$this->renderNext().' '.$this->renderLast(); } /** * Set debug mode * * @access public * @param bool $debug Set to TRUE to enable debug messages * @return void */ function setDebug($debug) { $this->debug = $debug; } } ?> I've tried a bunch of things. I tried changing pagination links so that the url includes the island, cuisine, and keyword. I then tried changing the .htacess file to accommodate the urls. I also tried saving the html form's variables into sessions, but that never worked. I tried to using GET instead of POST, and that still didn't work. I believe it has something to do with $pager = new PS_Pagination($myClass,$query_select,10,10); $query_select is not updating even with using sessions variables (e.g. $_SESSION['cboIsland'] = $_POST['cboIsland'] Please help! Link to comment https://forums.phpfreaks.com/topic/132871-pagination-subsequent-pages-not-working/ Share on other sites More sharing options...
snrecords Posted November 15, 2008 Author Share Posted November 15, 2008 After looking at my problem again ... I know that $condition doesn't update. So I have a form that sends variables through POST and depending on what those variables are, $condition changes. So how do I save $condition in a session? Link to comment https://forums.phpfreaks.com/topic/132871-pagination-subsequent-pages-not-working/#findComment-690999 Share on other sites More sharing options...
Garethp Posted November 15, 2008 Share Posted November 15, 2008 $_SESSION['Condition'] = $Condition; You call it up as $_SESSION['Condition']; Link to comment https://forums.phpfreaks.com/topic/132871-pagination-subsequent-pages-not-working/#findComment-691001 Share on other sites More sharing options...
snrecords Posted November 16, 2008 Author Share Posted November 16, 2008 Hi again. Ok, so i change my code to use sessions now. Everything works when i use it the first time. But because I'm using sessions to store a variable, when I go back to the form page and attempt to use the form again, my if statement prevents the code from running again. How do I reset the session variable when I go back to the form? The code is below. I feel that this code is not the best method for this ... and if you have any suggestions, please feel free to shoot my way. (I'm trying to avoid the GET method, because of ugly SEO urls). session_start(); include("dbclass/database.php"); $myClass = new database(); session_register("condition"); if(!isset($_SESSION['condition'])) { $count = 0; if(isset($_POST['cboIsland'])) { $island = $_POST['cboIsland']; } if(isset($_POST['cbocuisine'])) { $cuisine = $_POST['cbocuisine']; } if(isset($_POST['txtKeyword'])) { $Keyword = $_POST['txtKeyword']; } if($island == '') { $island = 'Select'; $island = $island; } if ($cuisine == '') { $cuisine = 'Select'; $cuisine = $cuisine; } if ($island != 'Select') { $_SESSION['condition'] .= " where rest_neighborhoods_info.rest_island_info_id = $island"; $icount = 1; } if ($cuisine!='Select' && $island == 'Select') { $_SESSION['condition'] .= " where rest_has_cuisines.rest_cuisines_info_id = $cuisine"; $count = 1; //----------------- $query_select_cuisine = "select * from cuisine_page_details where rest_cuisines_info_id=$cuisine and cui_type_page=1"; $result_select_cuisine = $myClass->query($query_select_cuisine); while($row_cuisine=$myClass->fetch_array($result_select_cuisine)) { $page_title = $row_cuisine['page_title']; $meta_description = $row_cuisine['meta_description']; $meta_keyword = $row_cuisine['meta_keyword']; } //----------------- } else if($cuisine!='Select' && $island != 'Select') { $count = 1; $_SESSION['condition'] .= " and rest_has_cuisines.rest_cuisines_info_id = $cuisine"; $query_select_cuisine = "select * from cuisine_page_details where rest_cuisines_info_id=$cuisine and cui_type_page=".$island; $result_select_cuisine = $myClass->query($query_select_cuisine); while($row_cuisine=$myClass->fetch_array($result_select_cuisine)) { $page_title = $row_cuisine['page_title']; $meta_description = $row_cuisine['meta_description']; $meta_keyword = $row_cuisine['meta_keyword']; } } if ($Keyword!='' && $cuisine=='Select' && $island == 'Select') { $_SESSION['condition'] .= " where restaurent_info_details.rest_name like '%$Keyword%'"; } else { $_SESSION['condition'] .= " and restaurent_info_details.rest_name like '%$Keyword%'"; } } include('ps_pagination.php'); $query_select = "SELECT distinct restaurent_info_details.rest_name, restaurent_info_details.rest_page_title, restaurent_info_details.rest_address, restaurent_info_details.rest_city, "; $query_select .= " restaurent_info_details.rest_zipcode, restaurent_info_details.rest_phone, rest_pricerange.pricerange, "; $query_select .= " rest_neighborhoods_info.neighborhood_name, restaurent_info_details.restaurent_info_details_id, "; $query_select .= " rest_neighborhoods_info.rest_neighborhoods_info_id FROM restaurent_info_details Left Join "; $query_select .= " rest_has_neighborhoods ON restaurent_info_details.restaurent_info_details_id = "; $query_select .= " rest_has_neighborhoods.restaurent_info_details_id Left Join rest_neighborhoods_info ON "; $query_select .= " rest_has_neighborhoods.rest_neighborhoods_info_id = rest_neighborhoods_info.rest_neighborhoods_info_id "; $query_select .= " Left Join rest_pricerange ON restaurent_info_details.rest_pricerangeid = rest_pricerange.rest_pricerangeid "; $query_select .= " Left Join rest_has_cuisines ON restaurent_info_details.restaurent_info_details_id = "; $query_select .= " rest_has_cuisines.restaurent_info_details_id"; $query_select .= $_SESSION['condition']." order by restaurent_info_details.rest_name"; echo $_SESSION['condition']; $pager = new PS_Pagination($myClass,$query_select,10,10); and here's the form page code: <?php session_start(); include("dbclass/database.php"); $myClass = new database(); $query_select = "select * from rest_island_info order by island_name"; $result_select = $myClass->query($query_select); while ($row=$myClass->fetch_array($result_select)) { $option_island .="<option value='".$row['rest_island_info_id']."'>".$row['island_name']."</option>"; } $query_select = "select * from rest_cuisines_info order by cuis_name"; $result_select = $myClass->query($query_select); while ($row=$myClass->fetch_array($result_select)) { $option_cuisine .="<option value='".$row['rest_cuisines_info_id']."'>".$row['cuis_name']."</option>"; } ?> <html> <head> </head> <body> <form name="frmResult" action="results.php" method="POST"> <select class="topdropdown" name="cboIsland" id="cboIsland"> <option value="Select">-- Select All --</option> <?php echo $option_island; ?> </select> Islands <p> <select class="topdropdown" name="cbocuisine" id="cbocuisine"> <option value="Select">-- Select All --</option> <!--<option>American</option><option>Chinese</option><option>Italian</option><option>Japanese</option><option>Mexican</option><option>Vietnamese</option>--> <?php echo $option_cuisine; ?> </select> Cuisine </p> <p><input name="txtKeyword" type="text" id="txtKeyword"> Keyword(s)</p> <input name="btnSearch" type="submit" id="btnSearch"> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/132871-pagination-subsequent-pages-not-working/#findComment-691042 Share on other sites More sharing options...
Garethp Posted November 16, 2008 Share Posted November 16, 2008 unset($_SESSION['Condition']); Link to comment https://forums.phpfreaks.com/topic/132871-pagination-subsequent-pages-not-working/#findComment-691046 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.