|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
So, I have 2 lists, each with 5 values.
One list is the id numbers of the row, the other is the new value to be written into one of those fields. The lists are in corresponding order. How can I loop thru both lists at the same time to enter in the corresponding values into their correct row? ex: form.id = 52,21,5,14,8 form.neworder = 3,2,4,5,1 <cfparam name="order_counter" default="0"> <cfparam name="id_counter" default="0"> <cfloop list="#form.id#" index="id_ctr"> <cfset id_counter=id_counter+1> <cfloop list="#form.neworder#" index="order_ctr"> <cfset order_counter=order_counter+1> <cfif id_ctr EQ order_ctr> <cfquery name="updateSortOrder" datasource="xxxx"> UPDATE articles SET sortorder = #order_ctr# WHERE id = #id_ctr# </cfquery> </cfif> </cfloop> </cfloop> |
|
#2
|
||||
|
||||
|
try running this query as a test --
Code:
<cfquery name="updateSortOrder" datasource="xxxx">
UPDATE articles
SET sortorder =
CASE WHEN id = 52 THEN 3
WHEN id = 21 THEN 2
WHEN id = 5 THEN 4
WHEN id = 14 THEN 5
WHEN id = 8 THEN 1 END
WHERE id IN (52,21,5,14,8)
</cfquery>
of course, you gotta rewrite your loops so that they take place inside the CFQUERY, not outside ![]() |
|
#3
|
|||
|
|||
|
Thanks, your help is much appreciated. CASE statements work much easier.
But I've now got a problem with my looping . Every row gets the first value of the list form.neworder entered into it...form.neworder = 3,4,1,5,2 form.id = 22,23,24,18,16 <cfquery name="updateSortOrder" datasource="xxxx"> UPDATE articles</cfquery> |
|
#4
|
||||
|
||||
|
it doesn't have to be that complicated
![]() Code:
<cfquery name="updateSortOrder" datasource="xxxx">
UPDATE articles
SET sortorder = CASE
<cfloop index="i" from="1" to="#ListLen(form.neworder)#">
WHEN id = #ListGetAt(form.id,i)# THEN #ListGetAt(form.neworder,i)#
</cfloop>
END
WHERE id IN (#form.id#)
</cfquery>
|
|
#5
|
|||
|
|||
|
Thanks, you're the man. I owe you a beer.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ColdFusion Development > 2 lists in sql query |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|