CFLOOP with doubles - Coldfusion - coldfusion

I am trying to use cfloop to loop form 0.0 to 5.0 but it takes out the decimal point and is looping from 0 to 5 instead.
This is my code
<select name="cweight">
<option value="">---</option>
<cfloop index = "cweight" from = "0.0" to = "5.0">
<option value="#cweightid#">#cweight#</option>
</cfloop>
</select>
I need the loop to go over 0.1,0.2,0.3 until it reaches 5.0.
What should I add to allow me to do this?

CF doesn't have "doubles" - numbers have decimal places when they need them.
To do what you want, use NumberFormat with the mask set to 0.0 so that you always get a decimal place.
To increment by 0.1 at a time, simply set the cfloop step attribute.
<cfloop index="cweight" from="0" to="5" step="0.1">
<option value="#cweight#">#NumberFormat( cweight ,'0.0' )#</option>
</cfloop>

Related

Display single digits with leading zero

I am trying to make a minutes dropdown. How do I make it come out as 01, 02, 03, 04, 05, 06, 07 08, 09, 10, 11, 12, etc.? Right now the first 9 numbers only come out as 1, 2, 3, 4, 5, 6, 7, 8, 9.
<select id="minutes">
<cfloop from="1" to="60" index="m">
<option>#m#</option>
</cfloop>
</select>
Well, I understand why it's doing what it's doing and was expecting it anyway. Haha. Just wondered if there was a way of getting the 0s to come up without having to manually create 01-09 options.
You can use numberFormat.
<select id="minutes">
<cfoutput>
<cfloop from="1" to="60" index="m">
<option>#numberFormat(m,'00')#</option>
</cfloop>
</cfoutput>
</select>
See also this runnable example at trycf.com.
I can think of two options off the top of my head:
Option 1 - cfloop over timespan
Create a start and end time, and use cfloop with a step value of one minute (source: adobe docs )
<cfset startTime = CreateTime(0,0,0)>
<cfset endTime = CreateTime(0,59,59)>
<select id="minutes">
<cfoutput>
<cfloop from="#startTime#" to="#endtime#" index="m" step="#CreateTimeSpan(0,0,1,0)#">
<option>#TimeFormat(m, 'mm')#</option>
</cfloop>
</cfoutput>
</select>
Option 2 - use RIGHT()
Just prepend 0 to all values, and take the right two characters:
<select id="minutes">
<cfoutput>
<cfloop from="0" to="59" index="m">
<option>#Right(0 & m, 2)#</option>
</cfloop>
</cfoutput>
</select>

Show where column equals a specific data?

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.

ColdFusion: Can you pull out a unique record from a query using recordCount?

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>

Limiting number of pages on a paging list

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).

How to handle comma separated decimals and floats in CF?

I have to validate form values as integers.
I have tried something like this:
<cfloop collection="#form#">
<cfif form.value eq int(form.value)>
#form.value# is an integer
</cfif>
</cfloop>
It works as long the user does not input comma as the decimal separator, which is the default way of doing this here in Germany.
I have to use CF MX 6.1.
It would also probably help to look into the Internatational functions that are available. LSParseNumber(), for instance.
You may, if you like, desensitize the input first.
<cfset var comma = ",">
<cfset var period = ".">
<cfset form.value = replace(form.value, comma, period, "all")>
But, if all you need is to verify if a field is an integer, why don't you look at CFLib.org - IsInt ?
<cfscript>
/**
* Checks to see if a var is an integer.
* version 1.1 - mod by Raymond Camden
*
* #param varToCheck Value you want to validate as an integer.
* #return Returns a Boolean.
* #author Nathan Dintenfass (nathan#changemedia.com)
* #version 1.1, April 10, 2002
*/
function isInt(varToCheck){
return isNumeric(varToCheck) and round(varToCheck) is vartoCheck;
}
</cfscript>
Like Al Everett, I recommend using the locale specific functions:
<!--- actually *setting* the desired locale is mandatory for this to work --->
<cfset SetLocale("German (Standard)")>
<cfif CGI.REQUEST_METHOD eq "POST">
<!--- loop the FieldNames list so only real posted values are handled --->
<cfloop list="#FORM.FieldNames#" index="FieldName">
<cfif LSIsNumeric(FORM[FieldName])>
<cfset num = LSParseNumber(FORM[FieldName])>
<!--- do stuff with #num# --->
</cfif>
</cfloop>
</cfif>