You are here:  » Random posts on homepage sometimes not showing

Support Forum



Random posts on homepage sometimes not showing

Submitted by marco@flapper on Tue, 2010-11-02 00:06 in

Hi, I have a website {link saved}

I have random articles on the homepage but I noticed that sometimes (one out of 3/5 times)there are no articles displayed. You can check this by pressing f5.

My index.php code is:

<?php
  
require("includes/common.php");
  
$header["meta"]["description"] = "";
  
$header["meta"]["keywords"] = "";
  require(
"html/header.php");
  require(
"html/searchform.php");
  
?>

<?php
  
print javascript_focus("search.q");
  
$sql "SELECT name,1 AS sequence FROM `".$config_databaseTablePrefix."products` ORDER BY RAND() LIMIT 12";
  if (
database_querySelect($sql,$rows))
  {
    
$sqlNames = array();
    foreach(
$rows as $featured)
    {
      
$sqlNames[] = "'".$featured["name"]."'";
    }
    
$sqlIn implode(",",$sqlNames);
    
$sql "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants FROM `".$config_databaseTablePrefix."products` WHERE name IN (".$sqlIn.") GROUP BY name";
    
database_querySelect($sql,$rows);
    
$featured["products"] = $rows;
    foreach(
$featured["products"] as $k => $product)
    {
      if (
$config_useRewrite)
      {
        
$featured["products"][$k]["productHREF"] = "product/".tapestry_hyphenate($product["name"]).".html";
        
$featured["products"][$k]["reviewHREF"] = "review/".tapestry_hyphenate($product["name"]).".html";
      }
      else
      {
        
$featured["products"][$k]["productHREF"] = "products.php?q=".urlencode($product["name"]);
        
$featured["products"][$k]["reviewHREF"] = "reviews.php?q=".urlencode($product["name"]);
      }
    }
  }
  if (isset(
$featured)) require("html/featured.php");
  require(
"html/footer.php");
?>

Submitted by support on Tue, 2010-11-02 09:29

Hi Marco,

I see that you are running the latest distribution of the script, however
the random modification is based on the original version; and uses name
not normalised_name, so occasionally it would be possible for none, or
less than 12 products to be selected.

Please try REPLACEing the section you posted with the following:

<?php
  
print javascript_focus("search.q");
  
$sql "SELECT name,1 AS sequence FROM `".$config_databaseTablePrefix."products` ORDER BY RAND() LIMIT 12";
  if (
database_querySelect($sql,$rows))
  {
    
$sqlNames = array();
    foreach(
$rows as $featured)
    {
      
$featured["name"] = tapestry_normalise($featured["name"]);
      
$sqlNames[] = "'".$featured["name"]."'";
    }
    
$sqlIn implode(",",$sqlNames);
    
$sql "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants FROM `".$config_databaseTablePrefix."products` WHERE name IN (".$sqlIn.") GROUP BY name";
    
database_querySelect($sql,$rows);
    
$featured["products"] = $rows;
    foreach(
$featured["products"] as $k => $product)
    {
      if (
$config_useRewrite)
      {
        
$featured["products"][$k]["productHREF"] = "product/".tapestry_hyphenate($product["normalised_name"]).".html";
        
$featured["products"][$k]["reviewHREF"] = "review/".tapestry_hyphenate($product["normalised_name"]).".html";
      }
      else
      {
        
$featured["products"][$k]["productHREF"] = "products.php?q=".urlencode($product["normalised_name"]);
        
$featured["products"][$k]["reviewHREF"] = "reviews.php?q=".urlencode($product["normalised_name"]);
      }
    }
  }
  if (isset(
$featured)) require("html/featured.php");
  require(
"html/footer.php");
?>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by marco@flapper on Tue, 2010-11-02 13:28

It helps alot.

Thanks.

Submitted by marco@flapper on Fri, 2010-11-05 08:47

Hi,
I'm experimenting with it.
I tried to display one or two categories on the homepage instead of random.

Found this sql code:
$sql = "SELECT name,1 AS SEQUENCE FROM `".$config_databaseTablePrefix."products` WHERE category='lego' LIMIT 12";

But don't know what to do with it with regard to my index.php

Could you give me a hint?

Submitted by support on Fri, 2010-11-05 10:25

Hi Marco,

Simply use that line in place of:

  $sql = "SELECT * FROM `".$config_databaseTablePrefix."featured` ORDER BY sequence";

...in your index.php - that's all!

Cheers,
David.
--
PriceTapestry.com

Submitted by marco@flapper on Fri, 2010-11-05 19:31

I did that but it still displays products from other categories. Strange?

Submitted by support on Sat, 2010-11-06 13:52

Hi Marco,

If you're using the code on a page that is already displaying Featured Products they may be getting combined. Just before your new block to select the "lego" category, add:

  unset($featured);

That should be all it is...

Cheers,
David.
--
PriceTapestry.com

Submitted by marco@flapper on Sat, 2010-12-04 15:07

Hi,
Tried the random selection with Price tapestry 12/10Beta but this only returns a few products instead of the configured 12. Should the code be different for 12/10?

You can check it out here:
{link saved}

Submitted by support on Sat, 2010-12-04 16:35

Hi Marco,

The same code should work just fine - is there sufficient variation in the products import for there to be 12 unique product names selected? It is possible that if there are lots of matched products the first query may select the same product name more than once, in which case it would reduce the total number of featured products being displayed. To stop this happening, a DISTINCT clause can be added to the SQL - have a go with:

  $sql = "SELECT DISTINCT(name) AS name,1 AS sequence FROM `".$config_databaseTablePrefix."products` ORDER BY RAND() LIMIT 12";

Cheers,
David.
--
PriceTapestry.com

Submitted by marco@flapper on Sat, 2010-12-04 23:36

Tried the distinct clause but it is still not showing the 12 products.

There should be enough products.

Submitted by support on Sun, 2010-12-05 09:39

Hi Marco,

Could you email me your modified index.php and html/featured.php and I'll check out for you on my test server against the bet version...

Cheers,
David.
--
PriceTapestry.com