Jump to content

Php-mysql Pagination


sridaran
 Share

Recommended Posts

Hi I am using pagination concept to display records from mysql database page by page using php. I am getting records on the first page but when clicking next button its displaying blank page without displaying remaining records. Can any one help me to get a solution. Here is my code:<?phpinclude('ps_pagination.php');$con = mysql_connect("localhost","root","admin");$d1 = mysql_select_db("mercant",$con);$sql = "select * from emps";$pager = new PS_Pagination($con,$sql,10,3);$rs = $pager->paginate();echo '<table width="" border="1" cellspacing="0" cellpadding="0">';while($row=mysql_fetch_assoc($rs)){echo '<tr>';echo '<td align="center">'.$row['URL'].'</td>';echo '<td align="center">'.$row['Ad_Feed_1'].'</td>';echo '<td align="center">'.$row['Ad_Feed_2'].'</td>';echo '</tr>';}echo $pager->renderFullNav();echo '</table>';?>************************************************************************************************ps_pagination.php<?phpclass PS_Pagination { var $php_self; var $rows_per_page; var $total_rows; var $links_per_page; var $sql; var $debug = false; var $conn; var $page; var $max_pages; var $offset; 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']); } } 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); if($this->page > $this->max_pages || $this->page <= 0) { $this->page = 1; } $this->offset = $this->rows_per_page * ($this->page-1); $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; } function renderFirst($tag='First') { if($this->page == 1) { return $tag; } else { return '<a href="'.$this->php_self.'?page=1">'.$tag.'</a>'; } } 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>'; } } function renderNext($tag=' >>') { if($this->page < $this->max_pages) { return '<a href="'.$this->php_self.'?page='.($this->page+1).'">'.$tag.'</a>'; } else { return $tag; } } function renderPrev($tag='<<') { if($this->page > 1) { return '<a href="'.$this->php_self.'?page='.($this->page-1).'">'.$tag.'</a>'; } else { return $tag; } } 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; } function renderFullNav() { return $this->renderFirst().' '.$this->renderPrev().' '.$this->renderNav().' '.$this->renderNext().' '.$this->renderLast(); } function setDebug($debug) { $this->debug = $debug; }}?>

Link to comment
Share on other sites

  • 2 months later...

Is it better to do pagination through AJAX or just have a page refresh with a URL variable telling the page? Which technique is more popular? Im creating a site that has student reviews, and im not sure which way is best. AJAX looks cooler and more web 2.0 ish, people cant exactly share links for reviews past page 1. Any thoughts?

Link to comment
Share on other sites

If you want to use AJAX, develop it purely with PHP first, and then add the AJAX functionality. Or else search engines won't see anything but the first page.

Link to comment
Share on other sites

You can always use a querystring variable to tell it which page to load on startup, and have a link button people can click to show the link that will get them back to that page.
I could do that, but say a user clicks to page 4 through ajax. If they do a page refresh, it would go back to page 1. So if a user found a review on page 4 and wanted to send someone the link, the person who clicks the link would only see page 1. So i'm not sure which is better... doing pagination without ajax so the page # is in the url, or just by using ajax. what's the most common? what would u recommend?
Link to comment
Share on other sites

You do the pagination without AJAX first, then you do it with AJAX if you like. It is not recommendable to use just AJAX. So you allow the script to load the right page depending on the querystring variable, and have links that use the querystring, but then you hijack those links so that they use AJAX instead.

<a href="?page=2" onclick="load(2)">Page 2</a>

For linking to other pages using AJAX, you can read the hash part of the URL: http://www.aspektas.com/blog/using-the-loc...-hash-with-ajax

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...