I have a feeling that there should be a really cool way of acheiving this, but I can't seem to crack it. I just thought I'd see if anyone else has come across this before and is willing to supply that spark of genius I so desperatly need!
Here's what I'm trying to do:
I have a fairly basic mySQL table as follows:
chapter_id INT AUTO_INCREMENT
chapter_sequence_id INT
chapter_title VARCHAR(255)
body LONGTEXT
I am printing the chapter titles out to the PHP page, hyperlinked with a chapter_id reference in the querystring that leads off to another page - fairly standard stuff.
But..
I would like the user to be able to change the order that the chapters appear on the page (independent of the chapter_id's which aren't necessarily consistent/contiguous).
I have 2 buttons to the left of each chapter on the page. One moves the chapter up a position and the other moves it down a position. The first chapter cannot be moved up a position (because its first!) and the last similarly cannot be moved any further down the list.
The numbers that represent the position of each chapter need to be kept contiguous within the table, as they will be used in another part of the application to link between the various chapters.
My intention is to store the "actual" position of the chapter in the second INT field in the DB table and use this to ORDER BY in my sql statement.
Does anyone have any bright ideas on how to handle the re-ordering of the records when the user clicks and up or down button?
Once I have the new order in an array (for example) it should be relatively easy to do an UPDATE statement on the table to change all the chapter_sequence_id's accordingly.
I suppose I just need to know the best way of re-ordering the records using PHP before UPDATING the DB table and re-loading the page.
Any ideas will be greatly appreciated!
jr