How would I create an array that will return data in the following format via CF 8?
This information originates from an order table based on SKU value and QTY. I already know the query to use to pull the data. I just would like some help to format it.
The original data exists in the following format
SKU82328 QTY 1
SKU9832 QTY 3
SKU8923 QTY 1
skulist=SKU82328,SKU9832,SKU8923&quantitylist=1,3,1
<cfquery name"SkuQuery" datasource="DSN">
SELECT sku, quantity FROM someTable WHERE someCondition = 'true'
</cfquery>
<cfset SkuList = ValueList(SkuQuery.sku)>
<cfset QuantityList = ValueList(SkuQuery.quantity)>
<cfset QueryString = "skulist=#URLEncodedFormat(SkuList)#&quantitylist=#URLEncodedFormat(QuantityList)#">
I think you would have to do something like below
<!--- Do the query --->
<cfquery name="test" datasource="cfsnippets">
SELECT Emp_ID, LastName, FirstName, Email
FROM Employees
</cfquery>
<!--- Declare the array --->
<cfset myarray=arraynew(2)>
<!--- Populate the array row by row --->
<cfloop query="test">
<cfset myarray[CurrentRow][1]=Emp_ID>
<cfset myarray[CurrentRow][2]=LastName>
<cfset myarray[CurrentRow][3]=FirstName>
<cfset myarray[CurrentRow][4]=Email>
</cfloop>
<!--- Now, create a loop to output the array contents --->
<cfset total_records=test.recordcount>
<cfloop index="Counter" from=1 to="#Total_Records#">
<cfoutput>
ID: #MyArray[Counter][1]#,
LASTNAME: #MyArray[Counter][2]#,
FIRSTNAME: #MyArray[Counter][3]#,
EMAIL: #MyArray[Counter][4]# <br>
</cfoutput>
</cfloop>
Related
I want to get the total number of records that a column has for a string.
For example, seller column = 'ONA'. I want to know how many records are equal to "ONA" and how many are not.
This is what I have:
<cfset stringONA= "ONA">
<CFSET onaseller= 0>
<CFSET notseller=0>
<cfloop query="getunion_again">
<cfif #getunion_again.seller# eq stringONA>
<cfset onaseller = onaseller +1 >
<P>TEST</P>
<cfelse>
<cfset notseller = notseller +1>
</cfif>
</cfloop>
<cfdump var=#onaseller #>
<cfdump var=#notseller #>
I'm not getting any errors, just not getting any count.
Wouldn't it just be easier to do a query-of-query to get the number of records that match your criteria without looping?
<cfquery name="qryCount" dbtype="query">
SELECT COUNT(*) AS positive_count
FROM getunion_again
WHERE seller = <cfqueryparam cfsqltype="cf_sql_varchar" value="#stringONA#">
</cfquery>
<cfset onaseller = qryCount.positive_count>
<cfset notseller = getunion_again.recordcount - onaseller>
One can use listValueCountNoCase() to count the matches in a list, and one can use valueList() to extract a query column as a list. And the number of mismatches is the number of rows less the number of matches.
So:
values = valueList(getunion_again.seller);
onaseller = listValueCountNoCase(values, stringONA);
notseller = getunion_again.recordCount - onaseller;
I am getting the following details in my form. I need to loop through the fields which have "attachment" in the name and add the count to the table, and also the values in the new table.
ATTACHMENT C:\ColdFusion10\cfusion\runtime\work\Catalina\localhost\tmp\neotmp3230094756217875313.tmp
ATTACHMENT2 C:\ColdFusion10\cfusion\runtime\work\Catalina\localhost\tmp\neotmp4341408903737742616.tmp
ATTACHMENT3 C:\ColdFusion10\cfusion\runtime\work\Catalina\localhost\tmp\neotmp2809169853442728277.tmp
I am trying to do a loop over the form fields, but I am lost in between, and am unsure where to proceed.
<CFLOOP collection="#structform#" item="whichField">
<cfif FindNoCase('attachment',whichField)>
<cfset total = len(whichField)>
<cfoutput>#total#</cfoutput><br><br>
</cfif>
<CFOUTPUT>#whichField# = #structform[whichField]#</CFOUTPUT><br>
</CFLOOP>
I'm still not certain I've understood what's going on, so this might need tweaking/changing to make it work as needed, but this is without doubt a better approach:
<cfset Total = 0 />
<cfloop collection=#StructForm# item="FieldName" >
<cfif findNoCase('attachment',FieldName) AND len(StructForm[FieldName]) >
<cfset ++Total />
</cfif>
</cfloop>
<CFLOOP collection="#structform#" item="whichField">
<cfif FindNoCase('attachment',whichField)>
<cfset lstvalue = ListAppend(lstvalue,whichField)>
</cfif>
</CFLOOP>
<cfset total = ListLen(lstvalue)>
<cfif (len(total) AND total EQ 1) AND (Evaluate(lstvalue) EQ '')>
<cfset total = 0>
</cfif>
I am trying to set a variable dynamically into a structure via CFLOOP. I have read Ben Nadal's blog post but cant seem to get the assignment correct. I would like to use dot notation and make the VIN a sub structure of values.
Here is my code:
<cfloop query="VINs">
<cfquery name="carsQue" datasource="carsData">
SELECT VIN, MODEL, MAKE, etc
FROM CarsDB
WHERE (VIN = #VIN#)
</cfquery>
<cfset carsStruct= StructNew()>
<cfset carsStruct.[VIN].MAKE = '#carsQue.MODEL#'>
<cfset carsStruct.[VIN].MODEL = '#carsQue.MAKE#'>
</cfloop>
Any guidance would be greatly appreciated,
Thanks
Running a query inside a loop is almost always a bad idea. In your case, a better option would be:
<cfif ListLen(valuelist(vins.vin)) gt 0>
<cfquery name=CarsQue datasource = "carsData">
select vin, model, make, etc
from carsDB
where vin in (<cfqueryparam cfsqltype="cf_sql_varchar"
value="#valuelist(vins.vin)#" list="true">)
</cfquery>
<cfset carsStruct = StructNew()>
<cfloop query="carsQue">
code for your struct
</cfloop>
<cfelse>
code for vins query returning no data
</cfif>
Better yet would be to get all the data with one query. You didn't provide enough information to determine if this was possible, but it often is.
Create a structure outside loop and and setting variable within loop can solve the problem. in a current scenario each time loop run its create a new structure.
you can do some thing like this
<cfset carsStruct= StructNew()>
<cfloop query="VINs">
<cfquery name="carsQue" datasource="carsData">
SELECT VIN, MODEL, MAKE, etc
FROM CarsDB
WHERE VIN = <cfqueryparam cf_sql_type="cf_sql_varchar" value="#VINs.VIN#">
</cfquery>
<cfset carsStruct[VINs.VIN].MAKE = carsQue.MODEL>
<cfset carsStruct[VINs.VIN].MODEL = carsQue.MAKE>
</cfloop>
Based on the limited information you've given you should be able to run one query and loop through that to add to your struct.
<cfset carsStruct= {}> //new struct
<cfif VINs.RecordCount> //your VINs query has records
<cfquery name="carsQueue" datasource="carsData">
SELECT VIN, MODEL, MAKE, etc
FROM CarsDB
// Quoted list of all your VINs. cfqueryparam prevents against SQL injection
WHERE VIN IN (<cfqueryparam cf_sql_type="cf_sql_varchar" value="#ValueList(VINs.VIN)#" list="true">
</cfquery>
<cfloop query="carsQueue">
<cfset carsStruct.[carsQueue.VIN].MAKE = carsQueue.MODEL>
<cfset carsStruct.[carsQueue.VIN].MODEL = carsQueue.MAKE>
</cfloop>
<cfelse>
// if VINs query return nothing a blank struct will be returned.
//You do NOT need this <cfelse> unless you are returning something when the query is blank
</cfif>
If FF firebug window, I see this is being passed to my coldfusion action page:
RowOrder[]=&RowOrder[]=row_5&RowOrder[]=row_2&RowOrder[]=row_1&RowOrder[]=row_3&RowOrder[]=row_4&RowOrder[]=row_6&RowOrder[]=row_7&RowOrder[]=row_8&RowOrder[]=row_11
Now I need to loop over this to get the updated sort order but due to the [], I'm having issues. How can I loop over this so that I can update my table??? I expected this to be the easy part but I'm obviously missing something.
+ I'm using the jquery plugin (http://www.isocra.com/2008/02/table-drag-and-drop-jquery-plugin/). +
Here is the code I'm using to loop over the submitted data:
<cfif StructKeyExists(form, "RowOrder")>
<!---<cfset variables.Order = ReReplaceNoCase(form.RowOrder, "(&){0,1}row_\[\]=", ",", "all") />--->
<cfset variables.Order = ReplaceNoCase(form["RowOrder[]"],"row_","","all")>
<cfloop from="1" to="#ListLen(variables.Order)#" index="index">
<cfquery name="qryOrder" datasource="#dsn#">
update SystemTypes
set Order = <cfqueryparam value="#index#" cfsqltype="cf_sql_integer" />
where WETypeNum = <cfqueryparam value="#ListGetAt(variables.Order, index)#" cfsqltype="cf_sql_integer" />
</cfquery>
</cfloop>
</cfif>
+ The ajax code I'm using is as follows:
$("#RowOrder").tableDnD({
onDrop: function(table, row) {
var RowOrderData = $.tableDnD.serialize();
$.ajax({
type: 'POST',
url: '../../ajax/UpdateListingOrder.cfm',
cache: false,
data: RowOrderData
});
}
});
+
The + indicates information that was added after posting question
How is the problem manifesting itself? Are you getting an error, the query is not executing, .. ?
Dump the FORM scope to verify the name of field being passed? It looks like FORM['RowOrder[]'] rather than form.RowOrder. In which case you would need to use:
<cfif StructKeyExists(form, "RowOrder[]")>
<cfset variables.Order = ReReplaceNoCase(form["RowOrder[]"], "(&){0,1}row_\[\]=", ",", "all") />
... rest of code ...
<cfelse>
oops, that variable name does not exist
</cfif>
Could you not match the number rather than replace?
I've done the regex match within the loop - You can then focus on just one part of the string at a time.
An an illustration of the approch:
<cfif structKeyExists(form, "rowOrder")>
<cfset data = listToArray(form.rowOrder, "&")/>
<cfif not arrayIsEmpty(data)>
<cfloop form="1" to="#arrayLen(data)#" index="index">
<cfset match = reFind("[1-9][0-9]+", data[index], 1, true)/>
<cfif arraySum(match)>
<cfset weTypeNumber = val(mid(data[index], match["pos"], match["len"]))/>
<cfquery name="qryOrder" datasource="#dsn#">
UPDATE
system_types
SET
order = <cfqueryparam cfsqltype="cf_sql_integer" value="#index#"/>
WHERE
we_type_num = <cfqueryparam cfsqltype="cf_sql_integer" value="#weTypeNumber#"/>
</cfquery>
</cfif>
</cfloop>
</cfif>
</cfif>
Looking for a way to track the last 5 product ids in a cookie via coldfusion. I'll set a new id each time a product page is visited. The product ids could be stored in a comma separate list. How would I store only the last 5 product ids?
You could try something like this (untested):
<cfset thisproductid = id_you_somehow_know_from_this_page>
<cfif not structkeyexists( cookie, 'mylist' )>
<!--- no cookie? make one and set it to this ID --->
<cfcookie name="mylist" value="#thisproductid#">
<cfelse>
<cfif listlen( cookie.mylist ) eq 5>
<!--- lifo --->
<cfset cookie.mylist = listdeleteat( cookie.mylist, 1 )>
<cfelse>
<!--- check for odd conditions like listlen gt 5? up to you --->
</cfif>
<cfset cookie.mylist = listappend( cookie.mylist, thisproductid )>
</cfif>