I am using CF8 and MySQL 5.
I have a form with several date fields (one for each day and the form may have 10+ days of data on it) that the user can select different dates for and they all have different var names within a loop.
The default values for these date fields is null in the DB. I can insert a date into a DATE column in MySQL with no issues and have verified that the data is inserted correctly (using cfqueryparam DATE also).
I have the 'value' of the form datefield set to the variable name and can not get the value to show up.
The date updates to the DB fine every time it is entered, but when the form posts back to itself the date fields are blank (other non-date fields work fine and changes show up).
Then when I submit it with the blank date fields the value is set back to null in the DB (empty string in the form) since the form field does not pull the value from the DB.
The field name (ses#i#Date) shows up correctly (ses1Date, ses2Date, etc...) in the form with the right value when I dump it.
<cfloop from="1" to="#form.days#" index="i"> <cfinput type="datefield" name="ses#i#Date" value="#DateFormat(qGetUWHeader["ses#i#Date"],"yyyy-mm-dd")#" /> ....
Thanks for your time and help.
To reference dynamic column names, you can use array notation. But as I mentioned, you must supply a row number.
#queryName["columnName"][rowNumber]#
If you know the query contains one (1) record, only, you could use the query object's "recordCount" property as the row number. Alternatively, you could hard code the row number "1". (Personally, I dislike hard-coding). But any one of these should work.
<!--- pick ONE option --->
<cfloop from="1" to="#form.days#" index="i">
<!--- syntax option 1 --->
<cfinput type="datefield" name="ses#i#Date" value="#DateFormat(qGetUWHeader['ses#i#Date'][qGetUWHeader.recordCount], 'yyyy-mm-dd')#" />
<!--- syntax option 2 --->
<cfinput type="datefield" name="ses#i#Date" value="#DateFormat(qGetUWHeader['ses'& i &'Date'][qGetUWHeader.recordCount],'yyyy-mm-dd')#" />
<!--- syntax option 3 --->
<cfinput type="datefield" name="ses#i#Date" value="#DateFormat(qGetUWHeader['ses#i#Date'][1], 'yyyy-mm-dd')#" />
</cfloop>
If however, you are looping through multiple records in the qGetUWHeader query, you can use the query object's "currentRow" property as the row number. But based on the field naming convention, I am guessing the query only contains one (1) record.
EDIT:
I forgot about the initial nulls. You could apply a simple if condition, and only call DateFormat() if the query value is a valid date.
<cfloop from="1" to="#form.days#" index="i">
<cfset dateValue = qGetUWHeader["ses#i#Date"][qGetUWHeader.recordCount]>
<!--- if this is a valid date, format the value --->
<cfif IsDate(dateValue)>
<cfset dateValue = dateFormat(dateValue, "yyyy-mm-dd")>
</cfif>
<cfinput type="datefield" name="ses#i#Date" value="#dateValue#" /><hr>
</cfloop>
Another option is to format the dates in your SQL. Then you would not need to use CF's DateFormat() function. Just be aware that the new result would be a string, not a datetime object.
SELECT DATE_FORMAT(ses1Date, '%Y-%m-%d') AS ses1Date, ....
Where is "qGetUWHeader" defined?
When a form is posted, all values will be posted to the form scope. Therefore in order to display a value from a postback, you should reference the form scope unless you're copying the value into 'qGetUWHeader'. Even though the correct date from the form is being stored in the form scope, you're displaying the default value from qGetUWHeader on post back, unless you're doing something there I'm not aware of. Then the next time you post your form, the default value overrides the prior entered value.
<!--- In order to reference FORM values, you must CFPARAM them first to define the default value. --->
<cfparam name="form.days" default="10">
<cfloop from="1" to="#form.days#" index="i">
<cfparam name="form['ses#i#Date']" default="">
</cfloop>
<!--- Display the Form --->
<cfform action="#cgi.SCRIPT_NAME#" method="post">
<cfloop from="1" to="#form.days#" index="i">
<cfset thisFieldName = "ses" & i & "Date">
<cfset thisFieldValue = form["ses#i#Date"]>
<cfoutput>#thisFieldName#</cfoutput> <!--- For Debugging --->
<cfinput type="datefield" name="#thisFieldName#" value="#thisFieldValue#" /><br /><br />
</cfloop>
<input type="submit" name="submit" value="submit" />
</cfform>
<!--- Debug --->
<cfdump var="#form#">
Related
MarkLogic 9.0.8
On UI, apart from search text, few filters are applied including publish date year.
As we can't control how end user will write query in multiline textbox.
So at end query look like something
AU:Manish AND PY:>=2001 AND CT:Control AND UT:uncontrol AND PY:<=2010
(AU:Manish AND PY:>=2001) OR (CT:Control AND UT:uncontrol AND PY:<=2010)
AU:Manish AND CT:Control AND UT:uncontrol AND PY:>=2001 AND PY:<=2010
Till now we managed with having year range at the end in query and was working with following code
Qyery: AU:Manish AND CT:Control AND UT:uncontrol OR PY:>=2001 AND PY:<=2010
<additional-query>
{
cts:and-query((
cts:path-range-query("contg/sortdate/yr", ">=",xs:int($startYear)),
cts:path-range-query("contg/sortdate/yr", "<=",xs:int($endYear))))
}
</additional-query>
But now as user can put year range anywhere in the query text, its not working as expected
So can we write condition in constraint directly and how to put additional query inside ?
<constraint name="Year">
<range type="xs:int" facet="false">
<path-index>article/date/year</path-index>
</range>
</constraint>
Expected Behavior
If user pass year range then it should return documents within given range
if not then it will not apply year range
I have form where user can pick start/end date and time, after from is submitted I want to create createDateTime and use passed arguments. So I tried this way but I got only date stored in my DB. Here is my code:
<cfargument name="DateFrom" type="date" required="yes">
<cfargument name="DateTo" type="date" required="yes">
<cfargument name="TimeFrom" type="string" required="yes">
<cfargument name="TimeTo" type="string" required="yes">
<cfset StartDateTime = createDateTime(year(arguments.DateFrom), month(arguments.DateFrom), day(arguments.DateFrom), hour(arguments.TimeFrom), minute(arguments.TimeFrom), 0)>
<cfset EndDateTime = createDateTime(year(arguments.DateTo), month(arguments.DateTo), day(arguments.DateTo), hour(arguments.TimeTo), minute(arguments.TimeTo), 0)>
<cfquery name="addReservation" datasource="test">
Insert Into tableReserv(PickDateTime,DropDateTime)
Select <cfqueryparam cfsqltype="cf_sql_date" maxlength="12" value="#StartDateTime#">,
<cfqueryparam cfsqltype="cf_sql_date" maxlength="12" value="#EndDateTime#">
Where Not Exists(Select UserID
From aviRequests
Where PickDateTime < <cfqueryparam cfsqltype="cf_sql_timestamp" value="#StartDateTime#">
And DropDateTime > <cfqueryparam cfsqltype="cf_sql_timestamp" value="#EndDateTime#">
)
Select SCOPE_IDENTITY() As RecID;
</cfquery>
For some reason my StartDateTime and EndDateTime do not create date and time together. Here is how my value looks stored in DB:
Start:2016-01-20 00:00:00.000
End:2016-01-21 00:00:00.000
Is my createDateTime properly formatted or something else is wrong in my code?
This is from your question:
<cfset StartDateTime = createDateTime(year(arguments.DateFrom)
, month(arguments.DateFrom)
, day(arguments.DateFrom)
, hour(arguments.TimeFrom)
, minute(arguments.TimeFrom), 0)>
The date part is fine because your date arguments are dates. The time part is not because your time arguments are strings. You have to process that string to get the hours and minutes.
In the comments, you said your time strings resemble 7 AM. You have to process this to get the correct number of hours and minutes. Things you have to consider are:
Are the last two characters AM or PM?
Does the number of hours have 1 or 2 digits?
If it's not at the top of the hour, how are minutes represented?
Having said all that, you are probably not the first person to have this problem. Look at cflib.org to see if there a function you can use.
Having headache day again. Need to make some changes to some things and populate available stuff first. Basically from when something is open till it closes. And add available spots every 15 minutes.
I can get the start date and end date populating - but can't get the inside loop populating the increment times that need to be inserted... Little lost here... Any help is appreciated...
Start Date and End Date come from a cfform - works fine...
Start Date:<br>
<cfinput type="datefield" name="startDate" required="true" message="Start Date Required">
<br><br>
End Date:<br>
<cfinput type="datefield" name="EndDate" required="true" message="End Date Required">
<cfloop index="dtToday" from="#StartDate#" to="#EndDate#">
<cfoutput>
<br>#DateFormat(dttoday)#<br>
<cfloop index="incr" from="#TimeFormat(sadd.topen)#" to="#TimeFormat(sadd.tclose)#" step="#CreateTimeSpan( 0, 0, sadd.increment, 0 )#">
[#TimeFormat(incr)#]
Do Database Insert of Date/Time at increments
</cfloop>
<cfset schdate = #DateAdd('d', 1, '#schdate#')#>
</cfoutput>
</cfloop>
I modified your code so that I could test it quickly. CF Live shows this to work in Railo and CF.
It isn't clear where your trouble is coming from because I didn't make any drastic changes to your code.
<!--- You can get rid of these two form declarations, and the sadd declaration. This was just mimicking your data. --->
<cfset form.startdate = "11/17/95">
<cfset form.enddate = "12/20/95">
<cfset sadd = {topen= "13:00", tclose= "17:00", increment = 15}>
<cfif isDate(form.startdate) and isDate(form.enddate)>
<cfloop index="dtToday" from="#form.StartDate#" to="#form.EndDate#">
<cfoutput>
<br>#DateFormat(dttoday)#<br>
<cfloop index="incr" from="#TimeFormat(sadd.topen)#" to="#TimeFormat(sadd.tclose)#" step="#CreateTimeSpan( 0, 0, sadd.increment, 0 )#">
[#TimeFormat(incr)#] - Do Database Insert of Date/Time at increments<br>
</cfloop>
<!---<cfset schdate = #DateAdd('d', 1, '#schdate#')#>--->
</cfoutput>
</cfloop>
<cfelse>
One or both of the dates entered is invalid.
</cfif>
I changed:
I form scoped startdate and enddate, it shouldn't have any negative impact on your code, it just seemed logical that startdate. While this is proper practice, the only way it could be causing you issues is if you had variables in multiple scopes with the name of startdate and/or enddate. (Safety against that, and security against exploitation of that is exactly why scoping your variables is best practice).
For my sample data, I set start time to 13:00 (1:00 PM) and end time to 17:00 (5:00). You can also use normal format of "1:00 PM" and "5:00 PM".
I commented out schDate because its use wasn't apparent here.
I added a sadd scope to mimic what it seems like your data probably is. In the future, give samples of what your data actually looks like :)
I am currently working on a website that offers tutoring services and I have been stuck on this issue quite a while..
I have 2 text boxes where the user chooses a start date and a finish date, when the user clicks view he would be suppose to see the results in a gridview. I am using FormParameters to insert the date into the query.
SelectCommand of the sqldatasource
SelectCommand="SELECT [Session].Session_num AS Num, [Session].Session_Time_Stamp AS Session_Date, Student.Student_First & ' ' & Student.Student_Last AS Student FROM (([Session] INNER JOIN Student ON [Session].Student_Num = Student.Student_Num) INNER JOIN Class ON [Session].Class_ID = Class.Class_ID) WHERE ((([Session].Session_Time_Stamp) Between #firstdate And #seconddate))">
Parameters of the SqlDataSource
<SelectParameters>
<asp:FormParameter Name="firstdate" Type="DateTime"/>
<asp:FormParameter Name="seconddate" Type="DateTime"/>
</SelectParameters>
This is executed when the user clicks the view button, it is where I set the values of the parameters and execute the sql select.
Dim fdate As DateTime = Format(CDate(txtStartDate.Text), "MM/dd/yyyy")
Dim ldate As DateTime = Format(CDate(txtEndDate.Text), "MM/dd/yyyy")
gridTutor.SelectParameters("firstdate").DefaultValue = fdate
gridTutor.SelectParameters("seconddate").DefaultValue = ldate
gridTutor.Select(DataSourceSelectArguments.Empty)
gridTutorSessions.DataBind()
fdate and ldate are not empty, however after this is executed the query result is empty. Could it be that wrong methods to execute the select?
Edit: I realized the problem is probably with the query and DateTime format. When I transformed my textbox value to DateTime it put it like this #2/20/2014#. However, it doesn't return anything even if there are values between the two dates.
If anybody have an access query with DateTime I would like to see it. Thanks
I managed to fix it by formatting the date in my access database it's not the best solution but it is a fix for the situation
I believe you need to manually set fdate and ldate just before you do your 'selectparameters' (i.e. use "fdate = Format(CDate(txtStartDate.Text), "MM/dd/yyyy")". According to the following, values asigned to Dim in the manner you have would not reflect the realtime values you want. See the following regarding VB: "You can assign a value to a variable when it is created. For a value type, you use an initializer to supply an expression to be assigned to the variable. The expression must evaluate to a constant that can be calculated at compile time.
I found the ParseDateTime function but it only parses a date/time string according to the English (U.S.) locale conventions.
How to parse the date which is in dd/mm/yyyy format?
Try out this:
<cfset TestdateFrom = ParseDateTime("10/9/2010") />
<cfloop index="i" from="1" to="30" step="1">
<cfset TestdateFrom = DateAdd( "d", 1, TestdateFrom ) />
#TestdateFrom#<br/>
</cfloop>
In CF9 there is a LSParseDateTime function.
I don't know whether this will help me or not.
At last should I use java library for this issue?
Looks like this is working:
<cfset formatter = createObject("java","java.text.SimpleDateFormat")>
<cfset formatter.init("dd/MM/yyyy")>
<cfset newDate = formatter.parse("10/09/2010")>
#newDate#
Any other suggestions?
If your format is consistent, you can also do something like this:
<cfset dy=listGetAt(dateString,1,"/")>
<cfset mo=listGetAt(dateString,2,"/")>
<cfset yr=listGetAt(dateString,3,"/")>
<cfset myDate=createDate(yr,mo,dy)>
But, really, Tim's answer is the best.
I've had success with this technique for dealing with (UK formatted) dates entered as free text:
<cfset SetLocale("English (UK)")>
<cfset valid = true>
<!--- Convert DD-MM-YYYY or DD.MM.YYYY to DD/MM/YYYY --->
<cfset dt = replacelist(dt, ".,-", "/,/")>
<!--- count date elememnts (must be 3 - LSParseDateTime will make potentially incorrect assumptions otherwise) --->
<cfif listlen(dt, "/") neq 3>
<!--- wrong number of elements --->
<cfset valid = false>
<cfelse>
<!--- Correct number of elements so try to interpret as date/time object --->
<cftry>
<cfset dt = LSParseDateTime(dt)>
<cfcatch type="Expression">
<cfset valid = false>
</cfcatch>
</cftry>
</cfif>
If valid is true at the end of this, the string date representation in dt has been converted to a date/time object. The replacelist step allows for the date to be entered as DD.MM.YYYY or DD-MM-YYYY as well as DD/MM/YYYY.
You need to use lsParseDateTime(). This is the locale specific date parser.
You can either pass a locale straight into the second parameter of the function or use setlocale() to set it for the request.
You'll probably find it useful to look at the other LS prefixed functions too.