SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool Changing order of pages in an asp CMS

    Hi There

    I am currently building a CMS using asp and Dreamweaver behaviours. All is going well and I can add, edit and delete pages. However I need some advice on how I can give the user the option of changing the order that the pages are displayed within the navigation menu. I’ve seen this on other CMS’s where the pages are displayed as a recordset on a page with a button next to each of the rows entitled ‘move up’ and ‘move down’. Then when any of the buttons are clicked the action is performed and the page is refreshed i.e. the selected page is moved up or down. Please could someone show me how this is done as being more of a web designer this issue is proving to be quite tricky for me?

    Many Thanks in advance.

  2. #2
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I would create a field in your DB for sorting order. If the user wants to move a menu item to the top then the sorting order field for that menu record would be changed to 1.

    Make sense?

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool

    Many thanks for the quick response.

    I think I understand what you mean. Would the sort column be added to the ‘Pages’ table I already have in my DB? i.e. my columns would then be pageID, pageName, Page content, Active, sort. So when a user adds a new page a new record is created in the DB, which they can then edit. What value would I place in the sort column when they first add the new page? Also would I have a ‘move up’ and ‘move down’ form button next to each of the pages in the recordset as I mentioned by, which would change the value of the sort column for that particular page i.e. increment by 1 or decrement 1? If yes would you know how to actually code this? If I’m going down the wrong root, please let me know…

    Once again many thanks for your help so far.

  4. #4
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 9greej10
    Would the sort column be added to the ‘Pages’ table I already have in my DB? i.e. my columns would then be pageID, pageName, Page content, Active, sort.
    Correct, exactly like that

    Quote Originally Posted by 9greej10
    What value would I place in the sort column when they first add the new page?
    I would find out what the last record entered and increment that by one. So if you have 12 menu items, the next menu item added will be in 13th place. Then its up to the user to move the item where they would like it.

    Quote Originally Posted by 9greej10
    Also would I have a ‘move up’ and ‘move down’ form button next to each of the pages in the recordset as I mentioned by, which would change the value of the sort column for that particular page i.e. increment by 1 or decrement 1?
    Correct. Basically you could have an up arrow and down arrow. The up arrow could pass two querystring values.

    1. Menu ID
    2. Up or Down value

    So your link might look something like this:

    http://www.blahblah.com/CMS_edit.asp?menu_id=23&pos=up

    Now you can use some pretty simple logic to update your record:

    Code:
    If request.querystring("pos") = "up" then
    	'move record ID 23 up one position
    Elseif request.querystring("pos") = "down" then
    	'move record ID 23 down one position
    End if

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see what you mean thanks! You have made it look very simple!

    One last question though, how do I find out what the last ‘sort’ position is? i.e. if I have 13 items in my menu (recordset) how do I find out what the last ‘sort’ value? Do I read out the recordset and perform some logic of some sort or it there a another way of doing this? Sorry if this is a stupid question, perhaps I’m making it more complicated than it actually is, but I’m just trying to envisage how it will work.

    Thanks again

  6. #6
    SitePoint Enthusiast Northern Star's Avatar
    Join Date
    Aug 2006
    Location
    Cheshire, UK
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi 9greej10,

    You can get the last (biggest) sort order by doing something like...

    Code:
    SQL = "SELECT * FROM Pages ORDER BY pageSort DESC"
    Set RS = DB.Execute(SQL)
    
    nextSort = RS("pageSort") + 1
    It's worth noting that when you perform your 'Up/Down' function (as 'Thing' explained) you'd have to swap the sort order with the 'Page' that already had that sort position.

    IE. If your 'Page' was in position '10' and you clicked 'Move Up' then you'd have to swap the 'Page' with the page position of '11' to '10', then update your current 'Page' to '11'. Hope that made sense!

    Andy

  7. #7
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool

    Hi Andy

    Thanks for the tips and just to clarify:

    1. create recordset (without loop) in desc order. The result of this will be the last page (largest sortNumber) in the DB. At this point I will then know what the sortNumber of the last page added.

    2. Then when a user adds a new page within the CMS I would get this value and +1 then include this in the ‘INSERT’ statement.

    I think I have understood this correctly.

    You then mentioned that when moving the pages up and down I would need to increase the pageSort(if moving up) of the page being moved and decrease the page above. I am a little confused as to the best way to tackle this, because lets say I have the following list and I want to move ‘find us’ to 2nd from the top – how would I perform this in terms of manipulating the recordset and using logic?

    PageID PageName pageSort
    1 home 1
    2 aboutus 2
    3 our services 3
    4 products 4
    5 find us 5

    Thanks again for your help so far I really appreciate it.

  8. #8
    SitePoint Enthusiast Northern Star's Avatar
    Join Date
    Aug 2006
    Location
    Cheshire, UK
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 9greej10
    I am a little confused as to the best way to tackle this, because lets say I have the following list and I want to move ‘find us’ to 2nd from the top – how would I perform this in terms of manipulating the recordset and using logic?

    PageID PageName pageSort
    1 home 1
    2 aboutus 2
    3 our services 3
    4 products 4
    5 find us 5
    OK, this is what I would do...

    Code:
         '## Pass the Current PageID & the direction via a querystring... your link for moving 'find us' up one position would look something like this..
    
         <a href="?move=up&pageID=5">Move Up</a>
    Code:
    <%
    
         If Request.QueryString("move") <> "" AND Request.QueryString("pageID") <> "" Then
              
              pageID = Request.QueryString("pageID")
    
              SQL = "SELECT sortNumber FROM Pages ORDER BY sortNumber DESC"
              Set RS = DB.Execute(SQL)
    
              If RS.EOF Then
              Else
              
              fSortNumber = RS("sortNumber") '## The original / starting sort number
    
                   If Request.QueryString("move") = "up" Then
                   nSortNumber = fSortNumber - 1 '## The new / next sort number
                   Else
                   nSortNumber = fSortNumber + 1 '## The new / next sort number
                   End If
    
                   '## Now check 'Pages' table for any pages that already have that position (nSortNumber).
    
                   SQL = "SELECT pageID, sortNumber FROM Pages WHERE sortNumber=" & nNumber"
                   Set RS = DB.Execute(SQL)
    
                   If RS.EOF Then
                        '## There is no other page sharing this position, so no need to update any.
                   Else
                        
                        uPageID = RS("pageID") '## The ID of the page we are swapping the sort number with
    
                        SQL = "UPDATE Pages SET(sortNumber=" & fPageNumber & ") WHERE pageID=" & uPageID
                        DB.Execute(SQL)
                    End If
    
                    '## Now that we've "swapped" the sortnumber for the existing page we can update our page with the new sortnumber
    
                    SQL = "UPDATE Pages SET (sortNumber=" & nPageNumber & ") WHERE pageID = pageID
    
                    DB.Execute(SQL)
              
    
    %>
    I apologise if this is a bit hard to follow... Basically, this is what happens in laymen's terms...

    • Get the last sortnumber (the largest).
    • Calculate the new sortnumber for the page('find us') we will be moving (up/down).
    • Search the database for any pages which already have this sortnumber
    • If found, update this page (in this case 'products') with the sortnumber of the page we want to move ('find us').
    • Now update the page we are moving with the new sort number.


    Let me know if you want any more explanation... I may well have confused myself somewhere along the line!
    "If it ain't broken, don't fix it!"
    ----
    Northern Star - Web design, stategy & development.


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
  •