Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Joining Two Arrays Values Together

Engaged ,
Mar 02, 2008 Mar 02, 2008
Hi,

I have a form, with some select lists etc and hidden fields with the records ID so that it knows what to update.

If I have two lists as such and convert them to arrays, how can I join each of their values?

<cfset variables.setTemplateArray = listToArray(form.setTemplate,",","true") />
<cfdump var="#variables.setTemplateArray#" />

<cfset variables.setTemplateIDArray = listToArray(form.setTemplateID,",","true") />
<cfdump var="#variables.setTemplateIDArray#" />

e.g if setTemplateArray returns:

a
b
c

and setTemplateIDArray returns

1
2
3

How can I join them and so that their values are directly related to eachother in order?

e.g.

a 1
b 2
c 3

Is this possible?

Thanks,
Mikey.
383
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 02, 2008 Mar 02, 2008
I don't think you have to turn them into arrays. The simplest way to match values is to loop through one of the lists and use ListGetAt to get the matching values.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 03, 2008 Mar 03, 2008
Hi Dan,

As ever, you are a great help! Thank you very much. I managed to solve it forgetting about the arrays and using the list function you suggested.

Below is the code if you'd like to see.

<cfset variables.setTemplateArray = form.setTemplate />
<cfset variables.setTemplateIDArray = form.setTemplateID />

<cfoutput>
<ul>
<cfloop from="1" to="#ListLen(variables.setTemplateArray)#" index="index">
<li>
#index#. <strong>ID</strong> = #listGetAt(variables.setTemplateIDArray, index)# <strong>Template</strong> = #listGetAt(variables.setTemplateArray, index)#
</li>
</cfloop>
</ul>
</cfoutput>

Thanks again!!
Mikey.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 03, 2008 Mar 03, 2008
Hi...

Actually, I have another question. For some reason when I use this loop to go over a query it says that my list is undefined.

Look at the following two pieces of code...

<cfset variables.setTemplate = arguments.setTemplate />
<cfset variables.setTemplateID = arguments.setTemplate />

<cfloop from="1" to="#ListLen(variables.setTemplate)#" index="index">
<cfoutput>[#index# - #listGetAt(variables.setTemplate, index)# / #listGetAt(variables.setTemplateID, index)#] </cfoutput>
</cfloop>


This outputs the values just fine (these lists are supplied to this cfc via cfinvoke - didn't include all that here).

However, as soon as I try to do an UPDATE query it says that setTemplate is undefined in variables. e.g.


<cfset variables.setTemplate = arguments.setTemplate />
<cfset variables.setTemplateID = arguments.setTemplate />

<cfloop from="1" to="#ListLen(variables.setTemplate)#" index="index">

<cfquery name="setTemplate" datasource="#request.dsn#" username="#request.username#" password="#request.password#">
UPDATE #request.tbl_items#
SET item_template = <cfqueryparam value="#listGetAt(variables.setTemplate, index)#" cfsqltype="cf_sql_integer" />
WHERE item_id = <cfqueryparam value="#listGetAt(variables.setTemplateID, index)#" cfsqltype="cf_sql_integer" />
</cfquery>

</cfloop>


I can't see where I'm going wrong - any ideas?

Thanks,
Mikey.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 03, 2008 Mar 03, 2008
If you have a variable in the arguments scope, it is more efficient to just use it than to set an indentical one in the variables scope.

Having the same variable name as a query might be confusing Cold Fusion.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 03, 2008 Mar 03, 2008
Hi Dan,

I agree, normally I owuld just use the arguments scope but I didn't want to show all the invoke etc - just tried to make it simpler to explain. Either way it gives me the same error wether or not I use argument. or variables.

It's odd because it prints out the values fine, but when I use it in a query it says it can't find it. I will try changing the name of the query when I get back home tonight.

Thanks,
Mikey.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 03, 2008 Mar 03, 2008
LATEST
Kapitaine wrote:
> Hi Dan,
>
> I agree, normally I owuld just use the arguments scope but I didn't want to
> show all the invoke etc - just tried to make it simpler to explain. Either way
> it gives me the same error wether or not I use argument. or variables.
>
> It's odd because it prints out the values fine, but when I use it in a query
> it says it can't find it. I will try changing the name of the query when I get
> back home tonight.
>
> Thanks,
> Mikey.
>

I just wanted to point out, other then code documenting purposes,
<cfquery...> only needs a 'name' parameter if it is a select query.
Otherwise you can just leave it off if you desire.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources