• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 448
  • Last Modified:

PHP Pagination wont work with menu keys

I have an issue. My pagination works correct with the alone paginate_customers.php page. It skips along each page without issue. Now since I have added the paginate_customers.php to my index page as a "/index.php?menukey=10" when I go to page 2 of the paginate_customers.php page it does not go, it just refreshes the page to page 1 of the paginate_customers.php page. How can I fix this?
I will add the code below of the ps_pagination. php and the paginate_customers.php page that is displayed on the website. Any suggestions will help, thanks!

<?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
		$res = @mysql_query($this->sql." LIMIT {$this->offset}, {$this->rows_per_page}");
		if(!$res) {
			if($this->debug) echo "Pagination query failed. Check your query.<br />";
			return false;
		}
		return $res;
	}
	
	/**
	 * 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=' &gt;&gt;') {
		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='&lt;&lt;') {
		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.'?menukey=10?page='.$i.'">'.$i.'</a> ';
			}
		}
		
		return $links;
	}
	
	/**
	 * Display full pagination navigation
	 *
	 * @access public
	 * @return string
	 */
	function renderFullNav() {
		return $this->renderFirst().'&nbsp;'.$this->renderPrev().'&nbsp;'.$this->renderNav().'&nbsp;'.$this->renderNext().'&nbsp;'.$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;
	}
}
?>


/********************paginate_customers.php*************/

<!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">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta name="keywords" content="" />
	<meta name="description" content="" />
	<!-- <link href="default.css" rel="stylesheet" type="text/css" /> -->
</head>
<body>

<?php
    //Include the PS_Pagination class
	include('ps_pagination.php');
	require_once ('mysqli_connect.php');
	
	
	
	//Connect to mysql db
	$conn = mysql_connect('localhost', 'corporatekeenan', 'eeee');
	mysql_select_db('okok',$conn);
	$sql = 'SELECT * FROM customers ORDER by CustomerID';

	
    //Create a PS_Pagination object
	$countperpage = 25;
	$pager = new PS_Pagination($conn,$sql,$countperpage,7);
	
    //The paginate() function returns a mysql result set 
	$res = $pager->paginate();
	echo '<table align="center" cellspacing="0" cellpadding="0" border=1>
		<tr>
			<td align="left" colspan="13"><h1>Customers:</h1></td>
		</tr>
		<tr>
			<td>CustomerID</td>
			<td>OldCustomerID</td>
			<td>Customer First</td>
			<td>Customer Middle</td>
			<td>Customer Last</td>
			<td>Username</td>
			<td>Car</td>
			<td>Car Color</td>
			<td>Computer</td>
			<td>Laptop (yes/no)</td>
			<td>Race</td>
			<td>Residence</td>
			<td>Birth Month</td>
		</tr>';

		while($row = mysql_fetch_assoc($res)) {
			echo '<tr><td>' . $row['CustomerID'] . '</td><td>' . $row['OldCustomerID'] . '</td><td>' . $row['FirstName'] . '</td><td>' . $row['MiddleName'] . '</td><td>' .  $row['LastName'] . '</td><td>' . $row['Username'] . '&nbsp;</td><td>' . $row['CarID'] . '</td><td>' . $row['CarColorID'] . '</td><td>' . $row['ComputerID'] . '</td><td>' . $row['IsLaptop'] . '</td><td>' . $row['RaceID'] . '</td><td>' . $row['ResidenceID'] . '</td><td>' . $row['BirthMonthID'] . '</td></tr>';
		   }
			echo '</table>';

	
    //Display the full navigation in one go
	echo "<br><center><font face=verdana size=3 color=green>";
	echo $pager->renderFullNav();
	echo "</font></center>";

	mysql_free_result($res);
	mysql_close($conn);



/*	Or you can display the inidividual links... */
	   // echo $pager->renderFirst();			//Display the link to first page: First
	    //echo $pager->renderPrev();			//Display the link to previous page: <<
	    //echo $pager->renderNav();			//Display page links: 1 2 3
	    //echo $pager->renderNext();			//Display the link to next page: >>
	    //echo $pager->renderLast();			//Display the link to last page: Last


?>

</body>
</html>

Open in new window

0
corporateKeenan
Asked:
corporateKeenan
1 Solution
 
corporateKeenanAuthor Commented:
This is what it currently looks like on the address bar "http://......./.../index.php?menukey=10?page=2" This is what keeps returning the "http://......./.../index.php?menukey=10" page. Any thoughts?
0
 
corporateKeenanAuthor Commented:
I had to change the addresses as above it was screwing up my links on the original paginate_customers.php page.
0
 
Lukasz ChmielewskiCommented:
what happens when you go to page = 3 ?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
corporateKeenanAuthor Commented:
@Roads_Roads

The page will stay at the landing page for ?menukey=10. If I remove "?menukey=10?page='..." from the ps_pagination.php page and just leave the '?page="..' instead of the "?menukey=10?page='..." the links value changes to 'index.php?page=3" and it displays nothing more than the "index.php?" page it doesn't recognize that I am requesting the menukey=10 page and from that page the 3rd page from the database table.
0
 
Ray PaseurCommented:
Sounds like there may be a bad rewrite rule.  Can you please post your .htaccess file?
0
 
corporateKeenanAuthor Commented:
@Ray

Unfortunately I don't have access to the root
0
 
Ray PaseurCommented:
Quel fromage.

I'll never get through 275 lines of code, but you might find some help in this tutorial.  It's the canonical example of correct pagination.
http://articles.sitepoint.com/article/perfect-php-pagination

Best of luck with it, ~Ray
0
 
corporateKeenanAuthor Commented:
I dont think you all understand, the pagination actually works on the paginate_customers.php perfectly fine. But when it is called from a menukey option and displayed into a primarycontent area(where the sidebar and menu stay fixed but pages display within the middle frame) the pagination doesn't work.
0
 
corporateKeenanAuthor Commented:
I'm trying to use a query string to pass two variables, the menukey=10 and the ?page=' ' function.
0
 
corporateKeenanAuthor Commented:
I found the solution. its was just one funky ampersand sign " /index.php?menukey=10&page=' ' "
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now