|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
I need an order logic. In otherwords I have a db with 2 relavant fields category and categorypriority. I need to be able to let the user change the priority and when priority is changed to one (the highest) all the other priorities are shifed down and the same thing if the priority is changed to say 5 then 5 needs to move.
Does anyone have any suggestions? |
|
#2
|
|||
|
|||
|
do you have more than 5 categories?
If so, can there only be one priority 1 and one priority 5? If the user makes something priority 1, do all the other priorities stay in order, or does just the old priority 1 need to change?
__________________
- Rogier Doekes |
|
#3
|
|||
|
|||
|
There can be many categories but I will know the number (from the number of categories in the recordset)
The tricky part is if they change category 6 to be category 1 then category 1 has to move to 2 and 2 to 3 etc.... |
|
#4
|
||||
|
||||
|
You just need to loop through your records and perform a query like this (this will raise the priorities)
Code:
for idx = lowestPriority to highestPriority UPDATE table SET priority = priority + 1 WHERE priority = " & idx next |
|
#5
|
|||
|
|||
|
hey stump, I believe your code will result in all records having the highest priority.
esthera, I think you need to add a field in your table old_priority, which you initialize to the priority. Then the code will be Code:
for idx = lowestPriority to highestPriority UPDATE table SET priority = priority + 1 WHERE old_priority = " & idx next UPDATE table SET old_priority = priority Let me know if I whiff on this pitch |
|
#6
|
||||
|
||||
|
Yes - I think you're right... I had another look @ the code I had written that uses this single increment method, and noticed that the WHERE is looking for the record ID, rather than the priority. Hopefully esthera gets the general idea tho.
I'm not sure what your second update statement achieves in it's current form tho rdoekes? Are you missing the WHERE statement? |
|
#7
|
|||
|
|||
|
i was trying to make the old_priority the same as the changed priority.
But i agree with you, that if you use the recordID for the loop your code will work fine. But you need to exclude the record which was set to the lowest priority: Code:
UPDATE table SET priority = 1 WHERE recordID = xxx for idx = firstrecord to lastrecord If Not idx = xxx Then UPDATE table SET priority = priority + 1 WHERE recordID = " & idx End If next |
|
#8
|
|||
|
|||
|
Thanks for your help. I am going to try this. But will this also work if they want to say change id 10 to priority 5 from 8 for example.
I also want to let them change priorities. The priority should only move up if it is larger than the set priority. Thanks, esther |
|
#9
|
||||
|
||||
|
There's nothing really technical we can help you with. It's now just down to your logic. The best way to tackle a problem like this is to sit down with piece of paper and step though the problem, thinking about each step, and writing out some psuedo-code.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > ASP Development > sorting categories |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|