SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    319
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ordering a bunch of things

    I'm essentially looking for advice on how todo this, if anyones every done it before and so on...

    Okay so I have a list, when creating the `order` field is set to 0, I have a ton of rows with order as 0.

    I display this list, and then next to it I want to have the ability to move things up and down.

    Obviously some sort of loop to update things before and above the item pressed to be moved up or down is going to happen, but the idea of giving them all a start number is confusing and to then manage there order.

    I was thinking, maybe upon insertion it should look at last row (by order) and +1 onto that order field in the new row...however for now I have tons of rows with no number...

  2. #2
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I would suggest is to run through your database and assign a sequential number to each item, leaving some space in between so you can insert directly into the list where you want (e.g. start the first item at 10, next at 20, next at 30, etc). You should be able to easily run a script that will do this for you, or else do it by hand.

    Once that's done, swapping spaces is easy - you literally just swap the relevant items' order number. If you're really clever, you can do this in a single SQL statement, but I recommend doing it in a couple statements as it'll be more readable and thus more maintainable (i.e. you can come back to it later and actually know what you were doing!).

    Inserting a new item to the end of the list would be just like you suggested, yes, except again I would add 10 instead of only 1 to allow for easy inserts into the middle of the list. Even if you never plan to insert into the middle of the list, doing this won't hurt and will make life easier if you change your mind in the future (I learned this the hard way!).
    PHP questions? RTFM
    MySQL questions? RTFM

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Build your list and index the order numerically, a numeric keyed array.
    You can then have a function that reorders that array based on the new position of list items. How do plan on presenting this to the user? I get the ideal of a list, but how are you going to allow the user to move options up or down?
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  4. #4
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    319
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks kromey, i like that idea.

    For the moment a simple arrow up and down, later it'll be ajax.

  5. #5
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by azz0r_wugg View Post
    For the moment a simple arrow up and down, later it'll be ajax.
    Heh, that's exactly what I said. That was almost a year ago. There's still just simple up/down arrows...
    PHP questions? RTFM
    MySQL questions? RTFM

  6. #6
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    319
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Edit ; nevermind.
    Last edited by azz0r_wugg; Jun 16, 2007 at 15:52.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •