ColdFusion paging and page numbers - coldfusion

I have a list of user comments stored in a database and I'm displaying 10 per page. On page load I need to dynamically render the appropriate amount of page numbers. At first I thought I would just get the total amount of comments and divide that by 10 to get the # of pages. That however doesn't work correctly. For example:
1. 1-10
2. 11-21
3. 22-32
4. 33-43
5. 44-54
6. 55-65
7. 66-76
So basically with my original math if I have 70 results I will have 7 pages, but if I have 71 results I get 8 pages, which obviously isn't correct. How can I fix this?
Here's my original code:
<cfset commentsNumber = getComments.recordcount / 10>
<cfloop from="1" to="#commentsNumber#" index="i" >
<cfoutput>
#i#
</cfoutput>
</cfloop>
Edit: I can't do math today :(

...if I have 71 results I get 8 pages, which obviously isn't correct.
10 results per page w/ 71 results is 8 pages. What's wrong?

If you want to be sure of your record paging in ColdFusion, you could use an open source library like Pagination.cfc. It handles all the math for you and gives you a customizable display.

hey correct you math first.. :) your example is wrong.
If you are showing 10 comments per page then it should be
1. 1-10
2. 11-20
3. 21-30
etc
By the way you can use CFGRID for paging... it's very simple..

Related

How can I calculate a new date conditionally based on other information?

I have a Google Sheet which is being populated by a Google Form. I am using Google Apps Script to add some extra functionality. Please feel free to access and modify these as needed in order to help.
Based on answers from the Form, I need to return a new date that factors in the time stamp at form submission.
This is a dumbed down example of what I need to do, but let's think of it like ordering a new car and its color determines how long it is going to take.
Car
Color
Toyota
Red
Honda
Blue
Tesla
Green
I need to write a conditional IF statement that determines how many weeks it will take to get the car based on the ordered color.
-
Red
Blue
Green
Toyota
1
3
5
Honda
2
4
6
Tesla
1
1
1
So if you order a Toyota in Red, it will take one week. If you order a Toyota in Green, it will take 5 weeks. If you order a Tesla, it will be really in one week no matter what color. Etc...
I started by writing some language in Sheets to take the Timestamp which is in Column A and add the appropriate amount of time to that:
=IFS(AND(B2 = "Toyota",C2 = "Red"),A2 + 7,AND(B2="Toyota",C2="Blue"), A2 + 21,AND(B2="Toyota",C2="Green"), A2 + 35,AND(B2 = "Honda",C2 = "Red"),A2 + 14,AND(B2="Honda",C2="Blue"), A2 + 28,AND(B2="Honda",C2="Green"), A2 + 42,AND(B2 = "Tesla"),A2 + 7)
And then I dragged that down the length of the entire column so that it would fill in as submissions came in.
However when you fill in the Google Form, it will overwrite what's in that entire row, blowing out what I had in that column.
Now I realized that the code needs to be written in Google Apps Script and returned as a value.
What kinds of modifications need to be made to my IFS statement in order to make it compatible with Google Apps Script?
For easier approach, QUERY would actually solve your issue without doing script as Broly mentioned in the comment. An approach you can try is to create a new sheet. Then have that sheet contain this formula on A1
Formula (A1):
=query('Form Responses 1'!A:C)
This will copy A:C range from the form responses, and then, copy/paste your formula for column Date Needed on column D.
Output:
Note:
Since you only copied A:C, it won't affect column D formula.
Your A:C in new sheet will update automatically, then the formula you inserted on D will recalculate once they are populated.
Add IFNA on your formula for column D to not show #N/A if A:C is still blank.
Formula (D2):
=IFNA(IFS(AND(B2 = "Toyota",C2 = "Red"),A2 + 7,AND(B2="Toyota",C2="Blue"), A2 + 21,AND(B2="Toyota",C2="Green"), A2 + 35,AND(B2 = "Honda",C2 = "Red"),A2 + 14,AND(B2="Honda",C2="Blue"), A2 + 28,AND(B2="Honda",C2="Green"), A2 + 42,AND(B2 = "Tesla"),A2 + 7), "")

CFCase values with spaces are not found

As anyone had this issue with parsing from html file with setting <cfcase> values that contain spaces in the value? If the value contains spaces ColdFusion isn't able to find the data match in the parsed html file.
For example:
<CFCASE value="LGT RAIN">
<CFSET application.condition="Lt Rain">
</CFCASE>`
The html file has the value "LGT RAIN", but due to the way its being parsed it is not defining the city's condition. Is there a work around like check the condition prior to running your switch statements? Like if "LGT RAIN" is found then set cfset value it to "LGTRAIN".
Here is sample link that I am parsing from.
https://forecast.weather.gov/product.php?site=CAE&issuedby=CAE&product=RWR&format=CI&version=1
<CFSET WeatherData = CFHTTP.FileContent>
Code:
<!--- Check For Condition with Two Word Phrase --------------->
<CFSET condition= #GetToken("#content#",#attributes.citystring# + 1, " ")#>
<br>
<CFSET city = #attributes.citystring#>
<CFIF (condition is "LGT")
or (condition is "HVY")
or (condition is "FRZ")
or (condition is "MIX")
or (condition is "NOT")>
<CFSET condition= (
#GetToken("#content#",#attributes.citystring# + 1, " ")#
& " " &
#GetToken("#content#",#attributes.citystring# + 2, " ")#
)>
<br>
<CFSET Temp = #GetToken("#content#",#attributes.citystring# + 3, " ")#>'
.......
There is a lot of missing context on what you're trying to ultimately do with the weather you get, so I made a couple of assumptions on ways to parse through the weather block.
You'll have to actually parse the page to get that actual block, but once you do, you can use my suggestions below to split those weather elements out. I got rid of the getToken() calls, because I think that may have been more work than you needed, and was essentially led to the problem with spaces. My code is still much wordier than I intended, but I tried to make sure I wasn't using anything above CF8. I think it should all work, but I don't have a CF8 server to test on.
https://trycf.com/gist/1993243eb476a629ec25f8c6e8ddff3c/acf?theme=monokai
I've included some notes inside the code as I went along.
<!--- Create a block of the parsed weather. --->
<cfsavecontent variable="myWX">ANDERSON FAIR 49 16 26 W12G20 30.01F
LIBERTY FAIR 49 14 24 W12G21 29.99F
SPARTANBURG FAIR 45 9 23 NW12G21 30.00F
CLEMSON FAIR 48 10 21 NW13G22 30.02F
GREENVILLE FAIR 48 13 24 VRB3 29.99F
GREENWOOD FAIR 49 13 23 W10G21 30.03F
GREER FAIR 48 9 20 VRB7 29.99F
ROCK HILL FAIR 46 17 31 NW10G22 29.99F
CHESTER FAIR 45 12 26 W12G17 30.01F
LAURENS FAIR 48 16 27 NW12G18 30.01F
</cfsavecontent>
<!--- end of line character (this may change if your feed uses other eol)--->
<cfset crlf = chr(13)&chr(10)>
<!--- Make weather an array of strings for each city. --->
<cfset wxArray = ListToArray(myWX, crlf, false, true)>
My suggestion is to put the different elements in a struct, then work with that. 1 row of city weather = 1 row of Struct.
My code below assumes you are parsing rows of a 65 character fixed-width record from the HTML, which is what it appears to be when you get at the individual rows.
<!--- Loop through the new weather array and make a new array of parsed weather. --->
<!--- Create initial weather array for output. --->
<cfset newWXArray = []>
<cfloop array="#wxArray#" index="wxRow">
<!--- NOTE: Yay go CF8s implicit struct creation! --->
<cfset WXRow = {
city = trim(mid(wxRow,1,15)) , <!--- trim() will remove leading/trailing spaces --->
sky = trim(mid(wxRow,16,10)) ,
tmp = trim(mid(wxRow,26,4)) ,
dp = trim(mid(wxRow,30,4)) ,
rh = trim(mid(wxRow,34,3)) ,
wind = trim(mid(wxRow,37,10)) ,
pres = trim(mid(wxRow,47,6)) ,
rmks = trim(mid(wxRow,53,11))
}>
<!--- ArrayAppend() returns true/false, so this variable isn't used. --->
<cfset throwaway = ArrayAppend(newWXArray, WXRow)>
</cfloop>
<cfdump var="#newWXArray#">
Now you have an array of all the cities, and can pull the individual pieces out for the city you need.
What's the weather like in Liberty? <br>
<cfset whichRow = 0>
<cfloop from="1" to="#arrayLen(newWXArray)#" index="i">
<cfif newWXArray[i].CITY IS "LIBERTY">
<cfset whichRow = i>
</cfif>
</cfloop>
<cfoutput>
City >> #newWXArray[whichRow].CITY# <br>
Sky >> #newWXArray[whichRow].SKY# <br>
Temp >> #newWXArray[whichRow].TMP# <br>
DewPoint >> #newWXArray[whichRow].DP# <br>
Relative Humidity >> #newWXArray[whichRow].RH# <br>
Wind >> #newWXArray[whichRow].WIND# <br>
Pressure >> #newWXArray[whichRow].PRES# <br>
Remarks >> #newWXArray[whichRow].RMKS# <br>
</cfoutput>
Final Note:
I had forgotten how limited CF8 was. There is a lot of stuff you can do to make this MUCH easier if you are able to upgrade to a current version of ACF or Lucee.
Also, as I suggested, changing your link to pull the Text-Only version (https://forecast.weather.gov/product.php?site=CAE&issuedby=CAE&product=RWR&format=txt&version=1&glossary=1) will significantly reduce the amount of text you have to parse through.
My experience with white space in text data messing up conditional logic has been with trailing spaces but the principles are the same. When in doubt look at your data. Here is a simple example:
MyVar = 'Lgt Rain'; // two spaces between the words
if (MyVar == 'Lgt Rain'){ // one space between words
applicable code
}
else {
writeoutput(replace(MyVar, ' ', '-space-', 'all'));
}

How to load specific columns with varying location from a text file in python?

I'm trying to read the discharge data of 346 US rivers stored online in textfiles. The files are more or less in this format:
Measurement_number Date Gage_height Discharge_value
1 2017-01-01 10 1000
2 2017-01-20 15 2000
# etc.
I only want to read the gage height and discharge value columns.
The problem is that in most files additional columns with metadata are added in front of the 'Gage height' column, so i can not just simply read the 3rd and 4th column because their index varies.
I'm trying to find a way to say 'read the columns with the name 'Gage_height' and 'Discharge_value'', but I haven't succeeded yet.
I hope anyone can help. I'm currently trying to load the text files with numpy.genfromtxt so it would be great to find a solution with that package but other solutions are also more than welcome.
This is my code so far
data_url=urllib2.urlopen(#the url of this specific site)
data=np.genfromtxt(data_url,skip_header=1,comments='#',usecols=2,3])
You can use the names=True option to genfromtxt, and then use the column names to select which columns you want to read with usecols.
For example, to read 'Gage_height' and 'Discharge_value' from your data file:
data = np.genfromtxt(filename, names=True, usecols=['Gage_height', 'Discharge_value'])
Note that you don't need to set skip_header=1 if you use names=True.
You can then access the columns using their names:
gage_height = data['Gage_height'] # == array([ 10., 15.])
discharge_value = data['Discharge_value'] # == array([ 1000., 2000.])
See the docs here for more information.

what is the .from(20).fetch(10) from the below code in play framework

In this line of code,
List<User> users = User.find("byEmailLike", "alexander#%") .from(20).fetch(10);
what is the .from(20).fetch(10) ?
It's intuitive usage: fetch 10 records starting from 20
Also it's described in docs, you should take a look there
List<Post> posts = Post.all().from(50).fetch(100); // 100 max posts start at 50

ColdFusion CFchart and feedback form

I am creating a feedback form system where users can fill in feedback forms and admin users will be able to run reports against the answers. I am trying to use cfchart to display the results in an easy to read manner for the administrators.
Here is my code:
&ltcfquery name="getFeedbackresults" datasource="#application.dsn#">
SELECT ff.feedbackFormId
,ff.feedbackFormName
,ff.feedbackFormDescription
,ff.activeFrom
,ff.activeTo
,ff.feedbackCountry
,ffq.question
,ffa.answer
,ffq.feedbackQuestionTypeId
,count(answer) AS distinctAnswer
FROM feedbackForm ff
,feedbackFormQuestion ffq
,feedbackFormInstance ffi
,feedbackFormAnswer ffa
WHERE ff.feedbackFormId = ffq.feedbackFormId
AND ff.feedbackFormId = ffi.feedbackFormId
AND ffi.feedbackFormInstanceId = ffa.feedbackFormInstanceId
AND ffa.feedbackQuestionId = ffq.feedbackQuestionId
AND ffq.feedbackQuestionTypeId not in (2,3)
GROUP BY ff.feedbackFormId, ff.feedbackFormName, ff.feedbackFormDescription, ff.feedbackCountry, ffq.question, ff.activeFrom, ffq.feedbackQuestionTypeId, ff.activeTo, ffa.answer
ORDER BY question
&lt/cfquery>
&ltcfoutput>
&lth2>#getFeedbackresults.feedbackFormName# Results&lt/h2>
&lth3>Description:
&ltp>#getFeedbackresults.feedbackFormDescription#&lt/p>
&ltul>
&ltli>&ltstrong>Active From: &lt/strong>#getFeedbackresults.activeFrom#&lt/li>
&ltli>&ltstrong>Active To: &lt/strong>#getFeedbackresults.activeTo#&lt/li>
&ltli>&ltstrong>Country: &lt/strong>#getFeedbackresults.feedbackCountry#&lt/li>
&lt/ul>
&lt/cfoutput>
&ltcfdump var="#getFeedbackresults#">
&ltcfchart format="flash" xaxistitle="Questions" yaxistitle="Answers" show3d="yes" chartheight="400" chartwidth="400">
&ltcfoutput query="getFeedbackresults" group="answer">
&ltcfchartseries type="bar" serieslabel="#answer#">
&ltcfchartdata item="#question#" value="#distinctAnswer#">
&lt/cfchartseries>
&lt/cfoutput>
&lt/cfchart>
The problem I am having is that the chart is displaying each list of answers in every series. I tried to upload an image but as I am a new user I'm not allowed. If anyone would like the image I can send it to them.
Any help would be greatfully appreciated.
You need to sort your query by the answer column if that's what you're going to group by. Your current ORDER BY is "question". Once you're sorting the same value that you're using for the group attribute on your cfoutput, you should be good to go.