Click to See Complete Forum and Search --> : [Resolved] Re-order Function - please critique
Roger Ramjet
01-15-2005, 04:24 PM
There is a post here (http://www.phpbuilder.com/board/showthread.php?s=&postid=10584746#post10584746) 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);
Roger Ramjet
01-15-2005, 04:25 PM
PS if someone knows of an array function that does this sort of thing, GREAT. Let us know please.
Roger Ramjet
01-15-2005, 05:53 PM
Anyone who looks at the first try will laugh their heads off: php arrays are not my forte. Still and all, this has been good for me, I've learnt how they work now: and that is not how I'm used to in other languages.
Here is the function that woorks a treat
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-1] = $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
$o<$n ? $inc = -1 : $inc = 1 ;
// set array index for loop
$i = $n-1;
while ($count>0) {
$ary[$i] = $ary[$i]+$inc;
// update loop count
$count--;
// update array index for next item
$i=$i+$inc;
}
return $ary;
}
May seem like a trivial exercise to some but it is going to fix thie other fellows problem.
And yes, I code PHP like a newbie. Stared with it back in 2000, and left off in 2001. Been solid Access, VBA and VB since. Still is most of the time so I get the syntax and constructs mixed all the time. I'm just here gearing up for the next generation of applications we are going to use - I'm moving us onto the web big-time. Just kicking my heals while the finance guys set up our new companies. So that's my story.
Roger Ramjet
01-15-2005, 09:20 PM
And no, I did not end up using this at all for the other guy's problem. Used sql like always: the best thing if it can possibly be done that way. But I started to get my head around PHPs wierd and wonderfull arrays.
Sorry about the bump.
onion2k
01-17-2005, 06:24 AM
and that is not how I'm used to in other languages.
Just as a matter of interest, how do PHP arrays differ from arrays in other languages you're used to? I know a few different languages and I've never spotted any differences.. Its always been zero indexed, push, pop, shift and unshift, splice and $array[$key]=>$value everywhere I've looked. Which languages treat them differently?
Roger Ramjet
01-17-2005, 09:12 AM
Well, that is what I'm used to as well. What was throwing me was that I wanted to use my table ids as key. Since they are integers, php was using them as index., despite my use of what I thought was the correct syntax for adding key=>value pair. Weedpacket pointed out that quotes around the key would overcome that. The main diff is that php does not reset the index when you empty the array, !!??. This is one of those instances where flexibility is NOT a bonus. I like my indexes to just be what they are, 0 is the first item, 1 the next, and if I remove 0 then 1 now has index 0. You know, it's just the offset from index base.
Larry Bigner
01-17-2005, 02:16 PM
You can use $array[] = array();
That will empty it and give it an index of 0.
Roger Ramjet
01-17-2005, 02:39 PM
Final note in an already resolved thread. Thanks folks, I needed to learn a lot more about php arrays and this helped a lot. I got the logic down it is just the language specifics that threw me.
As to the application this function was coded for. I solved Ted's problem using SQL as usual. See the final solution in this thread (http://www.phpbuilder.com/board/showthread.php?s=&threadid=10292244&highlight=god+bless) .
Weedpacket
01-18-2005, 04:18 AM
For amusement I thought I'd throw a few of the array functions at this. Unsuitable for really big arrays, because it doesn't operate in-place.
function reorder($old_array, $from, $to)
{
$count = count($old_array);
// Silly
if($from<0 || $from>=$count) return $old_array;
if($to<0 || $to>=$count) return $old_array;
// Boring
if($from == $to) return $old_array;
$min = min($from, $to);
$max = max($from, $to);
$before_all_this = array_slice($old_array, 0, $min);
$after_all_this = array_slice($old_array, $max);
$the_rest = array_slice($old_array, $min, $max-$min+1);
// Naughty obscurationism
if($from<$to)
array_push($the_rest, array_shift($the_rest));
else
array_unshift($the_rest, array_pop($the_rest));
$new_array = array_merge($before_all_this, $the_rest, $after_all_this);
return $new_array;
}
Roger Ramjet
01-18-2005, 08:26 AM
Thanks Weedpacket. Now I'll have to spend the next half hour looking up those functions to see what's going on. If I get it then I would have to use the index position of the items in the new array to save the new order in a db field, that being the original problem.
PHP Builder
Copyright Internet.com Inc. All Rights Reserved.