I have an issue that is greatly vexing me. I have written a basic pagination tool for a page that is running on Coldfusion 11: http://fftoolbox.scout.com/ffwc/rankings.cfm?rankings=season&page=2
I have designed it such that there is a url query called page that holds the page number. This way, the user can just type in the page number that he desires rather than having to press the next and previous buttons.
The pagination works just fine for pages 1 - 25, but on page 26 and after, it records the url.page variable as 1, even though the link on the control clearly shows that the variable should be page 26.
If you will notice, I have coldfusion dumping the URL scope at the top of the page. You will note that when the page is equal to 25 or less, the page is reported correctly. Any value over 25, though, url.page is assigned a value 1, even though that is not what is being sent according to the browser. I have tested this in FF, Chrome, and IE, and all exhibit the same behavior. This leads me to believe that it is not something in the code or a browser based peculiarity, but possibly some sort of server setting.
Here is some assorted code of the project:
The dump and setting the session variables.
<cfdump var='#url#'>
<cfif not StructKeyExists(url,'page')>
<cfset session.page = 1>
<cfelse>
<cfset session.page = url.page>
</cfif>
<cfset startingRank = (session.page - 1)*50 +1>
<cfset endingRank = (session.page - 1)*50 +50>
This starting and ending rank are used in an SQL query to retrieve the proper range of players. I am not including the SQL unless someone requests it as that is working fine. In fact, when I hard code the url.page to be 26, everything works as it is supposed to.
The Previous and Next links:
<tr><td colspan='2' style='text-align: center'>
<cfif session.page gt 1>
<a href='./rankings.cfm?rankings=season&action=#Evaluate(session.page - 1)#'>Previous</a>
</cfif></td>
<td colspan='2' style='text-align: center'>
<cfif session.page lt Evaluate(Int((GetMax.recordCount -1)/50) +1)>
<a href='./rankings.cfm?rankings=season&page=#Evaluate(session.page + 1)#'>Next</a>
</cfif></td></tr>
Please let me know if you need any more information. I am thinking that this is actually either an Apache issue or a CF one as the pagination is working properly on pages 1 through 25.
EDIT:
I have had a request to show the queries. There are two of them. One to get the data and another to get the count of the entire set of data.
<cfquery name='GetData' datasource='XXXXXXXX'>
select * from
(select #currentRank := #currentRank +1 as rank,player_name, point_sum, contest_type from
(select player_name,sum(points) as point_sum, contest_type, (select #currentRank := 0) r
from 2014_ranking_season
inner join ecom_item_type on ecom_item_type.ecom_item_type_id = 2014_ranking_season.contest_type
where contest_type = #session.filter#
group by player_name)t1
order by point_sum desc)t2
where rank between #startingRank# and #endingRank#
</cfquery>
<cfquery name='GetMax' datasource='XXXXXXXX'>
select * from
(select #currentRank := #currentRank +1 as rank,player_name, point_sum, contest_type from
(select player_name,sum(points) as point_sum, contest_type, (select #currentRank := 0) r
from 2014_ranking_season
inner join ecom_item_type on ecom_item_type.ecom_item_type_id = 2014_ranking_season.contest_type
where contest_type = #session.filter#
group by player_name)t1
order by point_sum desc)t2
</cfquery>
I hope that helps. Please bear in mind that except for the previous button with which I am experimenting, everything works fine until page > 25.
EDIT:
The value for GetMax.recordCount = 2242. Also, I am writing a small reproduction page that is scaled down to just produce the results. Once it is done, I will post the code of the entire page. Whether or not it reproduces the result it will tell us SOMETHING.
EDIT:
The reproduction page is in place and the error IS reproducible, even with the scaled down code. Here is the URL: http://fftoolbox.scout.com/ffwc/testPagination.cfm?rankings=season&page=26
Here is the entire code listing:
<cfdump var='#url#'>
<cfset session.page = 1>
<cfif StructKeyExists(url,'page')>
<cfset session.page = url.page>
</cfif>
<cfset startingRank = (session.page - 1)*50 +1>
<cfset endingRank = (session.page - 1)*50 +50>
<cfif not StructKeyExists(session,'filter')>
<cfset session.filter = 0>
</cfif>
<cfparam name="url.rankings" default="season">
<html>
<head>
<title>Test Pagination</title>
</head>
<body>
<br /><br />
<cfoutput>
<table width='99%' cellpadding='5' cellspacing='0'><tr class='header'>
<cfset count = 0>
<cfquery name='GetData' datasource='fftoolbox_sql'>
select * from
(select #currentRank := #currentRank +1 as rank,player_name, point_sum, contest_type from
(select player_name,sum(points) as point_sum, contest_type, (select #currentRank := 0) r
from 2014_ranking_season
inner join ecom_item_type on ecom_item_type.ecom_item_type_id = 2014_ranking_season.contest_type
group by player_name)t1
order by point_sum desc)t2
where rank between #startingRank# and #endingRank#
</cfquery>
<cfquery name='GetMax' datasource='fftoolbox_sql'>
select * from
(select #currentRank := #currentRank +1 as rank,player_name, point_sum, contest_type from
(select player_name,sum(points) as point_sum, contest_type, (select #currentRank := 0) r
from 2014_ranking_season
inner join ecom_item_type on ecom_item_type.ecom_item_type_id = 2014_ranking_season.contest_type
group by player_name)t1
order by point_sum desc)t2
</cfquery>
<th>RANK</th><th>PLAYER NAME</th><th>TEAM NAME</th><th>POINTS</th></tr>
<cfloop query='GetData'>
<cfquery name='GetOneName' datasource='fftoolbox_sql'>
select team_name
from 2014_ranking_season
where player_name = <cfqueryparam cfsqltype='CF_SQL_VARCHAR' value='#GetData.player_name#'>
limit 1
</cfquery>
<cfset name = URLEncodedFormat(#player_name#)>
<tr <cfif #count#%2 eq 0>class='evenRow'<cfelse>class='oddRow'</cfif>><td>#rank#</td>
<td><a href='./player.cfm?name=#variables.name#' target='_blank'>#player_name#</a></td><td>#GetOneName.team_name#</td><td>#point_sum#</td></tr>
<cfset count++>
</cfloop>
<tr><td colspan='2' style='text-align: center'>
<cfif session.page gt 1>
<a href='./testPagination.cfm?rankings=season&page=#Evaluate(session.page - 1)#'>Previous</a>
</cfif></td>
<td colspan='2' style='text-align: center'>
<cfif session.page lt Evaluate(Int((GetMax.recordCount -1)/50) +1)>
<a href='./testPagination.cfm?rankings=season&page=#Evaluate(session.page + 1)#'>Next</a>
</cfif></td></tr>
</table>
</cfoutput>
</body>
</html>
I will also get the http request headers to display on that page. I will put them in the question, if people want, but the question is starting to get long, and the cfdump formats it nicely in the browser. Thank you, everyone for the interaction so far.
UPDATE:
I am suspecting that our provisioning team, over whom I have no control, might have done something on the backend, such as put it on Varnish, which is not supposed to happen. Please continue to look at the code, etc, and I will bark up this tree to see if there is something they have done. 25 just seems like to "human" a number for it not to be a setting somewhere. I will keep you posted.
UPDATE:
I have checked with the provisioning team, and Varnish is not active, and the load balance is NOT causing the issue, as I could replicate the issue when bypassing the LB. My opinion that it is a configuration issue with Apache or CF seems to be becoming more and more likely as we rule things out.
UPDATE:
Upon adding the code that #Leigh recommended, we find that the http request is definitely page=26, and the URL scope is definitely page=1. I am looking to see if an Application file might be messing with it.
FINAL UPDATE:
Upon examining the Application.cfm, I found the following code:
<cfif cgi.script_name NEQ "/ffwc/all-time-leaderboard.cfm">
<cfset ValidPages = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25">
<cfif IsDefined("url.page") and ListFind(ValidPages,url.page) eq 0>
<cfset url.page = 1>
</cfif>
</cfif>
After this was commented out, the pagination worked just fine. Another alternative method of making it work would be to not use page as the variable name for the page. (This is counter intuitive and distasteful to me) I hold the opinion that constructs like pagination should be contained within one file or directory, and not be site wide.
Upon examining the Application.cfm, I found the following code:
<cfif cgi.script_name NEQ "/ffwc/all-time-leaderboard.cfm">
<cfset ValidPages = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25">
<cfif IsDefined("url.page") and ListFind(ValidPages,url.page) eq 0>
<cfset url.page = 1>
</cfif>
</cfif>
After this was commented out, the pagination worked just fine. Another alternative method of making it work would be to not use page as the variable name for the page. (This is counter intuitive and distasteful to me) I hold the opinion that constructs like pagination should be contained within one file or directory, and not be site wide.
Thank you to all that commented, and helped me in this endeavor.
First of all, your "previous" button isn't setting a URL.page or session.page - it's using "action." Since page isn't defined in the URL it automatically sets the page to one... So clicking "previous" and then "next" always sets me back to page 2 no matter where I start.
Now, your "next" link...
<cfif session.page lt Evaluate(Int((GetMax.recordCount -1)/50) +1)>
<a href='./rankings.cfm?rankings=season&page=#Evaluate(session.page + 1)#'>Next</a>
</cfif>
I just ran this code after setting session.page on my own .cfm page...
A "Next" button will only ever show up if session.page is zero or GetMax.recordcount is more than 50x the value of session.page. I raise issue with that because GetMax.recordcount shouldn't ever change. You're displaying 50 records at a time.
The following code doesn't work so I don't what you're doing on your end...
<cfset GetMax.recordcount = 50>
<cfset session.page = 1>
<cfoutput>
<cfif session.page lt Evaluate(Int((GetMax.recordCount -1)/50) +1)>
<a href='./rankings.cfm?rankings=season&page=#Evaluate(session.page + 1)#'>Next</a>
</cfif>
</cfoutput>
It might be time to show us the query logic.
Edit:
Why not do something like this?
<cfset StartRecord = #url.page# * 50 - 49>
<cfquery name="listOfPeople" datasource="XXX">
SELECT TOP 50 *
FROM PeopleTable
WHERE PeopleID >= #StartRecord#
</cfquery>
It's so simple and I'm just not getting why the query GetMax is needed. All that extra math involved just heightens the risk for error. Pagination shouldn't be a project unto itself.
I'm an "old dog" and largely self-taught on this stuff and can usually make things work (primative and convoluted as it might be), but this is the first thing that has me really stymied.
I didn't want to burden everyone with a lot of stuff to try to explain, but, here is perhaps a better explanation and example:
(#Leigh - and thank you for your time and help!) - The query is dynamic because what I desire to have is a single "universal" page combination (form page plus accompanying action page) that is used to edit multiple different (but fairly similar) record sets (so that I don't have to write a whole bunch of individual form/action page pairs).
When this "universal" "change" form page is invoked, it is passed the "ID" variable for the particular record to be edited, along with a "listID" variable unique to the particular record set containing the record to be edited.
Using the "URL.listID", the form page then looks at a pre-defined included list of record set variables (datasource, query table, field for column 1, field for column 2, etc.) pertaining to the value of the "listID" and sets (using ) the dynamic variables. Example - if "listID" is "5", which has only one column:
<cfif #URL.listID# EQ 5>
<cfset page_title = 'Change Member Role Picklist'>
<cfset datasource = '#Session.db_docs#'>
<cfset query_tbl = 'tblMemberRole'>
<cfset columns = 1>
<cfset column1_label = 'Member Role'>
<cfset column1_field = "role">
<cfset column1_input_type = "text">
<cfset column1_input_size = "100">
<cfset column1_input_maxlength = "100">
</cfif>
The query uses those variables ("ID" plus the others it got from the above list) to retrieve the individual record to be edited, and populate the "change" form.
Run query:
<cfquery name="cfqGetItem" datasource="#datasource#">
SELECT *
FROM #query_tbl#
WHERE ID = <cfqueryparam value="#URL.ID#" cfsqltype="cf_sql_integer">
</cfquery>
My "change" form (abbreviated here without table HTML) to be populated would be:
<form name="form_item_chg" action="chg_item2.cfm" method="post" enctype="multipart/form-data">
<input type="text" name="#column1_field#" maxlength="#column1_input_maxlength#" size="#column1_input_size#" value="#cfqGetItem[column1_field][currentRow]#">
<input type="Submit" value="Post Changes">
</form>
However, instead of the "change" form being populated with the VALUE for field "role", it instead is trying to use the variable name "column1_field", which it says is (true, of course) undefined in the query.
When I tried "#cfqGetItem[column1_field][currentRow]#", it says "Variable currentrow is not defined".
When I tried "#cfqGetItem.column1_field#", it says "Element column1_field is not defined in query cfqGetItem".
I apologize in advance for not knowing/using all the correct terminology, and hope I am explaining this reasonably clearly. I suspect I will have to revert to writing individual form-page/action-page pairs. Thank you to all for your time and help!
ORIGINAL POST:
I'm not highly technical, and this is probably something simple, but here is
my dilemma, where I am attempting to retrieve a single record using a variable name in the query.
First, I define some variables:
<cfset ID = #Form.ID#<!--- the single record I want to retrieve, passed from a form --->
<cfset datasource = 'MyDatabase'>
<cfset query_tbl = 'MyDatabaseTable'>
<cfset field1 = 'actual_fieldname1'><!--- field in MyDatabaseTable --->
<cfset field2 = 'actual_fieldname2'><!--- field in MyDatabaseTable --->
ETC.
Then, to retrieve this single record, I run a query using those variables:
<cfquery name="cfqGetItem" datasource="#datasource#">
SELECT *
FROM #query_tbl#
WHERE ID = #ID#
</cfquery>
Then, I attempt to display the query output:
EITHER AS
<cfoutput>
<p>#cfqGetItem.field1#
<p>#cfqGetItem.field2#
</cfoutput>
OR, AS
<cfoutput>
<p>#cfqGetItem[field1][currentRow]#
<p>#cfqGetItem[field2][currentRow]#
</cfoutput>
In each case, I get a similar CF error message: "Element field1 is not defined in query cfqGetItem", or "Variable currentrow is not defined".
How can I get the query output to generate the actual values for the record instead of the variable names?
Thank you very much for any help!
If you change this:
<cfoutput>
<p>#cfqGetItem[field1][currentRow]#
<p>#cfqGetItem[field2][currentRow]#
</cfoutput>
to this:
<cfoutput>
<cfloop query="cfqGetItem">
<p>#cfqGetItem[field1][currentRow]#
<p>#cfqGetItem[field2][currentRow]#
</cfloop>
</cfoutput>
You should be ok. However, this is the simplest way
<cfoutput query="cfqGetItem">
<p>#actual_fieldname1#
<p>#actual_fieldname1#
</cfoutput>
Setting the table and field names to variables complicates matters. Unless they really can vary, don't do it. Also, the cfqueryparam tag is your friend. Use it in places like this:
where id = <cfqueryparam
cfsqltype = "cf_sql_integer"
value = "#id#">
You can try out like this also:
<cfquery
name="GetParks" datasource="cfdocexamples"
>
SELECT PARKNAME, REGION, STATE
FROM Parks
ORDER BY ParkName, State
</cfquery>
<cfoutput>
<p>#GetParks.PARKNAME#
<p>#GetParks.REGION#
</cfoutput>
<cfoutput >
#GetParks['state'][GetParks.RecordCount]#
</cfoutput>
<cfoutput >
#GetParks['state'][2]#
</cfoutput>
This question already has answers here:
Coldfusion - variable field name when looping through database query results
(2 answers)
Closed 9 years ago.
Edit: The QUESTION is a duplicate, but the ANSWER is not!
I have the following code:
<cfquery name="contact" datasource="thesource">
SELECT * FROM #table# WHERE foo = '#bar#'
</cfquery>
and then later on (this is the problem part):
<cfloop from="0" to="9" index="i">
<cfset thisvar = Evaluate("contact.check" & i) />
<cfoutput>
#thisvar#
</cfoutput>
</cfloop>
Upon execution, it throws a nice big "Variable contact.check0 is Undefined". However, #contact.check0# will output just fine if hardcoded.
Any ideas on how to fix this?
Note:
I have seen Coldfusion - variable field name when looping through database query results, and although the problem seems to be exactly the same, the solutions do not work. As per the comments, I am also getting a "cannot be converted to a number" error. I notice Tomalak mentions "a little catch", but never says what it is. In the article he links to, I have tried every syntactically equivalent form, and it all throws an error...either cannot convert to a number or is not defined.
Also, I'm aware Evaluate() has overhead and "should not be used". I'll take any solution that works, whether it has Evaluate in it or not.
This is on ColdFusion 9.
Thank you
Edit: while a similar question already had an answer, this question had a different cause. See accepted answer below.
First of all, standard blub about escaping inputs. cfqueryparam exists for a reason. That aside you can do this without evaluate. Give this a try instead, including a check to make sure there is actually a result
<!--- make sure that there is actually a result --->
<cfif contact.RecordCount EQ 0>
NO RESULT
<cfelse>
<cfloop from="0" to="9" index="i">
<!--- assumes that you want row 1 --->
<!--- check.CurrentRow could also be used instead of 1 --->
<cfset thisvar = contact['check'&i][1] />
<cfoutput>
#thisvar#
</cfoutput>
</cfloop>
</cfif>
I suspect that it is attempting to Evaluate the column without specification of the row number, while when hard-coding it you'll find it uses the current row.
edit as Dan Bracuk pointed out this is an exact duplicate of the question you linked in your question, Coldfusion - variable field name when looping through database query results
This is ridiculous and absurd. Working code (moved things around a bit):
<cfoutput>
<cfloop from="0" to="9" index="i">
<cfset thisvar = contact["check"&i][1] />
#thisvar#
</cfloop>
</cfoutput>
NOT working code:
<cfoutput query="contact"> <!--- this line here --->
<cfloop from="0" to="9" index="i">
<cfset thisvar = contact["check"&i][1] />
#thisvar#
</cfloop>
</cfoutput>
Someone else had put cfoutput tags around the whole page. While this normally wouldn't be a problem, they ALSO declared the query attribute. This somehow overwrote the original query from the db. Calling contact.check0 worked just fine, but building the variable name dynamically, I think, was attempting to reference the cfoutput's query attribute.
Thanks to Peter Boughton for the suggestion to extract the problem code...which led me to this realization, and Simon for posting the syntactically correct code. Points go to them.
The basic idea is this: I have a form that generates form fields dynamically so let's say there are 5 evens people can sign up for (they all cost $10) then those 5 evens will be displayed. Like this:
<tr>
<th><label>#SeminarWisTitle#</label></th>
<td>
<label><input type="checkbox" name="#SeminarWisID#" value="10.00" onclick="CheckChoice(this);" onfocus="startCalc();" onblur="stopCalc();" class="checkbox" /> Individual Webinar ($119)</label>
</tr>
</cfoutput>
Now because of the Javascript the value on all these events will be 10.00 but the NAME of the form field will be unique, and that is what I actaully want to store in the database.
This is the code I've written:
<cfparam name="seminarBulkSignUp_List" default="">
<cfoutput query="qSeminarWisTwo">
<cfparam name="FORM.#SeminarWisID#" default="">
<cfif #FORM[#SeminarWisID#]# neq "">
<cfset seminarBulkSignUp_List = ListAppend(seminarBulkSignUp_List, #FORM[#SeminarWisID#]#)>
</cfif>
</cfoutput>
<cfset FORM.SeminarWisTitle = #seminarBulkSignUp_List#>
So with this code, I run a query for ALL the possible events, and then just check against the form that has been submitted to see which ones are "blank" as in not selected, and the ones that are selected i want to add to a list to store in the database.
Now this works as far as letting me know which events were selected and which not, but i want the list to compile the actual FORM FIELD names not the value they have. How would I do that?
<cfoutput>
<cfloop list="#StructKeyList(FORM)#" index="thisField">
My field name: #thisField#<br/>
My field value: #FORM[thisField]#<br/>
</cfloop>
</cfoutput>
Apply as necessary.
I have a query and a list of field/column names. I want to do a sort of double loop - loop through each record in the query, and then loop through the list of field/column names and output each corresponding field. The loops should be something like this:
<table>
<cfoutput query="myQuery">
<tr>
<cfloop list="#cols#" index="col">
<td>?</td>
</cfloop>
</tr>
</cfoutput>
</table>
The problem is what to put where the question mark is... I've tried #myquery[col]#, but this didn't work. I need to get the variable indicated by the string name in the variable col... And obviously, #col# will just return the column name. I need to figure out some way to double-evaluate the string... something like ##col##, which of course won't work either. How can I accomplish this?
When referencing column names as a structure, you need to also tell the query which row you want to get. You should also make sure that you check that the column name exists if you didn't get the cols variable via myQuery.ColumnList.
Use the following code to dynamically reference each column in your loop:
<table>
<cfoutput query="myQuery">
<tr>
<cfloop list="#cols#" index="col">
<td>#myQuery[col][CurrentRow]#</td>
</cfloop>
</tr>
</cfoutput>
</table>
You can still use Sergii's approach with your own columnlist:
<cfloop list="#cols#" index="col">
<cfif StructKeyExists(myQuery, col)>
<td>#col# = #myQuery[col][myQuery.CurrentRow]#</td>
</cfif>
</cfloop>
Got it!! :)
#evaluate(evaluate("col"))#