How do you use cfqueryparam on this line of code?
SET mailing_list = <CFIF IsDefined("FORM.mailing_list")>#FORM.mailing_list#<CFELSE>0</CFIF>
Like this:
SET mailing_list = <CFIF IsDefined("FORM.mailing_list")>
<cfqueryparam value="#FORM.mailing_list#" cfsqltype="cf_sql_integer" />
<CFELSE>
0
</CFIF>
On recent CFML engines, you can use the ternary conditional operator:
SET mailing_list = <cfqueryparam value="#IsDefined("FORM.mailing_list") ? FORM.mailing_list : 0#" cfsqltype="cf_sql_integer" />
(Though that is arguably less readable here.)
Best way is most likely to param it above your query and avoid any if altogether...
<cfparam name="Form.mailing_list" default="0" />
...
SET mailing_list = <cfqueryparam value="#FORM.mailing_list#" cfsqltype="cf_sql_integer" />
you would just use two separate cfqueryparam's
SET mailing_list =
<cfif StructKeyExists(form,'mailing_lis't)>
<cfqueryparam cfsqltype="cf_sql_varchar" value "#FORM.mailing_list#" />
<cfelse>
0
</cfif>
I generally use IIF for short IF statements:
SET mailing_list = <cfqueryparam value="#iif(IsDefined("form.mailing_list"), form.mailing_list, 0)#" cfsqltype="cf_sql_integer" />
Otherwise I prefer the "long and clean" way:
<CFIF IsDefined("FORM.mailing_list")>
SET mailing_list = <cfqueryparam value="#FORM.mailing_list#" cfsqltype="cf_sql_integer" />
<CFELSE>
SET mailing_list = <cfqueryparam value="0" cfsqltype="cf_sql_integer" />
</CFIF>
Related
The issue I am encountering is when a user is on the Spanish website, they are not able to see all results if their type the word incorrectly. Meaning, if their character does not have the accent above the character, the results will show blank.
For example, if the user types the word "Medico" without the accent on top of the "é", the search will return blank.
I am using a Mura, a CMS which the search is created. I was able to locate the function where it does search based on the user input. However, I am not able to clearly understand how the query is being implemented.
I am trying to modify the function so that it uses the collation, which I found an example on how to use on stackoverflow, which would handle the accents for Spanish or any other language(s).
I figure it should go after "where" and have a cfif statement where if it is the spanish site, to incorporate the collation.
<cfif #siteID# = "SpanishSite" />
//here would go the collation on keywords, titles, etc.
Note that the query is done in coldfusion:
<cffunction name="getPublicSearch" returntype="query" access="public" output="false">
<cfargument name="siteid" type="string" required="true">
<cfargument name="keywords" type="string" required="true">
<cfargument name="tag" type="string" required="true" default="">
<cfargument name="sectionID" type="string" required="true" default="">
<cfargument name="categoryID" type="string" required="true" default="">
<cfargument name="tagGroup" type="string" required="true" default="">
<cfset var rsPublicSearch = "">
<cfset var w = "">
<cfset var c = "">
<cfset var categoryListLen=listLen(arguments.categoryID)>
<cfquery attributeCollection="#variables.configBean.getReadOnlyQRYAttrs(name='rsPublicSearch',maxrows=1000)#">
<!--- Find direct matches with no releasedate --->
select tcontent.contentid,tcontent.contenthistid,tcontent.siteid,tcontent.title,tcontent.menutitle,tcontent.targetParams,tcontent.filename,tcontent.summary,tcontent.tags,
tcontent.restricted,tcontent.releaseDate,tcontent.type,tcontent.subType,
tcontent.restrictgroups,tcontent.target ,tcontent.displaystart,tcontent.displaystop,0 as Comments,
tcontent.credits, tcontent.remoteSource, tcontent.remoteSourceURL,
tcontent.remoteURL,tfiles.fileSize,tfiles.fileExt,tcontent.fileID,tcontent.audience,tcontent.keyPoints,
tcontentstats.rating,tcontentstats.totalVotes,tcontentstats.downVotes,tcontentstats.upVotes, 0 as kids,
tparent.type parentType,tcontent.nextn,tcontent.path,tcontent.orderno,tcontent.lastupdate, tcontent.created,
tcontent.created sortdate, 0 priority,tcontent.majorVersion, tcontent.minorVersion, tcontentstats.lockID, tcontentstats.lockType,
tcontent.expires,tfiles.filename as assocFilename, tcontentfilemetadata.altText as fileAltText,
CASE WHEN tcontent.title = <cfqueryparam cfsqltype="cf_sql_varchar" value="#renderTextParamValue(arguments.keywords)#">
or tcontent.menuTitle = <cfqueryparam cfsqltype="cf_sql_varchar" value="#renderTextParamValue(arguments.keywords)#"> THEN 0 ELSE 1 END AS superSort
from tcontent Left Join tfiles ON (tcontent.fileID=tfiles.fileID)
Left Join tcontent tparent on (tcontent.parentid=tparent.contentid
and tcontent.siteid=tparent.siteid
and tparent.active=1)
Left Join tcontentstats on (tcontent.contentid=tcontentstats.contentid
and tcontent.siteid=tcontentstats.siteid)
Left Join tcontentfilemetadata on (tcontent.fileid=tcontentfilemetadata.fileid
and tcontent.contenthistid=tcontentfilemetadata.contenthistid)
<cfif len(arguments.tag)>
Inner Join tcontenttags on (tcontent.contentHistID=tcontenttags.contentHistID)
</cfif>
where
(tcontent.Active = 1
AND tcontent.Approved = 1
AND tcontent.siteid = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.siteID#"/> )
AND
(
tcontent.Display = 2
AND
(
(tcontent.DisplayStart <= <cfqueryparam cfsqltype="cf_sql_timestamp" value="#now()#">
AND (tcontent.DisplayStop >= <cfqueryparam cfsqltype="cf_sql_timestamp" value="#now()#"> or tcontent.DisplayStop is null)
)
OR tparent.type='Calendar'
)
OR tcontent.Display = 1
)
AND
tcontent.type in ('Page','Folder','Calendar','File','Link','Gallery')
AND tcontent.releaseDate is null
<cfif len(arguments.sectionID)>
and tcontent.path like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#arguments.sectionID#%">
</cfif>
<cfif len(arguments.tag)>
and (
#renderTextParamColumn('tcontenttags.tag')# in (<cfqueryparam list="true" cfsqltype="cf_sql_varchar" value="#renderTextParamValue(arguments.tag)#"/> )
<cfif len(arguments.tagGroup) and arguments.tagGroup neq 'default'>
and #renderTextParamColumn('tcontenttags.taggroup')#=<cfqueryparam cfsqltype="cf_sql_varchar" value="#renderTextParamValue(arguments.taggroup)#"/>
</cfif>
)
<cfelse>
<!---
<cfloop list="#trim(arguments.keywords)#" index="w" delimiters=" ">
and
(tcontent.Title like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#w#%">
or tcontent.menuTitle like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#w#%">
or tcontent.metaKeywords like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#w#%">
or tcontent.summary like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#w#%">
or tcontent.body like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#w#%">)
</cfloop>
--->
and
(#renderTextParamColumn('tcontent.Title')# like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#renderTextParamValue(arguments.keywords)#%">
or #renderTextParamColumn('tcontent.menuTitle')# like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#renderTextParamValue(arguments.keywords)#%">
or #renderTextParamColumn('tcontent.metaKeywords')# like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#renderTextParamValue(arguments.keywords)#%">
or #renderTextParamColumn('tcontent.summary')# like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#renderTextParamValue(arguments.keywords)#%">
or (
tcontent.type not in ('Link','File')
and #renderTextParamColumn('tcontent.body')# like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#renderTextParamValue(arguments.keywords)#%">
)
or #renderTextParamColumn('tcontent.credits')# like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#renderTextParamValue(arguments.keywords)#%">
or tcontent.contenthistid in (
select distinct tcontent.contenthistid from tclassextenddata
inner join tcontent on (tclassextenddata.baseid=tcontent.contenthistid)
where tcontent.active=1
and tcontent.siteid=<cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.siteID#"/>
and #renderTextParamColumn('tclassextenddata.attributeValue')# like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#renderTextParamValue(arguments.keywords)#%">
))
</cfif>
and tcontent.searchExclude=0
<cfif categoryListLen>
and tcontent.contentHistID in (
select tcontentcategoryassign.contentHistID from
tcontentcategoryassign
inner join tcontentcategories
ON (tcontentcategoryassign.categoryID=tcontentcategories.categoryID)
where (<cfloop from="1" to="#categoryListLen#" index="c">
tcontentcategories.path like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#listgetat(arguments.categoryID,c)#%"/>
<cfif c lt categoryListLen> or </cfif>
</cfloop>)
)
</cfif>
#renderMobileClause()#
union all
<!--- Find direct matches with releasedate --->
select tcontent.contentid,tcontent.contenthistid,tcontent.siteid,tcontent.title,tcontent.menutitle,tcontent.targetParams,tcontent.filename,tcontent.summary,tcontent.tags,
tcontent.restricted,tcontent.releaseDate,tcontent.type,tcontent.subType,
tcontent.restrictgroups,tcontent.target ,tcontent.displaystart,tcontent.displaystop,0 as Comments,
tcontent.credits, tcontent.remoteSource, tcontent.remoteSourceURL,
tcontent.remoteURL,tfiles.fileSize,tfiles.fileExt,tcontent.fileID,tcontent.audience,tcontent.keyPoints,
tcontentstats.rating,tcontentstats.totalVotes,tcontentstats.downVotes,tcontentstats.upVotes, 0 as kids,
tparent.type parentType,tcontent.nextn,tcontent.path,tcontent.orderno,tcontent.lastupdate, tcontent.created,
tcontent.releaseDate sortdate, 0 priority,tcontent.majorVersion, tcontent.minorVersion, tcontentstats.lockID, tcontentstats.lockType,
tcontent.expires,tfiles.filename as assocFilename, tcontentfilemetadata.altText as fileAltText,
CASE WHEN tcontent.title = <cfqueryparam cfsqltype="cf_sql_varchar" value="#renderTextParamValue(arguments.keywords)#">
or tcontent.menuTitle = <cfqueryparam cfsqltype="cf_sql_varchar" value="#renderTextParamValue(arguments.keywords)#"> THEN 0 ELSE 1 END AS superSort
from tcontent Left Join tfiles ON (tcontent.fileID=tfiles.fileID)
Left Join tcontent tparent on (tcontent.parentid=tparent.contentid
and tcontent.siteid=tparent.siteid
and tparent.active=1)
Left Join tcontentstats on (tcontent.contentid=tcontentstats.contentid
and tcontent.siteid=tcontentstats.siteid)
Left Join tcontentfilemetadata on (tcontent.fileid=tcontentfilemetadata.fileid
and tcontent.contenthistid=tcontentfilemetadata.contenthistid)
<cfif len(arguments.tag)>
Inner Join tcontenttags on (tcontent.contentHistID=tcontenttags.contentHistID)
</cfif>
where
(tcontent.Active = 1
AND tcontent.Approved = 1
AND tcontent.siteid = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.siteID#"/> )
AND
(
tcontent.Display = 2
AND
(
(tcontent.DisplayStart <= <cfqueryparam cfsqltype="cf_sql_timestamp" value="#now()#">
AND (tcontent.DisplayStop >= <cfqueryparam cfsqltype="cf_sql_timestamp" value="#now()#"> or tcontent.DisplayStop is null)
)
OR tparent.type='Calendar'
)
OR tcontent.Display = 1
)
AND
tcontent.type in ('Page','Folder','Calendar','File','Link','Gallery')
AND tcontent.releaseDate is not null
<cfif len(arguments.sectionID)>
and tcontent.path like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#arguments.sectionID#%">
</cfif>
<cfif len(arguments.tag)>
and (
#renderTextParamColumn('tcontenttags.tag')# in (<cfqueryparam list="true" cfsqltype="cf_sql_varchar" value="#renderTextParamValue(arguments.tag)#"/> )
<cfif len(arguments.tagGroup) and arguments.tagGroup neq 'default'>
and #renderTextParamColumn('tcontenttags.taggroup')#=<cfqueryparam cfsqltype="cf_sql_varchar" value="#renderTextParamValue(arguments.taggroup)#"/>
</cfif>
)
<cfelse>
<!---
<cfloop list="#trim(arguments.keywords)#" index="w" delimiters=" ">
and
(tcontent.Title like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#w#%">
or tcontent.menuTitle like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#w#%">
or tcontent.metaKeywords like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#w#%">
or tcontent.summary like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#w#%">
or tcontent.body like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#w#%">)
</cfloop>
--->
and
(#renderTextParamColumn('tcontent.Title')# like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#renderTextParamValue(arguments.keywords)#%">
or #renderTextParamColumn('tcontent.menuTitle')# like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#renderTextParamValue(arguments.keywords)#%">
or #renderTextParamColumn('tcontent.metaKeywords')# like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#renderTextParamValue(arguments.keywords)#%">
or #renderTextParamColumn('tcontent.summary')# like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#renderTextParamValue(arguments.keywords)#%">
or
(
tcontent.type not in ('Link','File')
and #renderTextParamColumn('tcontent.body')# like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#renderTextParamValue(arguments.keywords)#%">
)
or #renderTextParamColumn('tcontent.credits')# like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#renderTextParamValue(arguments.keywords)#%">
or tcontent.contenthistid in (
select distinct tcontent.contenthistid from tclassextenddata
inner join tcontent on (tclassextenddata.baseid=tcontent.contenthistid)
where tcontent.active=1
and tcontent.siteid=<cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.siteID#"/>
and #renderTextParamColumn('tclassextenddata.attributeValue')# like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#renderTextParamValue(arguments.keywords)#%">
))
</cfif>
and tcontent.searchExclude=0
<cfif categoryListLen>
and tcontent.contentHistID in (
select tcontentcategoryassign.contentHistID from
tcontentcategoryassign
inner join tcontentcategories
ON (tcontentcategoryassign.categoryID=tcontentcategories.categoryID)
where (<cfloop from="1" to="#categoryListLen#" index="c">
tcontentcategories.path like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#listgetat(arguments.categoryID,c)#%"/>
<cfif c lt categoryListLen> or </cfif>
</cfloop>)
)
</cfif>
#renderMobileClause()#
ORDER BY supersort, priority, <cfif variables.configBean.getDBType() neq 'nuodb'>sortdate<cfelse>releasedate</cfif> desc
</cfquery>
<cfreturn rsPublicSearch />
My question is: Where should the collation go in the query above? Or is there a better approach than using collation in the query?
Or should I reach out to Mura and see if a new framework would need to be implemented to achieve what I am looking for?
Any help is appreciated
To expand on my comment.
const userInput = 'Médico';
const parsedInput = userInput.replace(/é/gmi, 'e').toLowerCase();
Then you take the that and store it in the database, so if the user searches for medico, Medico, MEDIcO, or any variant of that then it'll always be available to retrieve it from the database.
I can also write this in Spanish if it's easier for you to understand, but SO guidelines prefer English.
Here's a StackBlitz to kind of show you what I mean.
I am creating and assigning a value in a variable at the top of the function. But somehow the value gets replaced with other user's table record. This happened when two users doing transaction at same time with different records. Also it is happening only once in a while.
I have mentioned the issue in comments in below code.
<cffunction name="functionname" output="true">
<cfargument name="formData">
<cftransaction action="begin">
<cftry>
<cfquery name="getDoc_no" datasource="#variables.dsn#">
SELECT CONCAT(TO_CHAR(SYSDATE,'YYYY'),WORK_REQ_SEQ.NEXTVAL) as NEXT_NO FROM DUAL
</cfquery>
<cfset doc_no_seq=getDoc_no.NEXT_NO> <!---creating variable doc_no_seq --->
<cfquery name="getWo_no" datasource="#variables.dsn#">
SELECT WORK_ORDER_SEQ.nextval as NEXT_NO FROM DUAL
</cfquery>
<cfset Trns_seq=getWo_no.NEXT_NO> <!--- creating variable Trns_seq --->
<cfquery name="insert1" datasource="#variables.dsn#">
INSERT into WORK_ORDER
(
DOC_NO,
TRANS_ID
)
Values
(
<cfqueryparam value="#doc_no_seq#" cfsqltype="cf_sql_integer">, <!--- Inserted CORRECT doc_no_seq value --->
<cfqueryparam value="#Trns_seq#" cfsqltype="cf_sql_integer"> <!--- Inserted CORRECT Trns_seq value --->
)
</cfquery>
<cfif isdefined("IdnoRes") and IdnoRes gt 0>
<cfloop from="1" to="#IdnoRes#" index="q">
<cfif trim(Evaluate("rsc_grp_id_#q#")) is not "" and trim(Evaluate("qty_#q#")) is not "">
<cfquery name="insert2" datasource="#variables.dsn#">
INSERT into REQ_RESOURCE_DTLS
(
DOC_NO ,
TRANS_ID
)
Values
(
<cfqueryparam value="#doc_no_seq#" cfsqltype="cf_sql_integer">, <!--- Inserted WRONG doc_no_seq value --->
<cfqueryparam value="#Trns_seq#" cfsqltype="cf_sql_integer"> <!--- Inserted WRONG Trns_seq value --->
)
</cfquery>
</cfif>
</cfloop>
</cfif>
<cfif isdefined("Idnojob") and Idnojob gt 0>
<cfloop from="1" to="#Idnojob#" index="q">
<cfif trim(Evaluate("frm_des_#q#")) is not "" and trim(Evaluate("to_des_#q#")) is not "">
<cfquery name="insert3" datasource="#variables.dsn#">
INSERT into JOB_SEQ_DTLS
(
DOC_NO ,
TRANS_ID
)
Values
(
<cfqueryparam value="#doc_no_seq#" cfsqltype="cf_sql_integer">, <!--- Inserted WRONG doc_no_seq value --->
<cfqueryparam value="#Trns_seq#" cfsqltype="cf_sql_integer"> <!--- Inserted WRONG Trns_seq value --->
)
</cfquery>
</cfif>
</cfloop>
</cfif>
<cftransaction action="commit" />
<cfcatch type="any">
<cftransaction action="rollback" />
</cfcatch>
</cftry>
</cftransaction>
<cfreturn 1>
How do you create a cfqueryparam for a dynamic variable? How do I write the value for something like this..
Dynamic Session Variables:
<cfset session.checkout.info["firstname_#Add#"]=form["firstname_#Add#"]>
<cfset session.checkout.info["middlename_#Add#"]=form["middlename_#Add#"]>
<cfset session.checkout.info["lastname_#Add#"]=form["lastname_#Add#"]>
Database Entry:
<cfquery datasource="Titlesbymail" name="InsertEntry">
INSERT INTO owner (firstname, middlename, lastname)
VALUES (<cfqueryparam value="#session.checkout.info.firstname#" cfsqltype="CF_SQL_VARCHAR">
<cfqueryparam value="#session.checkout.info.middlename#" cfsqltype="CF_SQL_VARCHAR">,
<cfqueryparam value="#session.checkout.info.lastname#" cfsqltype="CF_SQL_VARCHAR">
</cfquery>
I believe you just do it like this :
<cfloop index="Add" from="1" to="#session.checkout.quantity.pcount#" step="1">
<cfif structKeyExists(session, "checkout")>
<cfquery datasource="Titlesbymail" name="InsertEntry">
INSERT INTO owner (firstname, middlename, lastname)
VALUES (
<cfqueryparam value='#form["firstname_#Add#"]#' cfsqltype='cf_sql_varchar' />
, <cfqueryparam value='#form["middlename_#Add#"]#' cfsqltype='cf_sql_varchar' />
, <cfqueryparam value='#form["lastname_#Add#"]#' cfsqltype='cf_sql_varchar' />
</cfquery>
</cfif>
</cfloop>
Please consider the code below:
I am getting JSON data based on the date range I am specifying on the url & I am deserializing it as folows:
<cfhttp url="#urladdress#" method="GET" resolveurl="Yes" throwonerror="Yes">
<cfset cfData=DeserializeJSON(CFHTTP.FileContent)>
<cfset arraylen = ArrayLen(cfdata)>
I am grabbing the data and inserting into MySQL database as follows:
<cfquery datasource="XX.X.XX.XXX" name="qCoulmnInsert">
INSERT INTO Mytable
(aol_int,
android_Phone_int,
androidTablet_int,
apple_mail_int,
blackberry_int,
Eudora_int,
gMail_int,
Hotmail_int,
lotus_notes_int,
other_int,
other_webmail_int,
Outlook_int,
Postbox_int,
sparrow_int,
thunderbird_int,
windowsLiveMail_int,
yahoo_int,
iPad_int,
iphone_int,
iPod_int,
<!--- FOR DATES --->
startdate_dt,
enddate_dt,
date_dt)
VALUES
<!--- loop through your array --->
<cfloop from="1" to="#arrayLen(cfData)#" index="i">
( <!--- 1 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["aol"]#">
<cfelse>
NULL
</cfif>,
<!--- 2 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Android Phone"]#">
<cfelse>
NULL
</cfif>,
<!--- 3 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Android Tablet"]#">
<cfelse>
NULL
</cfif>,
<!--- 4 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Apple Mail"]#">
<cfelse>
NULL
</cfif>,
<!--- 5 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Blackberry"]#">
<cfelse>
NULL
</cfif>,
<!--- 6 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Eudora"]#">
<cfelse>
NULL
</cfif>,
<!--- 7 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["gmail"]#">
<cfelse>
NULL
</cfif>,
<!--- 8 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["hotmail"]#">
<cfelse>
NULL
</cfif>,
<!--- 9 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Lotus Notes"]#">
<cfelse>
NULL
</cfif>,
<!--- 10 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Other"]#">
<cfelse>
NULL
</cfif> ,
<!--- 11 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Other Webmail"]#">
<cfelse>
NULL
</cfif>,
<!--- 12 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Outlook"]#">
<cfelse>
NULL
</cfif> ,
<!--- 13 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Postbox"]#">
<cfelse>
NULL
</cfif> ,
<!--- 14 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Thunderbird"]#">
<cfelse>
NULL
</cfif> ,
<!--- 15 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Sparrow"]#">
<cfelse>
NULL
</cfif> ,
<!--- 16 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Windows Live Mail"]#">
<cfelse>
NULL
</cfif> ,
<!--- 17 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Yahoo"]#">
<cfelse>
NULL
</cfif> ,
<!--- 18 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["iPad"]#">
<cfelse>
NULL
</cfif> ,
<!--- 19 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["iPhone"]#">
<cfelse>
NULL
</cfif> ,
<!--- 20 --->
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["iPod"]#">
<cfelse>
NULL
</cfif> ,
<!--- FOR DATES --->
<cfif structKeyExists(cfData[i], "startdate_dt")>
<cfqueryparam CFSQLTYPE="CF_SQL_DATE" value="#cfData[i].startdate_dt#">
<cfelse>
NULL
</cfif>,
<cfif structKeyExists(cfData[i], "enddate_dt")>
<cfqueryparam CFSQLTYPE="CF_SQL_DATE" value="#cfData[i].enddate_dt#">
<cfelse>
NULL
</cfif>,
<cfif structKeyExists(cfData[i], "date")>
<cfqueryparam CFSQLTYPE="CF_SQL_DATE" value="#cfData[i].date#">
<cfelse>
NULL
</cfif>
)
<cfif i neq arrayLen(cfData)>,</cfif>
</cfloop>
</cfquery>
Problem I am facing:
When the JSON data does not has "Lotus Notes" in it, I get an error "Element Lotus Notes is undefined in a CFML structure referenced as part of an expression. "
an points to the line <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Lotus Notes"]#">
When it has Lotus Notes in the JSON , it picks up some other element and I got the error "Element Eudora is undefined in a CFML structure referenced as part of an expression. "
and points to the line <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Eudora"]#"> in above code
Here is the sample JSON with Lotus Notes on date Jan 01
and here it is without Lotus Notes (from Jan 02) field where I get Eudora error
I don't understand where I am wrong. Please advise
You only need a single check for whether cfData[i].Open exists - if it doesn't all those ints are going to be null, so just output 20 nulls. (Or perhaps don't bother with the insert at all - depends what's going on here).
Also, you don't need explicit hard-coded logic for every single one - just use a loop and check the existence inside the loop with a single piece of code - i.e:
<cfif StructKeyExists(cfData[i],'open')>
<cfloop index="Key" array=#['aol','Android Phone','Android Tablet','etc']# >
,
<cfif StructKeyExists( cfData[i].open , Key ) >
<cfqueryparam value=#cfData[i].open[Key]# cfsqltype="cf_sql_integer" />
<cfelse>
<cfqueryparam value="" null=true />
</cfif>
</cfloop>
<cfelse>
#RepeatString( ',null' , 20 )#
</cfif>
I don't understand why you don't check before you reference. This is how I'd write it.
<!--- 1 --->
<cfset data = cfData[i]>
<cfset isVarDefined = isDefined("variables.data.open.aol")>
<cfset value = isVarDefined ? data.open.aol : "">
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#value#" null="#!isVarDefined#">
<!--- 2 --->
<!--- very similar to the code above... --->
I get a query of all the files (images) in a directory
<cfset local.teamBase = "#GetDirectoryFromPath(GetBaseTemplatePath())#teamlogo\">
<cfdirectory action="LIST" directory="#local.teamBase#" name="rc.qryTeamLogo">
I later loop over all these filenames, skipping those that are in use
<select name="Logo" <cfif deleted>disabled="disabled"</cfif>>
<option></option>
<cfloop query="rc.qryTeamLogo">
<cfif name EQ mylogo>
<option value="#name#" selected>#name#</option>
<cfelseif request.objTeam.isUsed(name) EQ 1 OR name EQ "thumbs.db">
<!--- Do not show --->
<cfelse>
<option value="#name#">#name#</option>
</cfif>
</cfloop>
</select>
The isUsed() function looks like
<cffunction name="isUsed" returntype="boolean" output="false">
<cfargument name="logo" required="true" type="string">
<cfquery name="qryUsed" datasource="#application.DSN#">
SELECT logo
FROM CCF.team
WHERE logo = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.logo#">
AND Deleted = 0
</cfquery>
<cfif qryUsed.recordcount EQ 0>
<cfreturn false>
</cfif>
<cfreturn true>
</cffunction>
The problem with this is as more and more files get added, this gets slower and slower.
What I would really like a single query that can assemble the whole thing
You can run one query before your select box
<cfquery name="qryLogos" datasource="#application.DSN#">
SELECT logo
FROM CCF.team
WHERE logo IN (<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#valueList(rc.qryTeamLogo.name)#" list="true">)
AND Deleted = 0
</cfquery>
Then set all those logo's into an array and add thumbs.db too, so you have one less check to do.
<cfset allLogs = listToArray(valueList(qryLogos.logo))>
<cfset arrayAppend(allLogs,'thumbs.db')>
and then change your elseif to this
<cfelseif arrayFind(allLogs,name)>
<!--- don't display --->
Load the file information into XML. Shread the XML into a table and JOIN on that.
<cfset local.teamBase = "#GetDirectoryFromPath(GetBaseTemplatePath())#teamlogo\">
<cfset local.qryTeamLogo = DirectoryList(local.teamBase, false, "query")>
<cfsavecontent variable="local.xmlTeamLogo">
<ul class="xoxo">
<cfoutput query="local.qryTeamLogo">
<li><code>#xmlformat(name)#</code></li>
</cfoutput>
</ul>
</cfsavecontent>
<cfquery name="local.qryResult">
DECLARE #xmlTeamLogo xml = <cfqueryparam cfsqltype="CF_SQL_varchar" value="#local.xmlTeamLogo#">
SELECT Value AS Name
FROM dbo.udfXOXORead(#xmlTeamLogo)
WHERE NOT Value IN (
SELECT Logo
FROM CCF.Team WITH (NOLOCK)
WHERE Deleted = 0
)
</cfquery>
The Table Valued Function
create function [dbo].[udfXOXORead](#xmlData xml)
returns #tblmessage TABLE (
[value] nvarchar(max) NULL
)
as
begin
INSERT INTO #tblMessage
SELECT Tbl.Col.value('(code)[1]', 'nvarchar(max)') AS [value]
FROM #xmlData.nodes('/ul/li') Tbl(Col)
RETURN
end