In one of my current projects I have a dropdown (select) form element. I populate the options using an array (PHP):
$regions=array(
1=>'North West',
2=>'North East',
3=>'South West',
4=>'South East',
);
(array key=option value, array value=option text)
Now this offers me the convenience of being able to re-order the options as I wish.
The problem is now my client wants to be able to add/modify/re-order options from an admin interface. This means I now need to put these options in to a database table.
What that also means is that there is no easy way (not that I’m aware of) of putting the records in a custom order.
Let’s suppose I create a regions table that includes a ‘sort_order’ field. Does anybody know of any solutions that will allow the client to re-order the records from an interface using simple up/down buttons?
The only way to represent ordering in a relational database is by designating a field that defines the order, as you suggested with the
sort_orderfield.In your admin interface, you could have your up/down buttons trigger a JavaScript function (assuming a web application) which would iterate through the newly ordered regions and assign an incrementing integer to each one. This integer can be assigned to a hidden field for each region, and posted with the full form data when changes are saved.
Upon posting the form, you can simply insert/update the records in the database, with the
sort_orderfield as it was calculated on the client-side.Getting an ordered result set from a database is usually quite straightforward: