You are here:  » RSS random

Support Forum



RSS random

Submitted by suffolk66 on Sat, 2012-02-11 08:29 in

Hi David

Like many people who use PT, my sites have thousands of products and so I've been thinking about the best way to use an RSS feed to promote them. My feeds are currently based on this post www.pricetapestry.com/node/4443, and are limited to 20 products so that the feed size isn't too large.

I was wondering how difficult it would be to create a feed that is randomised with products and posting-frequency in order for it to simulate a 'real' RSS news feed. It would update every nth hours with x product, taking the product description and details from the database. n and x are random figures.

Submitted by support on Sat, 2012-02-11 10:51

Hi,

There's a nice way to generate random products that change daily by using the date as the seed parameter to MySQL's RAND() function. To try this, take the line where $sql is generated:

$sql = "SELECT DISTINCT(name),normalised_name FROM `".$config_databaseTablePrefix."products` LIMIT 10";

...and REPLACE with:

$seed = strtotime(date("Y-m-d",time()));
$sql = "SELECT DISTINCT(name),normalised_name FROM `".$config_databaseTablePrefix."products` ORDER BY RAND(".$seed.") LIMIT 10";

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by suffolk66 on Sun, 2012-02-12 08:21

Thanks. I've changed the code so that the feed displays rather than prompts for download but now I only get the title for each product - there's no description or image. Here's the code:

{code saved}

Submitted by support on Sun, 2012-02-12 08:37

Hi,

The SQL in that particular example will need to select *, so instead use:

  $sql = "SELECT DISTINCT(name),* FROM `".$config_databaseTablePrefix."products` ORDER BY RAND(".$seed.") LIMIT 20";

Cheers,
David.
--
PriceTapestry.com

Submitted by suffolk66 on Mon, 2012-02-13 09:09

Not getting any joy with the amended code. Only the top few lines of the RSS feed display - none of the actual feed title, url, description, etc, are displaying even when I change the display limit to 1.

Submitted by support on Mon, 2012-02-13 10:43

Hi,

Have a go with the following; i've checked this out on my test server and corrected a couple of variable inconsistencies; and also included date() in the calculation of the random seed value - apologies this was missed off previously...

   set_time_limit(0);
   require("includes/common.php");
   header("Content-Type: application/octet-stream");
   $baseHREF = "http://".$_SERVER["HTTP_HOST"].$config_baseHREF;
   print "<?xml version='1.0' ?>\n";
   print "<rss version='2.0'>\n";
   print "<channel>\n";
   print "<title>".$config_title."</title>\n";
   print "<link>".$baseHREF."</link>\n";
   $link = mysql_connect($config_databaseServer,$config_databaseUsername,$config_databasePassword);
   mysql_select_db($config_databaseName,$link);
   $seed = strtotime(date("Y-m-d",time()));
   $sql = "SELECT *,MIN(price) AS minPrice FROM `".$config_databaseTablePrefix."products` GROUP BY name ORDER BY RAND(".$seed.") LIMIT 10";
   $result = mysql_unbuffered_query($sql,$link);
   while($row = mysql_fetch_array($result,MYSQL_ASSOC))
   {
     if ($config_useRewrite)
     {
       $link = $baseHREF . "product/".tapestry_hyphenate($row["normalised_name"]).".html";
     }
     else
     {
       $link = $baseHREF . "products.php?q=".urlencode($row["normalised_name"]);
     }
     print "<item>\n";
     print "<title><![CDATA[".$row["name"]."]]></title>\n";
     print "<link><![CDATA[".$link."]]></link>\n";
     print "<description><![CDATA[
".($row["image_url"]?"<img src='".$row["image_url"]."' />":"")."
".($row["description"]?"<p>".$row["description"]."</p>":"")."
"."<p>".$config_currencyHTML.$row["minPrice"]."</p>"."
]]></description>\n";
     print "</item>\n";
   }
   print "</channel>";
   print "</rss>";
?>

Cheers,
David.
--
PriceTapestry.com

Submitted by suffolk66 on Mon, 2012-02-13 10:51

Perfect, David. Thanks for your help - as always.

Submitted by suffolk66 on Tue, 2012-02-14 07:42

Could you please help me with changing the code so that the RSS updates every 30 minutes. I've looked around for some PHP code help for this line but I can't find anything that I'm happy with:

$seed = strtotime(date("Y-m-d",time()));

Submitted by support on Tue, 2012-02-14 13:48

Hi,

time() on its own returns an integer number of seconds, so for a new seed value every 30 minutes, have a go with:

$seed = intval(time()/1800);

Cheers,
David.
--
PriceTapestry.com