PHP Developers Network
http://forums.devnetwork.net/

How do I randomly display categories in a particular order
http://forums.devnetwork.net/viewtopic.php?f=2&t=142813
Page 1 of 1

Author:  adsegzy [ Wed Aug 03, 2016 6:05 pm ]
Post subject:  How do I randomly display categories in a particular order

Hello there,

I have an online store,where the members can subscribe to any of the 3 packages (1, 2 & 3). Below is a sample of the Products DB table.


id package product status
1 3 Biro 1
2 1 Paper 1
3 3 Pin 0
4 2 Eraser 1
5 2 Blade 1
6 3 Bag 1
7 2 Pen 0
8 1 Sharpener 1
9 1 Shoe 1
10 2 Chair 1

I displayed the package 3 products first, then package 2 products next then package 1 products last like this;
Syntax: [ Download ] [ Hide ]
$get_products = mysql_query("SELECT * FROM products WHERE status='1' ORDER BY package DESC");


But what i want to really do is to randomly display all package 3 products first, then randomly display package 2 products and lastly display package 1 products randomly. So that no particular item will always be at the top while some will always be on the bottom.

How do I write the syntax. Thanks in advance

Author:  Celauran [ Wed Aug 03, 2016 8:49 pm ]
Post subject:  Re: How do I randomly display categories in a particular ord

You could add RAND() to your select and then sort by that

Author:  Celauran [ Wed Aug 03, 2016 8:50 pm ]
Post subject:  Re: How do I randomly display categories in a particular ord

Syntax: [ Download ] [ Hide ]
SELECT *, RAND() AS shuffle
FROM products
WHERE STATUS='1'
ORDER BY package DESC, shuffle

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/