I am passing dynamically named parameters in the url. (The number of sSearch parameters can go beyond 5 to 7 or 8 etc)
sSearch_0
sSearch_1
sSearch_2
sSearch_3
sSearch_4
sSearch_5
I want to run a loop to do a search within a query. I am trying like this:
<cfloop from="0" to="5" index="k">
<cfset counter = k>
<cfif IsDefined('url.sSearch_' & counter)>
<cfset "check_" & k = 'url.sSearch_' & counter>
</cfif>
</cfloop>
I am trying to write in a query like this:
<cfquery datasource="#coldfusionDatasource#" name="qFiltered">
SELECT *
FROM mytable
<cfif len(trim(url.sSearch))>
WHERE
<cfloop list="#listColumns#" index="thisColumn">
<cfif thisColumn neq listFirst(listColumns)> OR </cfif>
#thisColumn# LIKE <cfqueryparam cfsqltype="CF_SQL_VARCHAR"
value="%#trim(url.sSearch)#%" />
</cfloop>
</cfquery>
But it is not working. The error says check_ is undefined.
For dynamic variable naming using quotes, try:
<cfset "check_#k#" = 'url.sSearch_' & counter>
See this article
Related
I have a query-of-queries that performs a LIKE condition on a variable string:
When the variable contains a single word that includes a single quote, some results are returned, but not all:
<cfset _myVar = "Women's" />
<cfquery name="_qData" dbtype="Query">
SELECT
ID
FROM MyQoQ
WHERE NAME LIKE '%#_myvar#%'
OR DESCRIPTION LIKE '%#_myvar#%'
</cfquery>
When the variable contains more than one word, and one of those words includes a single quote, no records are returned:
<cfset _myVar = "Women's Initiative" />
<cfquery name="_qData" dbtype="Query">
SELECT
ID
FROM MyQoQ
WHERE NAME LIKE '%#_myvar#%'
OR DESCRIPTION LIKE '%#_myvar#%'
</cfquery>
I've tried PreserveSingleQuotes() as well as wrapping the varaibles with CFQUERYPARAM, but, to no avail - I get the same results.
Is there a way to make this work?
Adding in a repro case
<cfset myQuery = queryNew('hello')>
<cfset queryAddRow(myQuery,5)>
<cfset querySetCell(myQuery,"hello","what up",1)>
<cfset querySetCell(myQuery,"hello","what's up",2)>
<cfset querySetCell(myQuery,"hello","what's up friends",3)>
<cfset querySetCell(myQuery,"hello","what u",4)>
<cfset querySetCell(myQuery,"hello","what",5)>
<cfdump var="#myQuery#">
<cfquery name="res" dbtype="query">
SELECT *
FROM myQuery
WHERE hello LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%$what's up%">
</cfquery>
<cfdump var="#res#">
Railo 4.1.1.009 - returns both results (rows 2 and 3)
ColdFusion 10,0,13,287689 - returns no results
If I change my SQL to
WHERE hello LIKE '%what''s up%'
I still get no results
aarh!! a classic case of royal pain in the rear.
To solve this, you have to add an extra ' to every ' in your search term, there by escaping it.
<cfset myQuery = queryNew('hello')>
<cfset queryAddRow(myQuery,5)>
<cfset querySetCell(myQuery,"hello","what up",1)>
<cfset querySetCell(myQuery,"hello","what's up",2)>
<cfset querySetCell(myQuery,"hello","what's up friends",3)>
<cfset querySetCell(myQuery,"hello","what u",4)>
<cfset querySetCell(myQuery,"hello","what",5)>
<cfdump var="#myQuery#">
<cfset x = "what's up" />
<cfquery name="res" dbtype="query">
SELECT *
FROM myQuery
WHERE hello LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="#replace(x, "'", "''", "all")#%">
</cfquery>
<cfdump var="#res#">
if you do this directly, as you mentioned,
WHERE hello LIKE '%what''s up%'
the parser is going bonkers. But, if you pass the value via a function return value, the run time assignment of values via a variable somehow makes the parser happy.
I remember using such tricks in sending multiple SQL statements delimited with ';' in cfquery. Directly writing
"DECLARE x NUMBER; SELECT 2 INTO x FROM DUAL;"
inside cfquery fails, but assigning them to a string and then sending the string as a return value for any string manipulator function (lcase, ucase, etc) worked perfectly.
Note: the problem is solved, but if my explanation and approach is diff, feel free to correct and comment.
I'm running ColdFusion 10 u13.
Modifying your repo code this seemed to work:
<cfset myQuery = queryNew('hello')>
<cfset queryAddRow(myQuery,5)>
<cfset querySetCell(myQuery,"hello","what up",1)>
<cfset querySetCell(myQuery,"hello","what's up",2)>
<cfset querySetCell(myQuery,"hello","what's up friends",3)>
<cfset querySetCell(myQuery,"hello","what u",4)>
<cfset querySetCell(myQuery,"hello","what",5)>
<cfdump var="#myQuery#">
<cfquery name="res" dbtype="query">
SELECT *
FROM [myQuery]
WHERE [hello] LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%what''s up%">
</cfquery>
Note the double single quotes in the <cfqueryparam> tag. Like Dan I would have thought that the <cfqueryparam> tag would have taken care of this for you automatically. Perhaps this is a bug in QoQ?
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>
I am looping through the results of a query, and I need to limit the number of rows displayed. I need to use cfoutput because I'm using the group attribute, and I cannot use the maxrows because not all rows will be displayed.
I tried to use <cfbreak> inside the <cfoutput>, but that throws an error.
How can I break out of the <cfoutput> loop?
If your group by is only there to remove duplicates from your results I would suggest using your query to cut them down then you can cfloop (select distinct and reducing your returned column list).
If you are using your group by to "group" your results You could run a counter within your loop and a cfif statement inside your first loop to omit later results.
You could fake the group by option in your cfloop by matching value from previous row if you need cfbreak
<cfloop query="queryname">
<cfif queryname.column[currentrow-1] neq queryname.column[currentrow]>
#queryname.column#
</cfif>
</cfloop>
Random note: you can maxrows on any/all levels of your grouped cfoutput
<cfset tmp = querynew('id,dd')>
<cfloop from="1" to="20" index="i">
<cfset queryaddrow(tmp,1)>
<cfset querysetcell(tmp,'id',rand(),i)>
<cfset querysetcell(tmp,'dd',(i mod 4),i)>
</cfloop>
<cfquery dbtype="query" name="tmp">select * from tmp order by dd</cfquery>
<cfoutput query="tmp" group="dd" maxrows="2">#dd#<br
<ul>
<cfoutput maxrows="2" group="id"><li>#id#</li></cfoutput>
</ul>
</cfoutput>
You could use the cfthrow tag to trigger an exception that will allow you to break out of the loop using cfcatch you can then ignore the exception and continue processing. That will give you what you want.
<cftry>
<cfset i = 0>
<cfoutput query="qMyQuery" group="someGroup">
<cfset i = i + 1>
Parent
<cfoutput>
Child
</cfoutput>
<cfif i GTE 10>
<cfthrow type="break">
</cfif>
</cfoutput>
<cfcatch type="break">
<!--- DO NOTHING - THIS IS A HACK FOR NOT BEING ABLE TO USE CFBREAK inside cfoutput. --->
</cfcatch>
</cftry>