Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can anyone provide advice with my pagination php code?

Posted on 2012-03-15
1
Medium Priority
?
250 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
[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
1 Comment
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 37724233
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

719 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