Page 1 of 1

Order Levels

Posted: Fri Dec 24, 2004 9:01 pm
by keiths
I am trying to create order levels so that a user can sort a row.

Basically it would be like this:

1 item1
2 item2
5 item3

The number will be in a text field that they can change. This is all easy except I also want it to work so that say they make item2's new number 1. Then I want item1 to push down to 2 and item2 to go to 1.

Right now it basically loops through an array with the order level numbers. If it finds one that's the same it adds 1 to it. Which doesn't really work how I want it to because you will never be able to change item2 to 1.

If anyone has any ideas or any code for this please let me know.

Thanks

Posted: Fri Dec 24, 2004 9:12 pm
by Hibba
I'm not really sure if this will help, but maybe something to chew on. In a form I have created, it steps through different question. Basically, it loops and sets the variable $currentquestion for the question that you are on. So in short, could you possibly use a variable to be set depending on what row they are in? It's Christmas Eve, I figured I would give it a try!

Merry Christmas!

Posted: Fri Dec 24, 2004 11:11 pm
by rehfeld
you could use this to swap any 2 elements

Code: Select all

<?php

function array_swap($keya, $keyb, $array){
    $tmpa = $array[$keya];
    $tmpb = $array[$keyb];
    $array[$keya] = $tmpb;
    $array[$keyb] = $tmpa;
    return($array);
}

?>

Posted: Fri Dec 24, 2004 11:16 pm
by keiths
It's not going to be swapping.

Basically say you have this

1=>a, 2=>b, 3=>c, 4=>d

and you want to make c 1

it would be

1=>c, 2=>a, 3=>b, 4=>d

It would work like the netflix queue system. Here's a screenshot of it I found on the internet: http://www.pelarious.com/art/squib/netflix-queue.gif

If you change Farscape to Priority 1, it would move Neon Genesis to 2 and Angel to 3.

Of course you could move number 1 down to 3 and it would move accordingly.

Posted: Fri Dec 24, 2004 11:40 pm
by rehfeld
then use a form to build an array.
use the "rank" the user chooses for each field, as the corresponding array key.

if for field 'a' the user gives it the number 3, then your array would have this entry
$array[3] = 'a';

you would acheive that by doing something like this:
$array[$_POST['a']] = 'a';


you might need to do some "cleanup" if the user leaves a field blank, or enters an absurd rank like 1000
you could have php reindex the array for you after your done building it.
do this by doing
$reindexed_array = array_values($built_array);


i agree its not a totally trivial thing, and you would obviously need to apply the concept into a loop because your cant hardcode $_POS['a'] and the like into the script, but see if that sparks your thoughts.

Posted: Sat Dec 25, 2004 1:33 am
by keiths
Wouldn't it be easier to just name the form field something like fieldName[<?=$r['id']?>]. That way it creates the array automatically?

That's not the part I'm having a problem with. I'm having a problem with changing the order level (priority) to what it should be.

Thanks