deucalion0
asked on
Can anyone provide advice with my pagination php code?
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:
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!
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/>£'.$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> ";
}
}
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.