I have a column(cse_dept) where it has integers , I would only like to show the columns where it equals 12 or 39.
Is there a way to do this?
<cfif (#GetCurrentUser.cse_dept# eq '12'39') >
<h1>test</h1>
</cfif>
It does not show me a error it just doesn't work the way I would like it.
You can use listFind. If the value of GetCurrentUser.cse_dept is 12 or 39 listFind will return the a number greater than 0
<cfif listFind('12,39', GetCurrentUser.cse_dept)>
<h1>test</h1>
</cfif>
listFind is case sensitive in case you were searching for something other than numbers. If you need a case-insensitve search you can use listFindNoCase
Alternatively you could check for each value separately
<cfif GetCurrentUser.cse_dept EQ 12 OR GetCurrentUser.cse_dept EQ 39>
<h1>test</h1>
</cfif>
If you want to check if GetCurrentUser.cse_dept is 12 or 39 for any result in your query you can do
<cfif listFind(valueList(getCurrentUser), 12) OR listFind(valueList(getCurrentUser), 39)>
<h1>test</h1>
</cfif>
There are a few ways to do this:
Method 1 - Query of Queries
<cfquery name="q1" dbtype="query">
select count(*) records
from GetCurrentUser
where cse_dept in (12,39)
</cfquery>
<cfif q1.records gt 0>
do something
</cfif>
Method 2 - List Functions
<cfif ListFind(ValueList(GetCurrentUser.cse_dept), 12)
+
ListFind(ValueList(GetCurrentUser.cse_dept), 39) gt 0>
do something
</cfif>
Method 3 - Array Functions
<cfif ArrayFind(GetCurrentUser['cse_dept'], 12)
+
ArrayFind(GetCurrentUser['cse_dept'], 39) gt 0>
do something
</cfif>
Method 2 is very similar to Matt's second suggstion. One difference is the use of a ValueList function. The second is that instead of using "or", I added the function results together. The results are the same.
Method 3 is probably the fastest. However, depending on where those numbers 12 and 39 come from, Method 1 might be a better approach.
Related
Is there a syntax in coldfusion that will let me do that following?
<cfif DataSet2.RecordCount gt 0 >
<cfset append = #ArrayAppend(DataSet2Results,VAL(DataSet2.RecordCount))# >
</cfif>
Replacing the '2' in each case with an variable name (an index in a loop in this case).
So it would look like:
<cfif DataSet#index#.RecordCount gt 0 >
<cfset append = #ArrayAppend(DataSet#index#Results,VAL(DataSet#index#.RecordCount))# >
</cfif>
I know I can do a two dimensional array for this, but it would save me a slice of time if this can be done.
I found the answer to my own question. I used Variables["DataSet#index#"].recordcount and so on.
I am creating a reservation system for the cars with instructors and its basically working correctly. I want to implement the rule where I can say want to block some additional sessions.
For example the instructor just finish session from 6:00Am to 8AM. the next available slot should be 8AM if they decide back to back scheduling and 8:15AM (if they want a break). The other sessions withing 105 minutes span should be hidden so the next available session will at either 10AM
<cfloop index="ii" from="#startHour#" to="#endHour#">
<cfloop index="jj" from="0" to="#60-locbtwMinute#" step="#locbtwMinute#">
<cfset showtime="y">
<cfset time = createtime(ii, jj, 0)>
<cfloop query="qAssignedSessions">
<cfset preSessionStart = dateadd("n",-locBtwSpan-buffertime,sessionstart)>
<cfset postSessionend = dateadd("n",bufferTime,sessionend)>
<cfif (hour(time) gt hour(preSessionStart) or (hour(time) eq hour(preSessionStart) and minute(time) gt minute(preSessionStart)))
and (hour(time) lt hour(postSessionend) or (hour(time) eq hour(postSessionend) and minute(time) lt minute(postSessionend)))>
<cfset showtime="n">
</cfif>
</cfloop>
<cfif ((datecompare(arguments.startDt,latestSessionDate) eq 1 and ((hour(time) eq 20 and minute(time) eq 00) or hour(time) lt 20))
or (datecompare(arguments.startDt,latestSessionDate) eq 0 and ((hour(time) eq 20 and minute(time) eq 00) or (hour(time) lt 20 and hour(time) gt hour(now()))))
)
and showtime eq "y">
<option value="<cfoutput>#timeformat(time, 'HH:mm')#</cfoutput>"><cfoutput>#timeformat(time, "hh:mm tt")#</cfoutput></option>
</cfif>
</cfloop>
</cfloop>
I figure out I just create 2 array one for time slots that are available and second for the one that are already taken and eliminate the one I do not need
I have a query as below which
<cfquery name="qryGetXXX" datasource="#sDataSource#">
SELECT COUNT(*) AS TotalCount,Quality.Qualitydesc
FROM QualityCheck INNER JOIN Quality
ON QualityCheck.QualityID = Quality.qualityID
WHERE DATEDIFF(d,DueDate,GETDATE()) >= 90
GROUP BY quality.qualityDesc
</cfquery>
will result in
1) *total count* 21 *QualityDesc* IO
2) *total count* 1 *QualityDesc* Max
3) *total count* 1 *QualityDesc* Min
4) *total count* 1 *QualityDesc* Other
5) *total count* 3 *QualityDesc* Reg
In order to get the first row I am using,
<cfif #qryGetXXX.RecordCount# gt 0 >
<cfloop query="qryGetXXX" startrow="1" endrow="1">
<cfset XXXTimeTotal =#qryGetXXX.TotalCount# >
</cfloop>
<cfelse>
<cfset XXTotal = 0 >
</cfif>
which is checking for the first the recordcount of the whole query but is there any way I can check whether the first row (i.e. startrow 1 and endrow 1) is has a value and then if the startrow 2 and endrow 2 has a value and so on? Can I put the results in an array and will that be easier?
Frank,
You are correct, i am new to coldfusion, hence all the confusion.The query provides me with the results i.e i have the result set as explained above, however i cannot figure out a way to check every row individually. For example i have to check if there are results for the first row and if it does i have to pass that value to my output, and if doesnot i have to put in no value was entered or '0'. I need to do this for the five rows and there will always only be 5 rows of results, which is why i was using start and endrow, however start and endrow donot allow me the flexibility to check for empty row values. In essence i would like to check the row as something like below.
<!--- check the first row of resuslt i.e. Max values ---!>
<cfif startrow1.endrow1 gt 0 >
<cfset nIOCount =#qryGetXXX.TotalCount# >
<cfelse>
<cfset nIOCount = '0'>
</cfif>
<!--- then check the second row resuslts Max ---!>
<cfif startrow2.endrow2 gt 0 >
<cfset nMaxCount =#qryGetXXX.TotalCount# >
<cfelse>
<cfset nMaxCount = '0'>
</cfif>
<!---Output the values---!>
Totals
<tr>
<th>IO</th>
<td>#nIoCount#</td>
</tr>
<tr>
<th>Max </th>
<td>#nMaxCount#</td>
</tr>
<tr>
I know i cannot reference startrow and endrow as i want, so i am looking for a way to reference each row individually in the result set some other way. Any suggestions?
thanks
That query/code is rough (I rendered it to something I could work with).
Your query: (if yours works just leave it as is (I'm making two assumptions below)).
<cfquery name="test" datasource="#sdatasource#">
select count(a.*) as totalcount, a.qualitydesc
from quality a, qualitycheck b
where a.qualityid = b.qualityid
and datediff(d,a.duedate,getdate()) >= 90
group by a.qualitydesc
</cfquery>
Then do your check if and sets like this: (instead of an array I did a struct (making more assumptions as well)).
<cfset totals = structnew()>
<cfif test.recordcount>
<cfoutput query="test">
<cfif test.totalcount neq "">
<cfset StructInsert( totals, test.QualityDesc, test.totalcount )>
<cfelse>
<cfset xxtotal = 0>
</cfif>
</cfoutput>
</cfif>
In fact, you can skip that nested if statement and for the xxtotal and do something else out of the loop leaving you with tighter code that looks like this:
<cfset totals = structnew()>
<cfif test.recordcount>
<cfoutput query="test">
<cfset StructInsert( totals, test.QualityDesc, test.totalcount )>
</cfoutput>
</cfif>
So if these are your targets:
1) totalcount 21 QualityDesc IO
2) totalcount 1 QualityDesc Max
3) totalcount 1 QualityDesc Min
4) totalcount 1 QualityDesc Other
5) totalcount 3 QualityDesc Reg
Then your looped values will look like this and any missing values or whatever will be bypassed (again you will need to do some checking down stream)...
totals.IO = 21
totals.Max = 1
totals.Min = 1
totals.Other = 1
totals.Reg = 3
Let me know if this helps and makes sense.
(Summary from comments...)
If you mean some of the descriptions, like "IO", are not included in query results at all, then that is what I suspected earlier. Since you are using an INNER join, the query will only return counts for descriptions that exist in both tables. If you want to include all descriptions, even if there is no match in "QualityCheck", you need to use an OUTER join instead. For example:
SELECT Quality.Qualitydesc
, COUNT(QualityCheck.QualityID) AS TotalCount
FROM Quality LEFT JOIN QualityCheck
ON QualityCheck.QualityID = Quality.qualityID
AND DATEDIFF(d,DueDate,GETDATE()) >= 90
GROUP BY quality.qualityDesc
Though as I mentioned on another thread, how you construct the filter can negatively impact the query's performance. See What makes a SQL statement sargable? for details and alternatives.
Side note, while you provided a lot of detail here, you omitted the most important part: .. the actual goal ;-) You will get faster and more accurate responses if you clearly summarize what you are trying to do first, then include the code.
The Golden Rule: Imagine You're Trying To Answer The Question
The XY Problem
How to Ask
It's a bit of tricky question, however, my page for most rated bands displays the band logos in order of how high they have been rated. My only problem is i want to count through the records by using a cfloop from 1 to 10 and because it is split in to two columns, have one counting 1 through to 9 and the other 2 through to 10, each of them with steps of two.
Can anybody help me with this? If i've confused just mention it and ill try to clarify exactly what i mean.
<DIV class="community_middle">
<cfoutput query="top10MostRated">
<cfloop from="2" to="10" index="i" step="2">
<DIV class="communityContent">
#chr(i)#
<IMG src="logo/#top10MostRated.Logo#" alt="#top10MostRated.Name#" width="100%" height="100%"></IMG>
</DIV>
<BR/>
</cfloop>
</cfoutput>
</DIV>
If you're looking to do odd/even lists separately, then you can use the currentrow property of the query combined with the modulo operator (%) to work out if the row is odd or even:
<cfloop query="topBands>
<cfif topBands.currentRow % 2 = 1>
<!--- do your odd number output here --->
</cfif>
</cfloop>
<cfloop query="topBands>
<cfif topBands.currentRow % 2 = 0>
<!--- do your even number output here --->
</cfif>
</cfloop>
I think these answers address your side-by-side part of your question but does not explain the "same image" issue. Their code is written correctly but does not explain the reason.
Your code:
<IMG src="logo/#top10MostRated.Logo#"
alt="#top10MostRated.Name#"
width="100%" height="100%"></IMG>
... would be fine if you were only inside a <cfloop query = "top10MostRated"> or <cfoutput query = "top10MostRated"> block. The reason is because inside these types of blocks CF is smart enough to know you want the data for the current row. It would be the same as:
<IMG src="logo/#top10MostRated.Logo[top10MostRated.currentRow]#"
alt="#top10MostRated.Name[top10MostRated.currentRow]#"
width="100%" height="100%" />
Because you're nesting the to/from cfloop inside a <cfoutput query = ""> block, you are getting unexpected results. Your existing code is always asking for the record provided by your outer loop. Hence you see the same image 5 times. (using any of the fine examples provided will help you get out of this) but, you can remove the query from your cfoutput and simply ask CF to show you the value for the correct row in your loop using your index (you set your index to "i") so the below would show you the image that corresponds to your loop.
<IMG src="logo/#top10MostRated.Logo[i]#"
alt="#top10MostRated.Name[i]#"
width="100%" height="100%" />
It sounds like what you'd like to get is a collection of even-numbered records and a collection of odd-numbered records. In Coldfusion 10 or Railo 4, you can use groupBy() from Underscore.cfc to split up your query result into manageable sub-sets, like so:
_ = new Underscore();// instantiate the library
groupedBands = _.groupBy(topBands, function (val, index) {
return index % 2 ? "odd" : "even";
});
This returns a struct with two elements odd and even, each containing an array of records which are odd or even. Example result:
{
odd: [{name: "Band one"}, {name: "Band three"}],
even: [{name: "Band two"}, {name: "Band four"}]
}
Splitting your results into logical sub-sets makes the code more readable:
<cfoutput>
<cfloop from="1" to="5" index="i">
<div class="left">#groupedBands.odd[i].name#</div>
<div class="right">#groupedBands.even[i].name#</div>
</cfloop>
</cfoutput>
You'll also be able to use those sub-sets in other places on your page if you need to.
Note: I wrote Underscore.cfc
Ben Nadel has a post exactly for this. Link here
A breakdown for this is
<cfloop query="top10MostRated">
<cfif top10MostRated.CurrentRow MOD 2>
<!--- Add to the "odd list" --->
<cfelse>
<!--- Add the record to the "even list" --->
</cfif>
</cfloop>
Then you'll have 2 lists oddList and evenList. Then it's just a matter of displaying them.
I'd do it a different way. The objective is to have records 1 and 2 side by side and I don't see that in #barnyr's answer.
<cfoutput>
<cfloop from="2" to="topbands.recordcount + 1" index = "i" step="2">
#topbands.fieldname[i-1]#
<cfif i lte topbands.recordcount>
#topbands.fieldname[i]# <br />
</cfif>
</cfloop>
</cfoutput>
I am working on revamping a sites' paging system, and I have run into something simple I can't seem to solve. I am trying to show pages (1 2 3 4 5) at a time, and when the user gets to page 5, the list changes to something like (4 5 6 7 8). How could I do this using a cfloop? Here is a sample of my code:
<cfloop from="1" to="#totalPages#" index="i">
<cfoutput><a class="paging" href="?start=#(i*25)-24#">#i#</a></cfoutput>
</cfloop>
At the moment it shows pages 1 - 54 all at once. Any tips?
Heres my code
<cfset curPage = Int(start / 25) + 1>
<cfloop from="1" to="#totalPages#" index="i">
<cfif i is curPage>
<div class="page current">#i#</div>
<cfelse>
<cfif totalPages lte 5 or i is 1 or i is totalPages or (i gt curPage - 3 and i lt curPage + 3) or ((curPage is 1 or curPage is 2) and i lt 6) >
<div class="page">#i#</div>
<cfelse>
<cfif i is 2 or i is totalPages - 1>
<div class="more">...</div>
</cfif>
</cfif>
</cfif>
</cfloop>
What this code does is it shows the first 5 pages, then an ellipsis, then the last page. As you page through it, it will always show the link to the first and last page, plus 2 page before and after the current page.
Screenshots: Page 1 and page 10
You should be able to easily modify this to work exactly how you want it. (I happen to not like when all of the links change at once the way you described)
There is a great open source pagination library that will solve your problem. I can't say enough good things about it because I wrote it. Shameless, I know. Anyways:
http://www.dopefly.com/projects/pagination/
Check out the docs, they are very complete and helpful. In your code, printing the page numbers are as simple as calling #pagination.getRenderedHTML()#. It is pretty customizable so that you can change the numbers that are printed and you can style the output however you like.
How about:
<cfloop from="#url.startpage#" to="#url.startpage+4#" index="i">
<cfif i LE totalpages>
<cfoutput><a class="paging" href="?start=#(i*25)-24#">#i#</a></cfoutput>
</cfif>
</cfloop>
Of course you would need to add "startpage" to your url and manipulate it appropriately. So your first link would be startpage=1 but your last would be startpage=4 (if I understand your question correctly).