ColdFusion Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreColdFusion Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 24th, 2005, 03:40 PM
kellogg880 kellogg880 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 3 kellogg880 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 57 sec
Reputation Power: 0
Red face 2 lists in sql query

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>

Reply With Quote
  #2  
Old January 24th, 2005, 05:30 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,982 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 18 h 20 m 26 sec
Reputation Power: 1028
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>
this will allow you to update all rows that need updating in one call to the database

of course, you gotta rewrite your loops so that they take place inside the CFQUERY, not outside

__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon

Reply With Quote
  #3  
Old January 25th, 2005, 12:15 PM
kellogg880 kellogg880 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 3 kellogg880 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 57 sec
Reputation Power: 0
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
SET sortorder = CASE
<cfloop list="#form.neworder#" index="neworder_index">
<cfset id_counter=1>
<cfloop list="#form.id#" index="id_index">
WHEN #order_counter# = #id_counter# THEN #neworder_index#
<cfset id_counter=id_counter+1>
</cfloop>
<cfset order_counter=order_counter+1>
</cfloop>
END
WHERE id IN (#form.id#)
</cfquery>

Reply With Quote
  #4  
Old January 25th, 2005, 12:37 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,982 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 18 h 20 m 26 sec
Reputation Power: 1028
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>

Reply With Quote
  #5  
Old January 25th, 2005, 01:03 PM
kellogg880 kellogg880 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 3 kellogg880 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 57 sec
Reputation Power: 0
Thanks, you're the man. I owe you a beer.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreColdFusion Development > 2 lists in sql query


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT