There is a post here about re-ordering a forum's contents. I'm working on it, interesting problem. Now I know how the html form will look: I'll simplify the problem by submitting and reordering after any 1 item is changed. This will ensure that the user does not try to set 2 items to the same order.
I have worked out the following function that I need someone to test. I'm not set up to do that right now. I guess that there will be some basic syntax errors but I hope that the logic is correct. The idea is that you pass in an array of the existing order, the order index of the item to change and it's new order index. Get me? So if number 2 on the list has been set to be number 4 the call would be
$old_ary = array('id1'=>0,'id2'=>1,'id3'=>2,'id4'=>3,'id5'=>4,'id6'=>5,'id7'=>6);
function reorder($o, $n, $ary) {
// get (unsigned)differance between old and new values of changed item
// this gives the number of other items that need updating: loop count
$count = abs($o-$n);
// set new order for changed item
ary[$o] = $n;
// get increment for others to be updated
// if old < new others move up: inc = -1
// if old greater > new others move down: inc = +1
if {$o<$n ? $inc = -1 || $inc = 1 }
// set array index for loop
$i = $n;
while ($count>0) {
ary[$i] = ary[$i+$inc];
// update loop count
$count--;
// update array index for next item
$i=$i+$inc;
}
return $ary
}
$new_ary = reorder(2,4,$old_ary);