I have made a PHP function that pulls data from a table (product_reviews) in my database, showing all reviews for a unique product_id. Here is the code:
function showReviews ($product_id)
{
include('database_conn.php');
$query = "SELECT * FROM product_reviews WHERE product_reviews.productID='".$product_id."'";
if ($queryresult = mysqli_query($conn, $query))
{
while ($currentrow = mysqli_fetch_assoc($queryresult))
{
$result_list[] = $currentrow;
}
foreach ($result_list as $currentrow)
{
$productitems[] = array(
$customer_forename = $currentrow['customer_forename'],
$customer_surname = $currentrow['customer_surname'],
$review_title = $currentrow['review_title'],
$review_text = $currentrow['review_text']);
echo '<article class="Review_Box">';
echo "<h3>".$review_title." by ".$customer_forename." ".$customer_surname."</h3></br>";
echo "<p>".$review_text."</p>";
echo "</article>";
}
}
}
This function is then called from the products page and it works as intended.
But when I add pagination to the function ( following an ehow tutorial), the function doesn't have any output(see here).
if(!function_exists('showReviews'))
{
function showReviews ($product_id)
{
include('database_conn.php');
include('functions.php');
$rowsPerPage = 3;
$currentPage = ((isset($_GET['page']) && $_GET['page'] > 0) ? (int)$_GET['page'] : 1);
$offset = ($currentPage-1)*$rowsPerPage;
$query = "SELECT * FROM product_reviews WHERE product_reviews.productID='".$product_id."' LIMIT '".$offset."','".$rowsPerPage."'";
if ($queryresult = mysqli_query($conn, $query))
{
while ($currentrow = mysqli_fetch_assoc($queryresult))
{
$result_list[] = $currentrow;
}
foreach ($result_list as $currentrow)
{
$productitems[] = array(
$customer_forename = $currentrow['customer_forename'],
$customer_surname = $currentrow['customer_surname'],
$review_title = $currentrow['review_title'],
$review_text = $currentrow['review_text']);
echo '<article class="Review_Box">';
echo "<h3>".$review_title." by ".$customer_forename." ".$customer_surname."</h3></br>";
echo "<p>".$review_text."</p>";
echo "</article>";
}
}
$count = countReviews (1);
$totalPages = $count/$rowsPerPage;
if($currentPage > 1)
{
echo '<a href="SingleProductPage.php?id='.$product_id.'&page=' . ($currentPage-1) . '#Customer_Reviews">Previous Page</a> ';
}
if($currentPage < $totalPages)
{
echo '<a href="SingleProductPage.php?id='.$product_id.'&page=' . ($currentPage+1) . '#Customer_Reviews">Next Page</a>';
}
}
}
I tested my sql query and it works fine in mysql Workbench. What am I doing wrong? Can anyone recommend a better way to do this?