Solved

PHP Pagination wont work with menu keys

Posted on 2010-08-23
12
432 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
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
 

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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 33504899
Sounds like there may be a bad rewrite rule.  Can you please post your .htaccess file?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:corporateKeenan
ID: 33505214
@Ray

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

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 142

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

930 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now