Hello
On the homepage of my website I've got the featured products added. (I've got over 20 featured products)
For some strange reason, there are times when 2 or 3 products show instead of the 4.
This leaves a gap ( see magazine subscription just under the picture of the female)
I'm wondering if this can be fixed?
thanks
Mally
I reloaded 4 times and saw 3,3,3,4 featured items. If your code requests 4 results, the only reason I could see it come back with less is if one of those is unavailable as David said.
If this is the case, it's a new product feed that's required. If it's not, you could always request 6 results and display the first 4. OK, not ideal but it is a feasible workaround.
On a side note, I like what you've done with the site :-)
Hi Mally,
Indeed, this is probably due to a missing product in your products table. I had the same problems which were making my sites look messy so I added some code to check the products being displayed in the featured.php code had a match in the products table. If they didn't I deleted the product from the featured table. Like this:
$ftNames[] = $featured["name"];
foreach($ftNames as $valuesrc)
{
$match = 0;
foreach($featured["products"] as $product)
{
if (strcasecmp(trim($valuesrc), trim($product["name"])) == 0)
{
$match = 1;
}
}
if ($match == 0)
{
$sql = "DELETE FROM `".$config_databaseTablePrefix."featured` WHERE name='".$valuesrc."'";
database_queryModify($sql,$insertId);
}
}
Probably not the most efficient of code but it does the job. The first time there is a missing product the page will still show 3 out of 4 or whatever, but at the same time the code will delete that missing product from the featured table so it won't try and display that missing product again.
The only thing to watch out for is to make sure you back up your featured table data as if you're playing around with the feeds and also refreshing the pages that show the featured products, you could inadvertantly delete some products! This could also be a problem if people are accessing your site while you are importing feeds as part of the import process is to delete the records before importing so you may again lose featured products by accident.
So, there's probably a better way to do this but this is how I'm doing it and it does work.
Cheers.
Keeop
mobilewiz
thanks for the advice guys.
Keeop, where should I add that code? which file?
thanks
Mally
Hi Mally,
This is the top half of my new featured.php - I have already put all the code that originally resided in index.php in to this file:
Find this line where you are using the Featured products:
$sql = "SELECT * FROM `".$config_databaseTablePrefix."featured` ORDER BY RAND() LIMIT 3";
And then find these lines further down:
foreach($featured["products"] as $k => $product)
{
if ($config_useRewrite)
Then, just paste this code in between, overwriting what's there already. I'd make copies of any files first just in case!
if (database_querySelect($sql,$rows))
{
$sqlNames = array();
foreach($rows as $featured)
{
$sqlNames[] = "'".$featured["name"]."'";
$ftNames[] = $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;
// New code to remove Featured Prods that have been removed
foreach($ftNames as $valuesrc) //($featured["name"] as $featured)
{
$match = 0;
foreach($featured["products"] as $product)
{
if (strcasecmp(trim($valuesrc), trim($product["name"])) == 0)
// if ($valuesrc == $product["name"])
{
$match = 1;
}
}
if ($match == 0)
{
$sql = "DELETE FROM `".$config_databaseTablePrefix."featured` WHERE name='".$valuesrc."'";
database_queryModify($sql,$insertId);
}
}
// New Code end
Cheers.
Keeop
mobilewiz
The above worked well for me, It turns out the template I have, the code that needed changing (as per above) was in the index file.
Thanks Guys for your help.
Mally
magazine subscriptions
Hi Mally,
I'm guessing you're using a "random" featured products mod - so this could mean that when less than 4 are displayed it has picked products from your featured products list that are no longer in your database. Could that be the case...?
Cheers,
David.