Jump to content

Help


lewis987

Recommended Posts

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.

Link to comment
Share on other sites

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')

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.