You are here:  » Reviews on home page

Active Forum Topics


Reviews on home page

Submitted by Perce2 on Tue, 2019-01-29 11:31 in

Hello David,

On a single PT install (latest edition), I'm trying to place product reviews on the index.php homepage. Adding the code in user_footer_before.php

I've tried adding the code from one of my multiple installs but struggling to get the reviews to show. Can you please give an example of what I should have for this to work?

Thanks,
Graham

Submitted by support on Tue, 2019-01-29 11:59

Hi Graham,

Here's a PHP/HTML example to use in html/user_footer_before.php based on html/ratings.php to show latest reviews in the same format, with product name and link above each review:

<?php if (strpos($_SERVER["PHP_SELF"],"index.php")!==FALSE): ?>
<?php
  $sql = "SELECT * FROM `".$config_databaseTablePrefix."reviews` WHERE approved <> 0 ORDER BY created LIMIT 2";
  if (database_querySelect($sql,$reviews))
  {
    foreach($reviews as $k => $review)
    {
      $sql = "SELECT * FROM `".$config_databaseTablePrefix."products` WHERE normalised_name='".database_safe($review["product_name"])."'";
      if (database_querySelect($sql,$products))
      {
        $reviews[$k]["product"] = $products[0];
      }
    }
  }
?>
<?php if (count($reviews)): ?>
<div class='row pt_ra'>
  <div class='small-12 columns'>
    <h3>Latest Reviews</h3>
    <?php foreach($reviews as $review): ?>
      <h5><a href='<?php print tapestry_productHREF($review["product"]); ?>'><?php print $review["product"]["name"]; ?></a></h5>
      <blockquote>
        <?php if ($review["comments"]): ?>
          <?php print htmlspecialchars($review["comments"],ENT_QUOTES,$config_charset); ?>
        <?php else: ?>
          <em><?php print translate("This reviewer did not leave any comments."); ?></em>
        <?php endif; ?>
        <br />
        <?php print tapestry_stars($review["rating"],""); ?>
      </blockquote>
    <?php endforeach; ?>
  </div>
</div>
<?php endif; ?>
<?php endif; ?>

Simply change the LIMIT value in the $sql at line 3 to control how many reviews are displayed. Within the loop in the main HTML section, if you wish to display any other product information use $review["product"] just as you would $product elsewhere for example within the loop in the price comparison table in html/prices.php

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by Perce2 on Tue, 2019-01-29 13:04

Hello David,
thanks very much for that speedy reply.

Using your code, I can see reviews now with their ratings, but I don't see their respective links above.

If it helps, this was the code used on a multiple install which displays product links.

<?php
      $sql 
"SELECT * FROM `%TABLE%` WHERE approved > 0 ORDER BY created DESC LIMIT 3";
      foreach(
$sites as $site => $directory)
      {
        
$installPath $directory."/";
        require(
$installPath."config.php");
        require(
$installPath."config.advanced.php");
        
$sqlReviews str_replace("%TABLE%",$config_databaseTablePrefix."reviews",$sql);
        if (
database_querySelect($sqlReviews,$rows))
        {
          foreach(
$rows as $review)
          {
            print 
"<p>";
            if (
$config_useRewrite)
            {
              
$href $config_baseHREF."product/".tapestry_hyphenate($review["product_name"]).".html";
            }
            else
            {
              
$href $config_baseHREF."products.php?q=".urlencode($review["product_name"]);
            }
            print 
"<a href='".$href."'>".$review["product_name"]."</a>";
            print 
"<br />";
            
$breakLimit 200;
            if (
strlen($review["comments"]) > $breakLimit)
            {
              
// find the first space after the limit
              
$breakOffset strpos($review["comments"]," ",$breakLimit);
              
// if found, crop the description and add "..."
              
if ($breakOffset !== false)
              {
                
$review["comments"] = substr($review["comments"],0,$breakOffset)."...";
              }
            }
            print 
"<em>\"".$review["comments"]."\"</em>";
            print 
"</p>";
          }
        }
      }
    
?>

Many thanks,
Graham

Submitted by Perce2 on Tue, 2019-01-29 13:18

Hello David,
forget my last email, it works perfectly...
Somehow my database has had an issue and there are no url's held in table. Added a test review and the product url displays fine. Great work.

Many thanks,
Graham

Submitted by support on Tue, 2019-01-29 13:23

No worries!

If you'd prefer the layout you were using on your multi-site install, the standalone equivalent would be as follows;

<?php if (strpos($_SERVER["PHP_SELF"],"index.php")!==FALSE): ?>
<div class='row'>
  <div class='small-12 columns'>
    <?php
      $sql = "SELECT * FROM `".$config_databaseTablePrefix."reviews` WHERE approved <> 0 ORDER BY created LIMIT 2";
      if (database_querySelect($sql,$reviews))
      {
        foreach($reviews as $review)
        {
          print "<p>";
          if ($config_useRewrite)
          {
            $href = $config_baseHREF."product/".tapestry_hyphenate($review["product_name"]).".html";
          }
          else
          {
            $href = $config_baseHREF."products.php?q=".urlencode($review["product_name"]);
          }
          print "<a href='".$href."'>".$review["product_name"]."</a>";
          print "<br />";
          $breakLimit = 200;
          if (strlen($review["comments"]) > $breakLimit)
          {
            // find the first space after the limit
            $breakOffset = strpos($review["comments"]," ",$breakLimit);
            // if found, crop the description and add "..."
            if ($breakOffset !== false)
            {
              $review["comments"] = substr($review["comments"],0,$breakOffset)."...";
            }
          }
          print "<em>\"".$review["comments"]."\"</em>";
          print "</p>";
        }
      }
    ?>
  </div>
</div>
<?php endif; ?>

Cheers,
David.
--
PriceTapestry.com

Submitted by Perce2 on Mon, 2019-02-04 05:57

Hello David,
I prefer your first code version and have it in place right now.

The only issue I'm having with it, when the database updates, if the product is now not available the product link disappears leaving just it's attached comment.
Using the previous code, all would be shown, but if you clicked on the link you would be taken to the product page where it would indicate the product now not available and a list of related products (if available).

Is that still possible?

Thanks.

Submitted by support on Mon, 2019-02-04 09:37

Hi Graham,

Sure - in the code look for the opening line of the main loop:

 <?php foreach($reviews as $review): ?>

...and REPLACE with:

<?php foreach($reviews as $review): ?>
  <?php
    if (!isset($review["product"]["name"]))
    {
      $review["product"]["name"] = $review["product_name"];
      $review["product"]["normalised_name"] = tapestry_normalise($review["product_name"]);
    }
  ?>

Cheers,
David.
--
PriceTapestry.com