Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2009
    Thanked 0 Times in 0 Posts

    Question Navigation link sorting for custom cms

    I am building a small cms where the user can add, edit, and removed pages. Each time the user adds or edits a page, they can specify the order placement of that page within the navigation.

    I have a field called sort_order where the user can add the sort order number. What I am wanting is when the user adds a sort number that is already assigned to another page, that the page with that sort number is bumped down along with any other pages underneath.

    I hope this makes sense. If at all possible, it would be cool to integrate this with a drag and drop function instead of having to manually type a number. Thanks in advance for the help!

  2. #2
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Great Britain.
    Thanked 6 Times in 6 Posts
    Pfft. I imagine something like this;

    PHP Code:

    13// The thing you are bumping, or moving, has an ID of 13.
    $thingSortOrder 2// We are moving this item to position 2.

    $sql 'SELECT id, sort_order FROM things WHERE sort_order = "' $thingSortOrder '" LIMIT 1';
    $result $db->query($sql);

    if (
    $result->numRows() != 0) {
    // Something else has this sort order, so lets bump everything with a HIGHER sort order by 1.
    $sql 'UPDATE things SET sort_order = (sort_order + 1) WHERE sort_order >= "' $thingSortOrder '" '
    $result $db->query($sql);

    // Now lets use the sort order that we wanted.
    $sql 'UPDATE things SET sort_order = "' $thingSortOrder '" WHERE id = "' $thingId '" LIMIT 1';
    $result $db->query($sql); 
    This will mean that you will eventually end up with sort orders which are not consecutive (eg: 1, 2, 7, 8, 9, 15, 16), but that should not really matter. This should be fine with a table of about 50 rows or so, with a maximum potential update of 49+1 rows, but this is an admin function and on average, those conditions will be pretty rare.
    Last edited by xconspirisist; 03-31-2010 at 12:22 AM.
    If I have been helpful, use the "thank" button - It makes me happy!

    xconspirisist.co.uk - homepage of my online alias
    technowax.net - a community for people interested in all forms of modern technology.

  3. #3
    Senior Coder
    Join Date
    Jul 2009
    South Yorkshire, England
    Thanked 304 Times in 303 Posts
    Add a time field as well and sort by position and time updated. Multiple items with the same position id will be sorted by time they were updated.


Tags for this Thread

Posting Permissions

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