SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to change link order in a db driven menu

    Ok, I have a database with some content and linknames for the menu. I need to give a number for every link to control the linkorder in the menu.

    id | linkname | linkorder | ...
    ----------------------------------------
    1 | xxxlink 1 | 1
    2 | xxxlink 2 | 3
    3 | xxxlink 3 | 2
    4 | xxxlink 4 | 5
    5 | xxxlink 5 | 4

    How about adding a link to the db?
    Creating a new record and set linkorder to 6...

    Aaarg, this is way too hard for me!

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2002
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    There is probably a bit more to this, such as when you remove things or need to re-order them, but to simply add a record on the end, use...

    'SELECT MAX(linkorder) AS lastvalue FROM mytable'

    ...to see what the biggest value is and add one, which gives you your next value to use in your append.

    Ed.

  3. #3
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I also need to reorder them...

    I also need to reorder them...

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2002
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I thought you might. I did this years ago but I don't have the code now. I would think someone would have a snippet for this, but it is (from memory) a rather convoluted thing to achieve. I will go through the logic which should get you going in the meantime.

    Additions we have covered, so that is fine.

    Deletions are a little trickier as it leaves a hole which needs to be filled. This can be easy to do if you store the linkorder number of the deleted item and -1 from every item with a linkorder greater than the deleted item.

    Moving items around is a little more intense. One thing to consider in this is whether linkorder allows duplicate fields. It is better to not allow dupliate fields as you want exclusive values for each item, but this neccesitates a little more complex processing during the update cycle. Another thing to consider is whether the items will swap with the next value only or will be move * number of places. There are a few ways to actually to this, but the important thing is which item to move and whether to increment it and decrement it and by how much?
    To keep things simple (at least to start) I would just have a go at swapping items with the next item and once this works you can move on.
    Ok, the basic logic is get the item ID, linkorder and direction. You can get these from a form or url? Get the ID and linkorder of the next item by using the direction value. Add or subtract one from linkorder of the selected item, and vice-versa for the next item.

    Ah, found something in a stoneage cms...

    [VBS]
    page = request.querystring("page")
    position = request.querystring("position")
    if Not IsEmpty(request.querystring("move")) Then
    rsMove.open "SELECT * FROM ARTICLES WHERE URL = '" & page & "' AND page_order = " & position, cnn, 2, 2
    if request.querystring("dir") = "up" Then
    if position <> 1 Then
    rsMove("page_order") = rsMove("page_order") - 1
    rsMove.Movelast
    rsMove.close
    rsMove.open "SELECT * FROM ARTICLES WHERE URL = '" & page & "' AND page_order = " & position - 1, cnn, 2, 2
    rsMove("page_order") = rsMove("page_order") + 1
    rsMove.Movelast
    rsMove.Close
    End If
    End If
    if request.querystring("dir") = "down" Then
    rsMove("page_order") = rsMove("page_order") + 1
    rsMove.Movelast
    rsMove.close
    rsMove.open "SELECT * FROM ARTICLES WHERE URL = '" & page & "' AND page_order = " & position + 1, cnn, 2, 2
    rsMove("page_order") = rsMove("page_order") - 1
    rsMove.Movelast
    rsMove.Close
    End If
    End If
    [/VBS]

    ...so this example gets page (unimportant for you), position and direction from a url qs, then selects the record, changes the value either up or down, re-uses the recordset to change the neighbouring record and (this bit omitted) re-displays the page. I have obviously not defined 'no duplicates' for the position field but I think you can ensure your own data integrity if the script is this simple?

    Hope this helps.

    Ed.


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
  •