Solved

Can anyone provide advice with my pagination php code?

Posted on 2012-03-15
1
224 Views
Last Modified: 2012-06-27
Hey guys, I am creating a way to display products on a page with page numbering, I am aiming that each page will contain the next amount of products. For testing purposes, I have 14 products in my table and I have set the items per page to 1 so that generates 14 pages, but when I click on a different page it just always displays the same product. Here is my code:

if($main == go){

			
			if (isset($_GET["page"])) { 
			
			$page  = $_GET["page"]; 
			
			} 
			else { 
			$page=1; 
			$_GET["page"] = 1; 
			} 
			$start_from = ($page-1) * $pageitems; 
			
			
			$pageitems = 1;
			
							//SQL to calculate amount of pages need for product listing
				$sql2 = "SELECT COUNT(productID) AS 'rowcount' FROM products";
				$stmt2 = sqlsrv_query( $conn, $sql2,array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
				if(!$stmt2) 
				{
					die( print_r( sqlsrv_errors(), true));
				}	while( $row2 = sqlsrv_fetch_array( $stmt2)){$total_records = $row2['rowcount'];}

				$total_pages = ceil($total_records / $pageitems);

						
			
			
			$sql = "SELECT productID, productName, category, range, price, picture, description, ROW_NUMBER() OVER(ORDER BY category) AS rownumber FROM products "; 


			//Check SQL Query		
			$stmt = sqlsrv_query( $conn, $sql,array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
			if(!$stmt) 
			{
				die( print_r( sqlsrv_errors(), true));
			}

				while( $row = sqlsrv_fetch_array( $stmt)) 
										{							
								$rownumber = $row['rownumber'];
								$end_at = $pageitems + $start_from;
								
								if (($rownumber>$start_from)&&($rownumber<=$end_at))
								{
								
								echo '<p class="productsContent">'.$row['productName'];
								echo '<br/>'.$row['category'];
								echo '<br/>'.$row['range'];
								echo '<br/>&pound;'.$row['price'];
								echo '<br/><br/><a href="products.php?productID='.$row['productID'].'"><img  src="' . $row['picture'] .'" /></a>';
								echo '<br/><br/></p>';
											}
								
							}	

		
						for ($i=1; $i<=$total_pages; $i++) { 
						echo "<a href='products.php?main=go&page=".$i."'>".$i."</a> "; 
						} 
						
			}
			

Open in new window


I have almost finished this, but just cant figure out the last part, I feel this would have been easier using the LIMIT function, but this is not supported in SQL Server.

If anyone can help out that would be great.


Thanks!
0
Comment
Question by:deucalion0
1 Comment
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
The canonical article about pagination is from SitePoint.  Although it is almost five years old it remains one of the most clearly written examples.  Comes complete with a class you can install and extend!
http://www.sitepoint.com/perfect-php-pagination/

HTH, ~Ray
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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

8 Experts available now in Live!

Get 1:1 Help Now