You are here:  » Reviews Pagination...

Support Forum



Reviews Pagination...

Submitted by paul30 on Wed, 2009-02-11 20:43 in

Hello, I searched the forums, but I can not seem to find anything about it, so here we go:

What is the easiest way to include pagination to reviews? I currently just limited the reviews to 30 latest, but having pagination is the ultimate solution.

Thanks a lot
Paul

Submitted by support on Thu, 2009-02-12 09:39

Hi Paul,

It's actually relatively easy to add simple pagination with a "More Reviews..." link. I just tried this on my test server. Firstly, in reviews.php, look for the following code on line 6:

$q = (isset($_GET["q"])?tapestry_normalise($_GET["q"],":\."):"");

...and ADD the following code on the next line.

$page = (isset($_GET["page"])?intval($_GET["page"]):1);

Then, look for the SQL to select reviews around line 69:

$sql = "SELECT * FROM `".$config_databaseTablePrefix."reviews` WHERE product_name = '".database_safe($q)."' AND approved <> '0' ORDER BY created LIMIT ".(($page-1)*$config_resultsPerPage).",".$config_resultsPerPage;

...and REPLACE this line with the following block of code:

      $sql = "SELECT COUNT(*) AS numReviews FROM `".$config_databaseTablePrefix."reviews` WHERE product_name = '".database_safe($q)."' AND approved <> '0'";
      if (database_querySelect($sql,$rows)) $numReviews = $rows[0]["numReviews"];
      $sql = "SELECT * FROM `".$config_databaseTablePrefix."reviews` WHERE product_name = '".database_safe($q)."' AND approved <> '0' ORDER BY created LIMIT ".(($page-1)*$config_resultsPerPage).",".$config_resultsPerPage;

Finally, in html/ratings.php, look for the following code on line 23:

<?php endif; ?>

...and ADD the following code on the next line:

<?php
  
if (($page*$config_resultsPerPage) < $numReviews)
  {
    print 
"<p><a href='?page=".($page+1)."'>More Reviews...</a></p>";
  }
?>

...and that should do the trick!

Cheers,
David.

Submitted by paul30 on Sun, 2009-02-15 03:43

That worked great! - Thank you very much!

I have another small issue (hopefully the last on my site!!)

I get a ton of reviews and the reviews table will (eventually) get pretty large. Now since some products are going out of stock, I thought it is a good idea to delete the reviews for the products that are no longer present.

I tried to create a loop (that will be run after all imports) to check if product name in reviews table corresponds to a product in products table and if no match found, DELETE FROM reviews WHERE... would be given...

I tried something along the lines of:

$sql = "SELECT reviews.product_name, products.buy_url FROM `".$config_databaseTablePrefix."reviews` LEFT JOIN products ON reviews.product_name = products.name";
       $result = mysql_unbuffered_query($sql,$link);
        while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
            if ($row['name'] == '') {
                $sql2 = "DELETE FROM reviews WHERE product_name = '".$row['product_name']."'";
            }
}

But that deleted all of the reviews from DB :)

Do you have any idea how to do it easier and more effective than my attempt at it?

Thanks a lot
Paul

Submitted by support on Mon, 2009-02-16 01:44

Hi Paul,

I would use something like:

$sql = "SELECT DISTINCT product_name FROM `".$config_databaseTablePrefix."reviews`"
if (database_querySelect($sql,$reviews))
{
  foreach($reviews as $review)
  {
    $sql = "SELECT id FROM `".$config_databaseTablePrefix."products` WHERE name='".database_safe($review["product_name"])."' LIMIT 1";
    if (!database_querySelect($sql,$products))
    {
      $sql = "DELETE FROM `".$config_databaseTablePrefix."reviews` WHERE product_name='".database_safe($review["product_name"])."'";
      database_queryModify($sql,$result);
    }
  }
}

Cheers,
David.