PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Dec 14, 2017 4:01 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Wed Aug 03, 2016 6:05 pm 
Offline
Forum Contributor

Joined: Tue Jul 28, 2009 9:26 am
Posts: 184
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


Top
 Profile  
 
PostPosted: Wed Aug 03, 2016 8:49 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6402
Location: Montreal, Canada
You could add RAND() to your select and then sort by that

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Aug 03, 2016 8:50 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6402
Location: Montreal, Canada
Syntax: [ Download ] [ Hide ]
SELECT *, RAND() AS shuffle
FROM products
WHERE STATUS='1'
ORDER BY package DESC, shuffle

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group