Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Random product display order in catigories. 
 
girls-obsession
Jr. Member
 
Avatar
Total Posts:  19
Joined:  2010-06-06
 

I want the products that are displayed in each of my categories to be presented in a random order to keep a fresh view on each visit.  I have this working by running the following SQL query across my database to create a random position for each of the products.

UPDATE catalog_category_product_index
SET position = (RAND() * 1000)

That works great when I run it manually from myPHPadmin and the resulting PHP code it produces is

$sql = “UPDATE catalog_category_product_index\n”
. “SET position = (RAND() * 1000)”;

My problem is that every attempt to fire it from my Cron Job has failed.  I have tried adding the PHP code to my cron.php file but that does nothing.  Witch of the codes should I use and where should I put it to get it to function????

Cheers!!

 
Magento Community Magento Community
Magento Community
Magento Community
 
girls-obsession
Jr. Member
 
Avatar
Total Posts:  19
Joined:  2010-06-06
 

Ok here is what I am trying to use in my php file.

<?php

$db['host'] = 'myhostname';
$db['name'] = 'myhostname_mydatabasename';
$db['user'] = 'myhostname_myusername';
$db['pass'] = 'mypassword';

$sql = "UPDATE catalog_category_product_index\n"
. "SET position = (RAND() * 1000)";

?>

This is saved in a php called randomizer.php in my root directory.  The Cron calls and executes it without errors but the update of the database fails.

 
Magento Community Magento Community
Magento Community
Magento Community
 
girls-obsession
Jr. Member
 
Avatar
Total Posts:  19
Joined:  2010-06-06
 

Ok if anyone is interested I got it working!!

I have a php file called randomizer.php in my root directory that contains the following.

########################################################################################

<?php

if (!$link = mysql_connect('localhost', 'myusername', 'mypasword')) {
echo 'Could not connect to localhost';
exit;
}

if (!mysql_select_db('mydatabase', $link)) {
echo 'Could not select database database';
exit;
}

$sql = "UPDATE catalog_category_product_index\n"
. "SET position = (RAND() * 1000)";

$result = mysql_query($sql, $link);

if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}

?>

########################################################################################

Obviously change the red bits to suit.  That is fired by a cron job every hour and replaces the position value for each product to a random number.  It is probably sufficient to run this only once a day to freshen up your store.

The cron is simply

php /full/path_to/public_html/your_site.com/randomizer.php

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top