Creating a system to "move" records up or down in atable

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Creating a system to "move" records up or down in atable

    Hey All,

    I have a simple database table of info that I would like to be able to change
    the output order of when queried, using a sort column.
    the tables columns are such:
    page_id(pk), page_name, page_content, sort_id

    So I output the list of records with a simple query:
    <cfquery datasource="#####" name="get_order">
    SELECT page_id, page_name, page_content, sort_id FROM pages ORDER BY sort_id
    </cfquery>

    What i would like to have happen is have a link that would run a query to
    change the value of the sort order for a certain record. (hence moving it up or
    down in the list)
    Where i run into trouble is figuring out how the syntax and concept for making
    sure what amount i need to add to the current records sort_id value to make it
    higher(or lower) than the next higher(or lower) record. After that, wouldn't i
    need to increment or decrement the other records in the table so there were no
    records with the same sort_id value?

    I have included some concept code below, but i am having a hard time wrapping
    my brain around what I need to do to get this working, so any help would be
    greatly appreciated. Or perhaps is there any easier way to do this?

    thanks!


    <!-- Set starting sort number-->
    <cfset basenum = #url.sort_id#>

    <!-- based on a url variable, look for a number above or below the starting
    sort numer-->
    <cfif url.upordown = "up">
    <cfquery datasource="#####" name="get_closest">
    SELECT page_id, sort_id FROM pages WHERE sort_id > #basenum#
    </cfquery>
    <cfelse>
    <cfquery datasource="#####" name="get_closest">
    SELECT page_id, sort_id FROM pages WHERE sort_id < #basenum#
    </cfquery>
    </cfif>
    <!-- Determine the value of the sort number closest to the the basenum -->
    <cfquery datasource="#######" name="get_closest_num" dbtype="query">
    SELECT MAX(sort_id) as closest_num FROM sort_id
    </cfquery>

    <!-- package the result into a variable -->
    <cfset nextclosest = get_closest_num.closest_num>
    <cfloop query="get_order">

    <cfif order_id GT #basenum#>
    hmm this is not making sense anymore...
    <cfelseif order_id LT #basenum# >

    <cfelse>

    </cfif>

    </cfloop>

    rowbeast Guest

  2. Similar Questions and Discussions

    1. "Error Creating Control" and "Cast from String"
      I'm creating a custom date control. In appearance, it's just a textbox and a button. It has three custom properties: CalDate, CalDateType and...
    2. Design time "move" and "resize"
      I saw the following post on dotnet24x7.com. Usually Victor Garcia Aprea has been pretty helpful. I have a similar issue. I WANT to derive from...
    3. Lsass.exe System error "object name not found". System keeps rebooting
      When trying to install the drivers for the PCI modem in my laptop, prompted for the driver CD. Installed the driver without any errors but asked...
    4. Since when UNIX is the "real" system that runs the "real" machines?
      *CROSS POSTED TO comp.unix.solaris In article <o3FNa.611731$vU3.43623@news1.central.cox.net>, David Janes <djanes@cox.net> wrote: Yes, Unix...
    5. Cannot move or copy folders from "My Documents"
      Is the folder a private folder or encrypted. If it's encrypted then unless you backed up your encryption key then you'll have a hard, if not...
  3. #2

    Default Re: Creating a system to "move" records up or down in atable

    I think you might be able to do it with javascript, arrays, and a div tag. But it would be hard.

    Hard for me anyhow.
    Dan Bracuk Guest

  4. #3

    Default Re: Creating a system to "move" records up or down in atable

    <cftransaction>
    <cfset sort_id_to_move = basenum>
    <cfquery datasource="#####" name="moveToTempSortID">
    update pages set sort_id = -1 where sort_id = #sort_id_to_move#
    </cfquery>
    <cfif url.upordown = "up">
    <cfquery datasource="#####" name="makeRoom">
    update pages set sort_id = sort_id + 1 where sort_id = #sort_id_to_move# - 1
    </cfquery>
    <cfquery datasource="#####" name="moveToNewSortID">
    update pages set sort_id = #sort_id_to_move# - 1 where sort_id = -1
    </cfquery>
    <cfelse>
    <cfquery datasource="#####" name="makeRoom">
    update pages set sort_id = sort_id - 1 where sort_id = #sort_id_to_move# + 1
    </cfquery>
    <cfquery datasource="#####" name="moveToNewSortID">
    update pages set sort_id = #sort_id_to_move# + 1 where sort_id = -1
    </cfquery>
    </cfif>
    </cftransaction>

    Kronin555 Guest

  5. #4

    Default Re: Creating a system to "move" records up or down in atable

    Kronin555, thanks for the code. I think i understand what you are doing here. I will have a go at it and re-post to this thread if I run into problems.

    thanks!


    rowbeast Guest

  6. #5

    Default Re: Creating a system to "move" records up or down in atable

    Kronin555,

    So that code works like a charm for incrementing and decrementing the sort_id,
    thanks so much.
    I thought i would make a note here for anyone else who searches for this kind
    of thing that in order for your numbering scheme to not get out of sequence
    (e.g. in a 3 record set the sort numbers could be 1,2,4 if you clicked down on
    the 3rd record) you need to put some trapping in place to not allow the numbers
    to move out of sequential order. (1,2,3) because if they to (as in example one)
    it would take 2 clicks to move the third record above the second. So test for
    first record and last record and if the user is trying to move them, and dont
    let them!

    Thanks for the help!

    rowbeast Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139