0

I have a page on my website which displays product categories in a 3x3 tile layout.

Currently I'm getting the distinct product categories out of the database and looping over the returned array to display the 9 tiles.

This is all working fine but the current order of the tiles is alphabetical. The customer has just come back asking to have them in a very specific order and I'm confusing myself over and over about how to achieve this.

Current Order:

Agri, Bespoke, Dairy, Farm, Forest, General, Grain, One off, Red Meat

Order Wanted:

Dairy, Red Meat, Forest, Grain, General, Farm, Bespoke, One off, Agri

Can someone point me in the right direction about what would be needed to achieve this?

Jamie Gracie
  • 115
  • 1
  • 9
  • 2
    Add a column to your DB with the correct order; then order by that column – WillardSolutions Jul 22 '14 at 20:42
  • I'm using the Silverstripe CMS/Framework so adding a column isn't so straight forward. Also there isn't just a category table so to speak. The product table just has a category column. – Jamie Gracie Jul 22 '14 at 20:49

2 Answers2

0

My understanding is that the database will return data in the order they were added, unless told otherwise.

You could add ORDER BY X to the end of your query to sort them. If no column exists that could give you that order you could add such a column.

Thomas
  • 871
  • 2
  • 8
  • 21
0

I guess if you can't add a new column with the correct sequence, which would really be the preferred method, you could do something like this:

SELECT DISTINCT Category FROM Products
ORDER BY
(Category='Dairy') DESC, (Category='Red Meat') DESC, (Category = 'Forest') DESC

... and so on. It's a really weird way of doing it, but it seems to get job done.

user470714
  • 2,858
  • 1
  • 28
  • 34