10 before 2

Soldato
Joined
18 Oct 2002
Posts
5,464
Location
London Town
For reference, you might want to check the type of the number field as Adz suggested. Won't make a massive difference, but should offer some speed optimisation over using natsort() instead of MySQL's native sorting.

It appears that your numbers coming out of mysql are being treated as strings. If it's not an integer type (smallint, int etc.) then ORDER BY won't sort it numerically - change the field type to INT or such, and try ORDER BY again :).
 
Soldato
Joined
12 Dec 2006
Posts
3,421
Location
Worthing, West Sussex
Yeah, you're ID column defo shouldn't be a string! Make sure it's an integer number. Also, I think the best you can do with a string is a full-text search (correct me if I'm wrong, I've never tried to put anything else on a string).

But with an Integer number, you can set it as a unique key which will speed up searching through the database for things when using the ID field :) Then you can also set it to auto_increment and not need to worry about checking whether the key exists before adding it to the table :cool:
 
Soldato
Joined
12 Dec 2006
Posts
3,421
Location
Worthing, West Sussex
Oh, right :D

I know you got a solution now but I'm going to ramble on anyway :p :D

You could have 'name' and 'occurance' in the table, so Sausage in the name field an 7 in the occurance field. Or when you put it into the database, you could use php to format it so it's sausage0007 which would allow for 9999 occurances of 'sausage', and the 0's would keep them in the correct order.

Doing one of those would mean you won't need to do any phpness, could keep all the ordering in the mysql query :p But it would also mean changing the way your database works, and seeings you already have a solution there's probably not much point...

But yeah, could be handy knowledge for future reference :)
 
Back
Top Bottom