Hi David
Recently you made me a rss feed for the whole list that was random and could vary the number of listings. This works great and has been very useful. I now want to be more specific and would like feeds for merchants categories and brands, would this be possible.
Thanks
Brent
hi David
This is the code you gave me for products, what I want is just products from a particular merchant, brand, category if that is possible.
Thanks
Brent
<?php
$baseURL = "http://www.example.com"; // URL of your site without trailing "/"
require("includes/common.php");
header("Content-Type: text/xml");
print "<?xml version='1.0' encoding='UTF-8'?>";
print "<rss version='2.0'>";
print "<channel>";
print "<title>RSS Product Export</title>";
print "<link>".$baseURL.$config_baseHREF."</link>";
print "<description>RSS Product Export</description>";
$sql = "SELECT * FROM `".$config_databaseTablePrefix."products` ORDER BY RAND() LIMIT 20";
if (database_querySelect($sql,$rows))
{
foreach($rows as $product)
{
print "<item>";
if ($config_useRewrite)
{
$href = "product/".tapestry_hyphenate($product["name"]).".html";
}
else
{
$href = "products.php?q=".urlencode($product["name"]);
}
print "<title>".$product["name"]."</title>";
print "<link>".$baseURL.$config_baseHREF.$href."</link>";
print "<guid>".$baseURL.$config_baseHREF.$href."</guid>";
print "<description><![CDATA[";
print "<img src='".$product["image_url"]."' /><br />";
print "<p>".$product["description"]."</p>";
print "]]></description>";
print "</item>";
}
}
print "</channel>";
print "</rss>";
?>
Hi Brent,
Sure; the following script will just show random products if called as normal, but you can now also add the parameters merchant,category or brand to the URL to limit to that selected item, for example:
rss.php?merchant=Acme
rss.php?category=Electronics
rss.php?brand=Sony
(use + in place of spaces in a URL)
<?php
$baseURL = "http://www.example.com"; // URL of your site without trailing "/"
require("includes/common.php");
header("Content-Type: text/xml");
print "<?xml version='1.0' encoding='UTF-8'?>";
print "<rss version='2.0'>";
print "<channel>";
print "<title>RSS Product Export</title>";
print "<link>".$baseURL.$config_baseHREF."</link>";
print "<description>RSS Product Export</description>";
if ($_GET["merchant"]) $where = " WHERE merchant='".database_safe($_GET["merchant"])."' ";
elseif if ($_GET["category"]) $where = " WHERE category='".database_safe($_GET["category"])."' ";
elseif if ($_GET["brand"]) $where = " WHERE brand='".database_safe($_GET["brand"])."' ";
else $where = "";
$sql = "SELECT * FROM `".$config_databaseTablePrefix."products` ".$where." ORDER BY RAND() LIMIT 20";
if (database_querySelect($sql,$rows))
{
foreach($rows as $product)
{
print "<item>";
if ($config_useRewrite)
{
$href = "product/".tapestry_hyphenate($product["name"]).".html";
}
else
{
$href = "products.php?q=".urlencode($product["name"]);
}
print "<title>".$product["name"]."</title>";
print "<link>".$baseURL.$config_baseHREF.$href."</link>";
print "<guid>".$baseURL.$config_baseHREF.$href."</guid>";
print "<description><![CDATA[";
print "<img src='".$product["image_url"]."' /><br />";
print "<p>".$product["description"]."</p>";
print "]]></description>";
print "</item>";
}
}
print "</channel>";
print "</rss>";
?>
Cheers,
David.
Hi
Just come to use this rss and I am getting the following error
Parse error: syntax error, unexpected T_IF, expecting '(' in /home/utoday/public_html/rss.php on line 12
Thanks
Brent
Hi Brent,
Change that block (starting at line 12) to:
if ($_GET["merchant"]) { $where = " WHERE merchant='".database_safe($_GET["merchant"])."' "; }
elseif ($_GET["category"]) { $where = " WHERE category='".database_safe($_GET["category"])."' "; }
elseif ($_GET["brand"]) { $where = " WHERE brand='".database_safe($_GET["brand"])."' "; }
else { $where = ""; }
That should be all it is...
Cheers,
David.
Hi,
I was looking for this functionality but it returns empty when I use it on my PT site. Could you take a look at this?
<?php
$baseURL = "http://www.example.com"; // URL of your site without trailing "/"
require("includes/common.php");
header("Content-Type: text/xml");
print "<?xml version='1.0' encoding='UTF-8'?>";
print "<rss version='2.0'>";
print "<channel>";
print "<title>RSS Product Export</title>";
print "<link>".$baseURL.$config_baseHREF."</link>";
print "<description>RSS Product Export</description>";
if ($_GET["merchant"]) { $where = " WHERE merchant='".database_safe($_GET["merchant"])."' "; }
elseif ($_GET["category"]) { $where = " WHERE category='".database_safe($_GET["category"])."' "; }
elseif ($_GET["brand"]) { $where = " WHERE brand='".database_safe($_GET["brand"])."' "; }
else { $where = ""; }
$sql = "SELECT * FROM `".$config_databaseTablePrefix."products` ".$where." ORDER BY RAND() LIMIT 20";
if (database_querySelect($sql,$rows))
{
foreach($rows as $product)
{
print "<item>";
if ($config_useRewrite)
{
$href = "product/".tapestry_hyphenate($product["name"]).".html";
}
else
{
$href = "products.php?q=".urlencode($product["name"]);
}
print "<title>".$product["name"]."</title>";
print "<link>".$baseURL.$config_baseHREF.$href."</link>";
print "<guid>".$baseURL.$config_baseHREF.$href."</guid>";
print "<description><![CDATA[";
print "<img src='".$product["image_url"]."' /><br />";
print "<p>".$product["description"]."</p>";
print "]]></description>";
print "</item>";
}
}
print "</channel>";
print "</rss>";
?>
Hi Marco,
I checked the script and it worked fine on my test server, and then went to check the example URL you provided and it seems to work intermittently (keep refreshing the page and sometimes it will display the products). The only reason I can see for this would be if MySQL is unable to create the temporary files required to perform the RAND() sort. If you could perhaps try removing the RAND() sort by REPLACEing line 15 of the script with;
$sql = "SELECT * FROM `".$config_databaseTablePrefix."products` ".$where." LIMIT 20";
...and see if that works every time. If it does, let me know and in the mean time I'll have a think what to suggest if that does turn out to be the case...
Cheers,
David.
--
PriceTapestry.com
Hi,
I replaced line 15, but then it doesn't show up even when refreshing it a couple of times.
Hi Marco,
The script is actually sending output but so I checked validation and it is because some product names contain special characters to the title field should be CDATA enclosed as is the description. To do this, look for the following code at line 29:
print "<title>".$product["name"]."</title>";
...and REPLACE that with:
print "<title><![CDATA[".$product["name"]."]]></title>";
Don't forget to re-instate the random ordering by changing line 15 back to:
$sql = "SELECT * FROM `".$config_databaseTablePrefix."products` ".$where." ORDER BY RAND() LIMIT 20";
...and that should be all it is!
Cheers,
David.
--
PriceTapestry.com
Thanks, I still had some problems but also CDATA enclosed the link field and then it worked.
Thanks
Hello Brent,
Sure - could you perhaps post the script that you have so far and I'll base it on that (perhaps adding a "q" parameter) so that you don't require multiple scripts...
Cheers,
David.