Random products in RSSFeatured
Hi,
I show random products on the featured section of my frontpage (http://www.pricetapestry.com/node/2276).
I also use rssFeatured.php to show the featured products (http://www.pricetapestry.com/node/1595) on other website. But because I don't configure it manually in the admin area it is empty.
My questions are:
a. How can I change rssFeatured.php to show 'random featured products'?
b. Can I combine the fields name and price into the title of the RSS items? e.g. Name=iPod, Price = 100 USD, Title RSS= iPod 100 USD
Hi,
Thanks, it worked but then I also changed something to get the images and the euro currency. But I don't get the images in the feed.
I changed
$sql = "SELECT name,description,price FROM `".$config_databaseTablePrefix."products` WHERE name IN (".$sqlIn.") GROUP BY name";
to
$sql = "SELECT name,description,price,image_url FROM `".$config_databaseTablePrefix."products` WHERE name IN (".$sqlIn.") GROUP BY name";
and replaced
print "<title>".$row["name"]." ".$row["price"]." USD</title>";
to
print "<title>".$row["name"]." ".$row["price"]." EUR</title>";
and added
print "<enclosure src='".$row["image_url"]."' type='image' />";
What is missing?
Hi Marco,
I'm not familiar with the ENCLOSURE tag - have you tried the standard IMG tag, e.g.:
print "<imgsrc='".$row["image_url"]."' />";If that doesn't help, check the HTML that is being generated (View > Source menu in your browser) and look for the IMG or ENCLOSURE HTML code and study the value of the src attribute. If it is empty, it could be the the random selection is choosing a product that doesn't have an image. You could rectify this by only SELECTing products that do have one, for example by changing the first SQL statement to:
$sql = "SELECT name FROM `".$config_databaseTablePrefix."products` WHERE image_url <> '' ORDER BY RAND() LIMIT 3";Cheers,
David.
Hi David,
Tried the standard IMG tag but got this error message:
This page contains the following errors:
error on line 1 at column 512: error parsing attribute name
Below is a rendering of the page up to the first error.
I looked into the soruce and it seems that there is a image:
<rss><channel><item><title>Hotel Club Val Cenis 405.00 EUR</title><link>http://aanbiedingen.ski-vakanties.eu/product/Hotel-Club-Val-Cenis.html</link><description>In Hotel Club Val Cenis hoeft u zich geen moment te vervelen. Dit hotel is zeer geschikt voor gezinnen met kinderen. Er wordt zovel overdag als 's avonds diverse activiteiten georganiseerd voor jong en oud. Daarnaast biedt het hotel u diverse faciliteiten, waaronder een zwembad en een sauna.</description><imgsrc='http://affiliates.jiba.nl/data/cache/dbimages/L1E4233A67W300H0.jpg
' /></item>Hi Marco,
There was a typo in my post, there should be a space between img and src, try:
print "<img src='".$row["image_url"]."' />";You may find that in order for the image to be displayed by browsers you need to move the IMG tag inside the DESCRIPTION tag - let me know if you're not sure about that...
Cheers,
David.
If you could give a hint on the IMG tag?
I now have this:
print "<title>".$row["name"]." ".$row["price"]." EUR</title>";
print "<link>http://www.mywebsite.com".$config_baseHREF.$href."</link>";
print "<description>".$row["description"]."</description>";
print "<img src='".$row["image_url"]."' />";
print "</item>";Hi Marco,
I would do this:
print "<title>".$row["name"]." ".$row["price"]." EUR</title>";
print "<link>http://www.mywebsite.com".$config_baseHREF.$href."</link>";
print "<description><![CDATA[";
print $row["description"];
print "<img src='".$row["image_url"]."' />";
print "]]></description>";
print "</item>";This will add CDATA tags to the description element, so that you can then use the img HTML without breaking the XML, and then also embeds the IMG tag within the description. This is how images are normally represented within an RSS feed (which doesn't have an image tag / type in its definition)...
Cheers,
David.
Hi David,
Got the images in there. Thanks.
I want to use the feed as input for a feedburner feed.
But I get the message:
The URL does not appear to reference a valid XML file.
This feed does not validate.
line 1, column 38: Missing rss attribute: version [help]
<?xml version='1.0' encoding='UTF-8'?><rss><channel><item><title>Alpenhotel ...
^
line 2, column 170: IRI found where URL expected: http://aanbiedingen.ski-vakanties.eu/product/Appartementen-Königsleiten.html (4 occurrences) [help]
... /product/Appartementen-Königsleiten.html</link><description><![CDATA[Dez ...
^
line 11, column 30: Missing channel element: description [help]
' />]]></description></item></channel></rss>
^
line 11, column 30: Missing channel element: link [help]
' />]]></description></item></channel></rss>
^
line 11, column 30: Missing channel element: title [help]
' />]]></description></item></channel></rss>
^
In addition, interoperability with the widest range of feed readers could be improved by implementing the following recommendations.
Your feed appears to be encoded as "UTF-8", but your server is reporting "US-ASCII" [help]
line 2, column 23: item should contain a guid element (10 occurrences) [help]
' />]]></description></item><item><title>Appartementen Königsleiten 280.00 ...
^
line 11, column 30: Missing atom:link with rel="self" [help]
' />]]></description></item></channel></rss>Hi Marco,
Could you post your existing script (removing URLs if you like) and I'll add the code to resolve each of those warnings for you...
Cheers,
David.
<?php
require("includes/common.php");
// ** header("Content-Disposition: attachment; filename=featured.xml");** //
header("Content-Type: text/xml");
print "<?xml version='1.0' encoding='UTF-8'?>";
print "<rss>";
print "<channel>";
$sql = "SELECT name FROM `".$config_databaseTablePrefix."products` ORDER BY RAND() LIMIT 10";
if (database_querySelect($sql,$rows))
{
$sqlNames = array();
foreach($rows as $featured)
{
$sqlNames[] = "'".$featured["name"]."'";
}
$sqlIn = implode(",",$sqlNames);
// ** $sql = "SELECT name,description FROM `".$config_databaseTablePrefix."products` WHERE name IN (".$sqlIn.") GROUP BY name";** //
$sql = "SELECT name,description,price,image_url FROM `".$config_databaseTablePrefix."products` WHERE name IN (".$sqlIn.") GROUP BY name";
database_querySelect($sql,$rows);
foreach($rows as $row)
{
print "<item>";
if ($config_useRewrite)
{
$href = "product/".tapestry_hyphenate($row["name"]).".html";
}
else
{
$href = "products.php?q=".urlencode($row["name"]);
}
// ** print "<title>".$row["name"]."</title>";** //
print "<title>".$row["name"]." ".$row["price"]." EUR</title>";
print "<link>http://www.mywebsite.com".$config_baseHREF.$href."</link>";
print "<description><![CDATA[";
print $row["description"];
print "<img src='".$row["image_url"]."' />";
print "]]></description>";
print "</item>";
}
}
print "</channel>";
print "</rss>";
?>Hi Marco,
This should resolve all issues mentioned in the report...don't forget to change each example.com back to your website URL..!
<?php
require("includes/common.php");
// ** header("Content-Type: text/xml; charset=utf-8"); ** //
// ** header("Content-Disposition: attachment; filename=featured.xml");** //
header("Content-Type: text/xml");
print "<?xml version='1.0' encoding='UTF-8'?>";
print "<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'>";
print "<channel>";
print "<title>RSS Product Export</title>";
print "<link>http://www.example.com/</link>";
print "<description>RSS Product Export</description>";
print "<atom:link href='http://www.example.com/rss.php' rel='self' type='application/rss+xml' />";
$sql = "SELECT name FROM `".$config_databaseTablePrefix."products` ORDER BY RAND() LIMIT 10";
if (database_querySelect($sql,$rows))
{
$sqlNames = array();
foreach($rows as $featured)
{
$sqlNames[] = "'".$featured["name"]."'";
}
$sqlIn = implode(",",$sqlNames);
/* $sql = "SELECT name,description FROM `".$config_databaseTablePrefix."products` WHERE name IN (".$sqlIn.") GROUP BY name"; */
$sql = "SELECT name,description,price,image_url FROM `".$config_databaseTablePrefix."products` WHERE name IN (".$sqlIn.") GROUP BY name";
database_querySelect($sql,$rows);
foreach($rows as $row)
{
print "<item>";
if ($config_useRewrite)
{
$href = "product/".tapestry_hyphenate($row["name"]).".html";
}
else
{
$href = "products.php?q=".urlencode($row["name"]);
}
/* print "<title>".$row["name"]."</title>";*/
print "<title>".$row["name"]." ".$row["price"]." EUR</title>";
print "<link>http://www.example.com".$config_baseHREF.$href."</link>";
print "<guid>http://www.example.com".$config_baseHREF.$href."</guid>";
print "<description><![CDATA[";
print $row["description"];
print "<img src='".$row["image_url"]."' />";
print "]]></description>";
print "</item>";
}
}
print "</channel>";
print "</rss>";
?>(nb: I changed the comment style slightly so that it works across line breaks)
Cheers,
David.
Hi David,
I noticed
print "<img border='0' width='150' src='".$row["image_url"]."' />";gives this as result:
<img border='0' width='150' src='http://www.mywebsite.com/image.jpg ' />How can I loose the space between .jpg and the ' sign?
So that I have the following as a result?
<img border='0' width='150' src='http://www.mywebsite.com/image.jpg' />Hi Marco,
That must be how the image URL appears in the feed. The ideal solution would be to add a Trim filter to the Image URL field for the feed contain the product where the extra space appeared. Alternatively, it is also straight forward to fix in code; simply use:
print "<img border='0' width='150' src='".trim($row["image_url"])."' />";Cheers,
David.
Hi David,
Thanks it works for me and it gets through the Feedburner validation.
Hi David
The rss code works great on the root directory. But if I try try it on a sub directory the original rss page works fine but feedburner puts the sub file in twice.Where it should be thenfljunkie.com/detroitlions/product feedburner has thenfljunkie.com/detroitlions/detroitlion/product.
Is there a fix to this problem?
Thank You
Dean
Hi Dean,
Could you email me a link to the feed and also your RSS script as an attachment and I'll check it out for you...
Cheers,
David.
Hi Marco,
> a. How can I change rssFeatured.php to show 'random featured products'?
This is a very similar change to the random featured products change. In your rssFeatured.php, look for the following line:
$sql = "SELECT * FROM `".$config_databaseTablePrefix."featured`";...and REPLACE with:
$sql = "SELECT name FROM `".$config_databaseTablePrefix."products` ORDER BY RAND() LIMIT 3";(changing the LIMIT value for more or less products as required)
> b. Can I combine the fields name and price into the title of the RSS items?
> e.g. Name=iPod, Price = 100 USD, Title RSS= iPod 100 USD
Currently only the name and description fields are being SELECTed, so firstly, look for the following code in rssFeatured.php:
$sql = "SELECT name,description FROM `".$config_databaseTablePrefix."products` WHERE name IN (".$sqlIn.") GROUP BY name";...and REPLACE with:
$sql = "SELECT name,description,price FROM `".$config_databaseTablePrefix."products` WHERE name IN (".$sqlIn.") GROUP BY name";And then look for the line where the title is generated:
print "<title>".$row["name"]."</title>";...and REPLACE with:
print "<title>".$row["name"]." ".$row["price"]." USD</title>";Hope this helps!
Cheers,
David.