?
Solved

PHP Pagination wont work with menu keys

Posted on 2010-08-23
12
Medium Priority
?
443 Views
Last Modified: 2013-12-13
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
Comment
Question by:corporateKeenan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 

Author Comment

by:corporateKeenan
ID: 33504046
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
 

Author Comment

by:corporateKeenan
ID: 33504450
I had to change the addresses as above it was screwing up my links on the original paginate_customers.php page.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 33504716
what happens when you go to page = 3 ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:corporateKeenan
ID: 33504768
@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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 33504899
Sounds like there may be a bad rewrite rule.  Can you please post your .htaccess file?
0
 

Author Comment

by:corporateKeenan
ID: 33505214
@Ray

Unfortunately I don't have access to the root
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 33505262
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
 

Author Comment

by:corporateKeenan
ID: 33506917
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
 

Author Comment

by:corporateKeenan
ID: 33507022
I'm trying to use a query string to pass two variables, the menukey=10 and the ?page=' ' function.
0
 

Author Comment

by:corporateKeenan
ID: 33507207
I found the solution. its was just one funky ampersand sign " /index.php?menukey=10&page=' ' "
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36045899
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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question