ds111 Posted March 31, 2008 Share Posted March 31, 2008 This is seriously driving me mad. I've rewrote the code AT LEAST 15 times. Here's my current code for pagination. <?php include 'PS_Pagination.php'; $conn = mysql_connect($host,$user,$pass); mysql_select_db($db,$conn); $sql = "select * from restaurants WHERE type='$type'"; //Create a PS_Pagination object $pager = new PS_Pagination($conn,$sql,10,5); // PS_Pagination(connection variable, SQL query variable, Number of Records per page, Number of Pagination links //The paginate() function returns a mysql //result set for the current page $rs = $pager->paginate(); //Loop through the result set while($row = mysql_fetch_assoc($rs)) { $name = $row['name']; //$address = mysql_result($quer, $index, "address"); $phone = $row['phone']; $hours = $row['hours']; $menu = $row['menu']; $type = $row['type']; $addr1 = $row['addr1']; $addr2 = $row['addr2']; $id = $row['id']; ?> <table width="503" align="center" class="result"> <tr> <hr color="C9D7F1"> <td width="61" valign="middle"><img src="restpic.gif" alt="" /></td> <td width="122"><p class="th3"><em class="result_name"><?php echo $name; ?></em><br /> <?php echo "$addr1, $addr2"; ?><br /> <a href="doDirections();">Directions</a></p></td> <td width="114"><p class="th3"><strong>Hours of Operation:<br /> </strong><?php echo $hours; ?></p></td> <td width="137"><p class="th3"> <a href="doDescription();">Description</a><br /> <?php if($menu == "no") { // DO NOTHING } else { ?> <a href="<?php echo $menu; ?>" target="_blank">Menu</a> <br/> <?php } ?><a href="javascript:alert("For Reservations please call $TH_53");">Reservations</a><br /> <a href="doPro(); ?>" target="_blank">Promotions</a></p></td> </tr> </table> <?php } //Display the navigation echo $pager->renderFullNav(); ?> that was search.php here's PS_Pagination.php: <?php 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) { return $tag; } 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) { return $tag; } 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=' >>') { if($this->page < $this->max_pages) { return '<a href="'.$this->php_self.'?page='.($this->page+1).'">'.$tag.'</a>'; } else { return $tag; } } /** * Display the previous link * * @access public * @param string $tag Text string to be displayed as the link. Defaults to '<<' * @return string */ function renderPrev($tag='<<') { if($this->page > 1) { return '<a href="'.$this->php_self.'?page='.($this->page-1).'">'.$tag.'</a>'; } else { return $tag; } } /** * 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; } } ?> So bascially this is the problem: Pagination works fine EXCEPT ONLY ON THE FIRST PAGE. So for example if i click Next i get a blank page and it says "Page 1 of 0". So what is going on? Seriously? I've tried differnet methods, i've tried anything and everything. Please help me! Im begging you! Link to comment https://forums.phpfreaks.com/topic/98730-pagination-question/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.