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?

2
  • 2
    Add a column to your DB with the correct order; then order by that column Commented Jul 22, 2014 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. Commented Jul 22, 2014 at 20:49

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.