function get_products() {
$query = query(" SELECT * FROM products");
confirm($query);
$rows = mysqli_num_rows($query); // Get total of mumber of rows from the database
if(isset($_GET['page'])){ //get page from URL if its there
$page = preg_replace('#[^0-9]#', '', $_GET['page']);//filter everything but numbers
} else{// If the page url variable is not present force it to be number 1
$page = 1;
}
$perPage = 6; // Items per page here
$lastPage = ceil($rows / $perPage); // Get the value of the last page
// Be sure URL variable $page(page number) is no lower than page 1 and no higher than $lastpage
if($page < 1){ // If it is less than 1
$page = 1; // force if to be 1
}elseif($page > $lastPage){ // if it is greater than $lastpage
$page = $lastPage; // force it to be $lastpage's value
}
$middleNumbers = ''; // Initialize this variable
// This creates the numbers to click in between the next and back buttons
$sub1 = $page - 1;
$sub2 = $page - 2;
$add1 = $page + 1;
$add2 = $page + 2;
if($page == 1){
$middleNumbers .= '
' .$page. '';
$middleNumbers .= '' .$add1. '';
} elseif ($page == $lastPage) {
$middleNumbers .= '' .$sub1. '';
$middleNumbers .= '' .$page. '';
}elseif ($page > 2 && $page < ($lastPage -1)) {
$middleNumbers .= '' .$sub2. '';
$middleNumbers .= '' .$sub1. '';
$middleNumbers .= '' .$page. '';
$middleNumbers .= '' .$add1. '';
$middleNumbers .= '' .$add2. '';
} elseif($page > 1 && $page < $lastPage){
$middleNumbers .= '' .$sub1. '';
$middleNumbers .= '' .$page. '';
$middleNumbers .= '' .$add1. '';
}
// This line sets the "LIMIT" range... the 2 values we place to choose a range of rows from database in our query
$limit = 'LIMIT ' . ($page-1) * $perPage . ',' . $perPage;
// $query2 is what we will use to to display products with out $limit variable
$query2 = query(" SELECT * FROM products $limit");
confirm($query2);
$outputPagination = ""; // Initialize the pagination output variable
// if($lastPage != 1){
// echo "Page $page of $lastPage";
// }
// If we are not on page one we place the back link
if($page != 1){
$prev = $page - 1;
$outputPagination .='Back';
}
// Lets append all our links to this variable that we can use this output pagination
$outputPagination .= $middleNumbers;
// If we are not on the very last page we the place the next link
if($page != $lastPage){
$next = $page + 1;
$outputPagination .='Next';
}
// Doen with pagination
// Remember we use query 2 below :)
while($row = fetch_array($query2)) {
$product_image = display_image($row['product_image']);
$product = <<
DELIMETER;
echo $product;
}
echo "";
}