I have a mysql database and some php that allows you to create an entry in the database, update an entry, and view the entries as a web page or xml. What I want to do is add a function to move an entry in the database up or down by one row, or, send to the top of the database or bottom.
I’ve seen some online comments about doing this type of thing that suggested doing a dynamic sort when displaying the page, but I’m looking for a persistent resort. I’ve seen one approach suggested that would be to have a separate “sort” field in the database that is agnostic of the actual database sort key, but I’m not sure why that would be better than actually re-ordering the database
Here is a dump of the table structure:
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `hlnManager`
--
-- --------------------------------------------------------
--
-- Table structure for table `hln_stations`
--
CREATE TABLE IF NOT EXISTS `hln_stations` (
`id` int(6) NOT NULL auto_increment,
`station_title` varchar(60) NOT NULL default '',
`station_display_name` varchar(60) NOT NULL default '',
`station_subtitle` varchar(60) NOT NULL default '',
`station_detailed_description` text NOT NULL,
`stream_url_or_playlist_url` text NOT NULL,
`link_type` varchar(25) NOT NULL default '',
`small_thumbnail_graphic_url` text NOT NULL,
`large_thumbnail_graphic_url` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;
Not sure what you mean by “Reordering” the database… SQL Databases typically do not make any guarantees on what order (if any) they will return records in short of an ORDER BY clause.
Easy to program, easy to use: Implement a simple drag+drop interface in HTML using jQuery or whatever javascript library works for you. In the on-complete method (or in response to a save button), trigger an ajax call which will simply send an array of
ids in the correct order. On the database side, loop over it and update the SortOrder accordingly, starting at 1, then 2, etc…Harder to program, hard to use: Implement a classical move-up and move-down buttons. When clicked, send the
idandaction(eg, up, down) to the server. There are several strategies to handle this update, but I will outline a couple:Assuming the user clicked “move up”, you can swap IDs with the previous record.
Find the previous record:
SELECT id FROM hln_stations WHERE SortOrder < (SELECT SortOrder FROM hln_stations WHERE id = ...) ORDER BY SortOrder DESC LIMIT 1Run two update statements, swapping the SortOrder. Reverse for moving down. Add special code to detect top or bottom.
etc…
There are other ways, but for a web interface, I suggest you do Drag+Drop, as the users will love it.