Forum Moderators: open
Widgets:
1. Blue Widgets
2. Red Widgets
3. Pink Widgets
4. Black Widgets
Wiggles:
1. Red Wiggles
2. Pink Wiggles
3. Blue Wiggles
This are all generated from a database and at the moment sorted by their UID. Now a widget supplier might want to change the order so Pink Widgets becomes first in the list. I have toyed with a few ways of doing this but they seem very cumbersome and wondered if here was a simpl way I am missing.
I was thinking I could have a field "fld_sort" that is used to sort the SQL query. thenin the admin section if pink widgets (currently 3 in the sort field) was moved to the top it would change its sort number to 1 and then everything between its old position and its new one increases by 1.
It should work but as I said before will put quite a load on the server unless I am missing something?
The ideal way is to start with the premise that two items can't have the same sort number. Then code your business logic or stored procedure to handle the insertion automatically. An easy way is to update all items where the sort order is greater than or equal to the one you are inserting. When you remove something from the sort, then decrement everything that is greater.
CREATE PROCEDURE [dbo].[getWidgets]
@WidgetID smallint
AS
SELECT WidgetID, Color FROM tblWidgets ORDER BY
CASE WHEN WidgetID <> @WidgetID Then 1 END,Color
GO
Heres the code that uses it to make the dropdown list
function GetRefData ( strSPName, intSelected )
dim objConn,objRS, strReturn
set objConn=Server.CreateObject("ADODB.Connection")
openDB( objConn )
set objRS=objConn.Execute ("Exec dbo." & strSPName & " " & intSelected)
while not objRS.EOF
strReturn = strReturn & vbCrLf
strReturn = strReturn &"<option value='"&objRS(0)&"'>"&objRS(1)&"</option>"
objRS.MoveNext
wend
GetRefData=strReturn
closeRS( objRS )
closeDB( objConn )
end function
To call it:
<select name="Widgets"><%= GetWidgets objReseller.FirstWidget )%></select>