You are here:  » Random from Mixed merchants

Support Forum



Random from Mixed merchants

Submitted by babyuniverse on Sun, 2008-02-10 10:01 in

Hi There,

I current have the random function selecting 18 products on my front page, however due to some merchants having 1000's of products and some only having a few, these few seem to always miss out on appearing as random.

Can i alter the code to select 1 random product from each merchant? (limited to 18)

Thanks
Richard

Submitted by support on Sun, 2008-02-10 19:13

Hi Richard,

This can be done using the MySQL's DISTINCT function. Assuming that you now have a line similar to the following:

$sql = "SELECT * FROM `".$config_databaseTablePrefix."products` ORDER BY RAND() LIMIT 18";

...try changing the query as follows:

$sql = "SELECT *,DISTINCT(merchant) AS dmerchant FROM `".$config_databaseTablePrefix."products` ORDER BY RAND() LIMIT 18";

That should do the trick...

Cheers,
David.

Submitted by babyuniverse on Mon, 2008-02-11 10:02

hi,

that line through up the following errors

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/onestopo/public_html/includes/database.php on line 21

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/onestopo/public_html/includes/database.php on line 26

Thanks

Submitted by support on Mon, 2008-02-11 11:13

Hi Richard,

Try this version;

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

Cheers,
David.