ColdFusion RecordCount as multiple conditional - coldfusion

I would like to run one query + output and in case no records exist run second query + output and in case both have no records then redirected to
<test1.RecordCount eq 0>
but the problem is that I can only use one <cfelse>
Any idea?
The Code for one query + output and RecordCount
<cfif test1.RecordCount eq 0>
<!--- Display some message.--->
<cfelse>
<cfoutput query="test1">
<!--- Display some other message --->
</cfoutput>
</cfif>

If I understand your question correctly, you should be able to use nested <cfif ...> conditions.
Something like:
<cfif test1.RecordCount gt 0>
<cfoutput query="test1">
<!--- Display test1 query results --->
</cfoutput>
<cfelse>
<cfif test2.RecordCount gt 0>
<cfoutput query="test2">
<!--- Display test2 query results --->
</cfoutput>
<cfelse>
<!--- Display some message.--->
</cfif>
</cfif>
Or you could use <cfelseif> like this:
<cfif test1.RecordCount gt 0>
<cfoutput query="test1">
<!--- Display test1 query results --->
</cfoutput>
<cfelseif test2.RecordCount gt 0>
<cfoutput query="test2">
<!--- Display test2 query results --->
</cfoutput>
<cfelse>
<!--- Display some message.--->
</cfif>

Related

CFspreadsheet adding a % to format a column

I am using CF10 and trying to add a % sign to a spreadsheet.
I am having an issue with adding a % sign on my 5th column entries. (Only the entries starting the second row (not the header) and not the blank cells when the query is done running.
<cftry>
<cfset objSpreadsheet = SpreadsheetNew()>
<cfset assocRows = ''>
<!--- Create and format the header row. --->
<cfset SpreadsheetAddRow( objSpreadsheet, "Associate Name,Location,Checklists Generated by Associate,Checklists Generated by Selected Location(s),Associate Percentage of Location Total" )>
<cfset rowNumber = 1 />
<cfoutput query="GetEmployeeInfo">
<cfset rowNumber++ />
<cfset rowList = "'#(rnA eq 1)?assoc_name:''#','#(rnl eq 1)?trans_location:''#','#checklistsByAssocLoc#','#assocChecklistsByLoc#','#DecimalFormat(totalChecklistsByAssocLocPct)#'">
<!--- Make list of rows --->
<cfif (rnA eq 1)>
<cfset assocRows = ListAppend(assocRows, rowNumber)>
</cfif>
<cfset SpreadsheetAddRow( objSpreadsheet, rowList)>
<cfif rnTotAssoc EQ 1>
<cfset rowNumber++ />
<cfset rowList = "'Associate Total','','#totalChecklistsByAssoc#','#totalAssocChecklistsByAllFilteredLoc#','#DecimalFormat(totalChecklistsByLocPct)#'" >
<cfset SpreadsheetAddRow( objSpreadsheet, rowList )>
</cfif>
</cfoutput>
<cfset SpreadSheetSetColumnWidth(objSpreadsheet,1,25)>
<cfset SpreadSheetSetColumnWidth(objSpreadsheet,2,25)>
<cfset SpreadSheetSetColumnWidth(objSpreadsheet,3,25)>
<cfset SpreadSheetSetColumnWidth(objSpreadsheet,4,25)>
<cfset SpreadSheetSetColumnWidth(objSpreadsheet,5,25)>
<!--- Move the line here --->
<cfset SpreadsheetFormatRow( objSpreadsheet, {bold=true, textwrap="true", alignment="center"}, 1 )>
<cfloop list="#assocRows#" index="i">
<cfset SpreadsheetFormatCell(objSpreadsheet, {'bold' : 'true'}, i, 1)>
</cfloop>
<cfheader name="Content-Disposition" value="inline; filename=CS_#Dateformat(NOW(),'MMDDYYYY')#.xls">
<cfcontent type="application/vnd.ms-excel" variable="#SpreadsheetReadBinary( objSpreadsheet )#">
<cfcatch type = "any">
#rowList#
<cfabort>
</cfcatch>
</cftry>
I have tried adding:
'#DecimalFormat(totalChecklistsByLocPct)# %'"
which then excel doesnt know the correct format.
Then I tried:
<cfset spreadsheetFormatCell( objSpreadsheet, {dataformat: "0.00%"}, 2 )>
Which then just through an error:
Parameter validation error for the SPREADSHEETFORMATCELL function.
Any help on how to add this % sign to the 5 column (not the header or blank cells once the query stops) would be greatly appreciated.
EDIT
Also tried:
<cfset rowList = "'#(rnA eq 1)?assoc_name:''#','#(rnl eq 1)?trans_location:''#','#checklistsByAssocLoc#','#assocChecklistsByLoc#','#NumberFormat(totalChecklistsByAssocLocPct, '0.00')# %'">
Excel still through the error saying its formatted as text.

ColdFusion cfloop skip row or change loop endRow

I am trying to display 2 images, and skip one if it matches the following criteria (<cfif myDir.name eq property_mainimage> which works fine, and then continue onto the next image and display the next image until I have a total of 2 images. But when the criteria image is say in row 5, the loop displays 3 images, when the criteria image is say row 1, it displays 2 images.
There doesn't seem to be a skip row in cfloop?
I have tried <cfif counter gt 3 > <cfabort> which works but it displays the 3 images still of the criteria image is in a row greater than 3. Below is what i'm working with...
<cfdirectory directory="C:\Domains\domain.com\wwwroot\uploads\images\#property_ID#\" filter="*.jpg" name="myDir" type="file" sort="datelastmodified">
<ul>
<cfset counter = 0>
<cfset endrowvalue = 3 >
<cfloop query="myDir" endRow="#endrowvalue#">
<cfset counter = counter +1 >
<li>
<cfif right(myDir.name,4) is ".jpg">
<cfif fileexists("C:\Domains\ domain.com\wwwroot\uploads\ images\#property_ID#\#left(mydir.name,len(myDir.name)-4)#.jpg")>
<cfif myDir.name eq property_mainimage>
DO NOT SHOW THIS IMAGE
<cfelse>
<cfset endrowvalue = 1 >
<cfset myDir.endRow = 1 >
test #endrowvalue#
test #counter# <img src="#request.root#uploads/images/#property_ID#/#myDir.name#" border="0" width="230px" style="margin-bottom:15px;" />
</cfif>
</cfif>
</cfif>
</li>
</cfloop>
</ul>
any help would be most appreciated.
I would use a counter to count when you show the image. Once you get to 2 images displayed break out of the loop.
<cfdirectory directory="C:\Domains\domain.com\wwwroot\uploads\images\#property_ID#\" filter="*.jpg" name="myDir" type="file" sort="datelastmodified">
<ul>
<cfset counter = 1>
<cfloop query="myDir">
<li>
<cfif right(myDir.name,4) is ".jpg">
<cfif fileexists("C:\Domains\ domain.com\wwwroot\uploads\ images\#property_ID#\#left(mydir.name,len(myDir.name)-4)#.jpg")>
<cfif myDir.name eq property_mainimage>
DO NOT SHOW THIS IMAGE
<cfelse>
<!--- image displayed increment coutner --->
<cfset counter++>
<img src="#request.root#uploads/images/#property_ID#/#myDir.name#" border="0" width="230px" style="margin-bottom:15px;" />
</cfif>
</cfif>
</cfif>
<cfif counter EQ 2>
<cfbreak>
</cfif>
</li>
</cfloop>
</ul>
This is a formatted comment. Your code has this:
<cfset endrowvalue = 3 >
<cfloop query="myDir" endRow="#endrowvalue#">
<cfif myDir.name eq property_mainimage>
DO NOT SHOW THIS IMAGE
<cfelse>
<cfset endrowvalue = 1 >
You would have to test it to be sure, but I suspect that the endRow attribute of your cfloop tag will continue to be 3, even if you change the value of the endrowvalue variable to 1.
This might not be the cause of your current problem, but, it my suspicians are correct, you will either have a problem elsewhere or you are running a pointless command.

How to compare two images in ColdFusion

I am trying to compare images and find if they are same or not. Images can have same name but the actual image might be different. The code that I have so far.
<cfset dirToReadFrom = #ExpandPath( '../properties-feed/unzipped/' )# />
<cfdirectory
action="list"
directory="#dirToReadFrom#"
listinfo="name"
name="qFile"
sort="asc"
filter="*.jpg"
/>
<cfset images = ArrayNew(1)>
<cfoutput query="qFile">
<cfset ArrayAppend(images, #qFile.name#)>
</cfoutput>
<cfset dirToCreate = #ExpandPath( './assets/images/resized/original/' )# />
<cfif not DirectoryExists(dirToCreate)>
<cfdirectory action = "create" directory = "#dirToCreate#" />
<cfoutput><p>Your directory has been created.</p></cfoutput>
</cfif>
<cfzip
action="unzip"
file="#ExpandPath( '../properties-feed/data.zip/' )#"
destination="#ExpandPath( './assets/images/resized/original/' )#"
overwrite="true"
/>
<cfset dirToReadFromOriginal = #ExpandPath( './assets/images/resized/original/' )# />
<cfdirectory
action="list"
directory="#dirToReadFromOriginal#"
listinfo="name"
name="qFileOriginal"
sort="asc"
filter="*.jpg"
/>
<cfset imagesLatest = ArrayNew(1)>
<cfoutput query="qFileOriginal">
<cfset ArrayAppend(imagesLatest, #qFileOriginal.name#)>
</cfoutput>
<!--- Loop over your current images --->
<cfloop query="qFileOriginal">
<!--- Check for a matching file name --->
<cfquery name="fileExists" dbtype="query">
SELECT
COUNT(*) AS num_Rec
FROM
qfile
WHERE
name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#qFileOriginal.name#" />
</cfquery>
<!--- do we have a matching file name? --->
<cfif val(fileExists.num_rec)>
<cfimage action="read" name="newImage" source="#dirToReadFrom##qFile.name#"/>
<cfimage action="read" name="originalImage" source="#dirToReadFromOriginal##qFileOriginal.name#"/>
<cfset newImageBlob = ImageGetBlob(newImage) />
<cfset originalImageBlob = ImageGetBlob(originalImage) />
<!--- Compare --->
<cfif toString(newImageBlob) eq toString(originalImageBlob) >
Images are same
<cfelse>
DIFFERENT
</cfif>
</cfif>
</cfloop>
The code doesn't seem to be working. Can Anyone see what am I doing wrong?
Update 1 from comments
The result that I actually get is that the first images are same and the rest of images in files are different. But this is not correct as most of the images that I am comparing are same.
Update 2 from comments
It incorrectly identifies same images as being different. What I actually get is that the first two images are same and the rest is different. Which is not right as most of the images I have are same.
I've always just done this with BinaryEncode(), and then compare the resulting strings. You have to be careful though, as compression can make the files different even though they look (to the eye) exactly the same.

Finding images not referenced in a DB table

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

Add Page Numbers to Paginated Data

I have implemented pagination to my data, but the problem is I only have the Next and Previous links, like so:
What I would like to do is add page numbers, with the page number I am on as normal text, and the other pages as links. So if I am on page 3 of 5, it would like look like this:
Here is what I have so far:
<cfset data = queryNew("id,name,age,active","integer,varchar,integer,bit")>
<cfloop index="x" from="1" to="50">
<cfset queryAddRow(data)>
<cfset querySetCell(data,"id",x)>
<cfset querySetCell(data,"name","User #x#")>
<cfset querySetCell(data,"age",randRange(20,90))>
<cfset querySetCell(data,"active",false)>
</cfloop>
<cfset perpage = 10>
<cfparam name="url.start" default="1">
<cfif not isNumeric(url.start) or url.start lt 1 or url.start gt data.recordCount or round(url.start) neq url.start>
<cfset url.start = 1>
</cfif>
<h2>Random People</h2>
<cfoutput query="data" startrow="#url.start#" maxrows="#perpage#">
#currentrow#) #name#<br />
</cfoutput>
<p align="right">
[
<cfif url.start gt 1>
<cfset link = cgi.script_name & "?start=" & (url.start - perpage)>
<cfoutput>Previous Page</cfoutput>
<cfelse>
Previous Page
</cfif>
/
<cfif (url.start + perpage - 1) lt data.recordCount>
<cfset link = cgi.script_name & "?start=" & (url.start + perpage)>
<cfoutput>Next Page</cfoutput>
<cfelse>
Next Page
</cfif>
]
</p>
There is a very good ColdFusion Open Source pagination project on RIAForge: http://paginationcfc.riaforge.org/. It covers everything you need and comes with a lot of predefined styles. At least you can analyze the code and customize it to meet your requirements.
This was a fun question. How about this to build your list of page links:
<cfset pageList = "">
<cfloop from="1" to="#ceiling(data.RecordCount/perpage)#" index="i">
<!--- Determine the start record for selected page --->
<cfset targetRecord = 1 + (perpage * (i - 1))>
<cfif ceiling(url.start/perpage) NEQ i>
<cfset link = cgi.script_name & "?start=" & targetRecord>
<cfset pageList = listAppend(pageList, "#i#", " ")>
<cfelse>
<cfset pageList = listAppend(pageList, i, " ")>
</cfif>
</cfloop>
Now you can just drop pageList into your navigation section like so:
<p align="right">
[
<cfif url.start gt 1>
<cfset link = cgi.script_name & "?start=" & (url.start - perpage)>
<cfoutput>Previous Page</cfoutput>
<cfelse>
Previous Page
</cfif>
/
#pageList#
/
<cfif (url.start + perpage - 1) lt data.recordCount>
<cfset link = cgi.script_name & "?start=" & (url.start + perpage)>
<cfoutput>Next Page</cfoutput>
<cfelse>
Next Page
</cfif>
]
</p>