lewis987 Posted March 30, 2008 Share Posted March 30, 2008 ok... this is going to be hard to describe so bear with me... I have a script that im coding up for a friend (its been in development for over 6 months now) and now ive hit a dead end. Its basically "Bebo" but on php instead... So i want to allow users to edit their profiles... Which does happen, They can change whats enabled and whats not. The script i am currently coding is to allow users to change where everything is located on the profile. So the blog doesnt always have to sit in the same place. Everything is stored in a database, so i need to make a loop that works with the new and old places. Page LAYOUT: |--------| | HEADER | |--------| | 1 || 2 | | 3 || 4 | | 5 || 6 | | 7 || 8 | |--------| so say someone wants to move number 5 to number 2, i want to make the script move everything down one, instead of swapping 5 and 2 around. I mean, 5 -> 2, 2 -> 3, 3 -> 4, 4 -> 5. So everything is moved... i know i can do it the other way, which will do until i can get a solution. I will post code if you require it. Quote Link to comment Share on other sites More sharing options...
cunoodle2 Posted March 30, 2008 Share Posted March 30, 2008 I would look into storing everything temporarly into an array. I'm gonna write some basic code here but the syntax will definelty be wrong. So hopefully it give you the right idea and then you can move on with it... <?php function swap($items[],$new,old) { //arrays start at zero not one so lets decrease each reference item by one $new = $new - 1; $old = $old - 1; $j = 0; //a counter we will use in for loop below $sorted_array = array(); for($i = 0; $i < count($items); $i++) { if ($new == $i) { array_push($sorted_array, $items[$i]); $j=$j+1; } else { array_push($sorted_array, $items[$j]); } $j = $j + 1; } return $sorted_array; } ?> Fuctions takes 2 variables. $items = all the items IN order in the array $new = the location of the item TO be moved (in your example this would be '5') $old = the location of the array item to be BUMPED (in your example this would be '2') Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.