I am working on paginating my MySQL results to 10 per page. So far, I have been able the count how many results there are in the column, count how many pages there will be if there are 10 results/page, display the number of pages and also add links to each page (ex: Page1, Page2, Page 3, Page 4...)
Currently, all 38 results are showing on one page. When I click the page number, I am taken to the correct link but the content with the 38 results is the same. I know I need to divide the 38 results into the four pages. This is where I'm stuck at:
To find the number of pages, I have variable:
$pages = ceil($items_number / $per_page )
However, on multiple tutorials I've seen:
$pages = ceil(mysql_results($items_number,0) / $per_page )
which allows the data count to start at 0. When I tried this, I get an error
Fatal error: Call to undefined function mysql_results()
So I don't have the option to start counting results from 0 (using mysql_result
) and break them into 10 per page.
How can I get around the mysql_result
and be able to show the appropriate data/number of items for each page number?
Here's a link, clickable page numbers are on top: http://test.ishabagha.com/classic_cars/pag_test.php
Code:
<?php
require_once("./includes/database_connection.php");
error_reporting(E_ALL);
ini_set('display_errors', 1);
$page = "";
$per_page = 10;
$query = "SELECT productCode, productName, productLine, productScale, productVendor, productDescription, buyPrice FROM products WHERE `productLine` = 'Classic Cars'";
$result = mysqli_query($dbc, $query)
or die(mysqli_error($dbc));
$query_count = "SELECT count(productLine) FROM products WHERE productLine = 'Classic Cars'";
$items = mysqli_query($dbc, $query_count)
or die(mysqli_error($dbc));
$row = mysqli_fetch_row($items);
$items_number = $row[0];
$pages = ceil($items_number / $per_page )
?>
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Home</title>
<link type="text/css" rel="stylesheet" href="classic_cars.css" />
</head>
<body>
<?php
require_once("./includes/navigation.php");
?>
<?php
for($number = 1; $number <= $pages; $number++) {
echo "<a href='?page=$number'>$number</a>";
}
while ($row = mysqli_fetch_array($result)) {
$product_code = $row['productCode'];
$product_name = $row['productName'];
$product_line = $row['productLine'];
$product_vendor = $row['productVendor'];
$product_description = $row['productDescription'];
$buy_price = $row['buyPrice'];
echo "<tr>
<td><p>$product_name</p></td>
</tr>";
} // end while ($row = mysqli_fetch_array($result))
?>
<?php
require_once("./includes/footer.php");
?>
</body>
</html>