Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I want to use cfloop but....

Status
Not open for further replies.

johnhig

MIS
Jun 25, 2007
54
US
Hi Everyone,

I have a form that has fields to it. I want the user to be able to enter up to 15 entries. I used a loop to have the additional rows display...

here is the form code.
<tr>
<td width="48">&nbsp;</td>
<td width="48">Date</td>
<td width="1">&nbsp;</td>
<td width="139">Customer</td>
<td width="1">&nbsp;</td>
<td width="180">Destination</td>
<td width="8">&nbsp;</td>
<td width="84">Start Mileage</td>
<td width="6">&nbsp;</td>
<td width="85">End Mileage </td>
<td width="3">&nbsp;</td>
<td width="100">NY Thruway </td>
<td width="3">&nbsp;</td>
<td width="62">State</td>
<td width="1">&nbsp;</td>
<td width="62">Total</td>
</tr>
<tr>
<td><cfloop from = "1" to = "15" index = "tripentry">
<input name = "#tripentry#" type = "hidden" size="5" maxlength="5">
</cfloop></td>
<td><cfloop from = "1" to = "15" index = "tripdate">
<input name = "#tripdate#" type = "text" size="15" maxlength="15">
</cfloop></td>
<td>&nbsp;</td>
<td><cfloop from = "1" to = "15" index = "tripcust">
<input type = "text" name = "#tripcust#"></cfloop></td>
<td>&nbsp;</td>
<td><cfloop from = "1" to = "15" index = "tripdest">
<input type = "text" name = "#tripdest#"></cfloop></td>
<td>&nbsp;</td>
<td><cfloop from = "1" to = "15" index = "tripstart">
<input name = "#tripstart#" type = "text" size="10" maxlength="10">
</cfloop></td>
<td>&nbsp;</td>
<td><cfloop from = "1" to = "15" index = "tripend">
<input name = "#tripend#" type = "text" size="10" maxlength="10">
</cfloop></td>
<td>&nbsp;</td>
<td><cfloop from = "1" to = "15" index = "tripthru">
<input name = "#tripthur#" type = "text" size="5" maxlength="5">
</cfloop></td>
<td>&nbsp;</td>
<td><cfloop from = "1" to = "15" index = "tripstate">
<input name = "#tripstate#" type = "text" size="5" maxlength="5">
</cfloop></td>
<td>&nbsp;</td>
<td><cfloop from = "1" to = "15" index = "triptotal">
<input name = "#triptotal#" type = "text" size="5" maxlength="5">
</cfloop></td>
</tr>

Now I am stuck. I am trying to learn the cfloop so please be kind. I am reading my book and trying out the code on test pages.

The next piece I would want to do is just cfloop through on my action page to insert those records. So when I go to write my insert code I get confused. Do I just loop a formfield?

Any help is so greatly appreciated.
John
 
<cfoutput>
<cfloop from=1 to=15 index=i>
<tr>
<td><input name = "tripentry#i#" type = "hidden" size="5" maxlength="5"></td>
<td><input name = "tripdate#i#" type = "text" size="15" maxlength="15">
</td>
...
</tr>
</cfloop>
</cfoutput>
hope it helps...


 
It does. I also found a tutorial on Easycfm.com that walked me through the same suggestion that you offered.

Thank you again.

I think I got it.

See below:
<tr>
<td class="five">Date</td>
<td class="five">Customer</td>
<td class="five">Destination</td>
<td class="five">Start Mileage</td>
<td class="five">End Mileage</td>
<td class="five">NY Thruway</td>
<td class="five">State</td>
<td class="five">Total</td>
</tr>
<cfoutput>
<!--- Gets the value of getnumba from above --->
<input type="hidden" name="numba" value="#getnumba#">
<!--- Loop Through from 1 to the number above --->
<cfloop from="1" to="#getnumba#" index="idx">
<!--- At first, it displays the original one row form --->
<!--- User enters the data and hits "Add Another" --->
<!--- The page reloads with the data already entered and creates a new blank row that is ready for data entry --->
<tr>
<cfif isdefined ("form.tripdate#idx#")>
<Td class="two"><input type="text" name="tripdate#idx#" value="#evaluate("form.tripdate#idx#")#"></TD>
<cfelse>
<Td class="two"><cfinput type="text" name="tripdate#idx#"></TD>
</cfif>
<cfif isdefined ("form.tripcust#idx#")>
<Td class="two"><cfinput type="text" name="tripcust#idx#" value="#evaluate("form.tripcust#idx#")#"></TD>
<cfelse>
<Td class="two"><cfinput type="text" name="tripcust#idx#"></TD>
</cfif>
<cfif isdefined ("form.tripdest#idx#")>
<Td class="two"><cfinput type="text" name="tripdest#idx#" value="#evaluate("form.tripdest#idx#")#"></TD>
<cfelse>
<Td class="two"><cfinput type="text" name="tripdest#idx#"></TD>
</cfif>
<cfif isdefined ("form.tripstart#idx#")>
<Td class="two"><cfinput type="text" name="tripstart#idx#" value="#evaluate("form.tripstart#idx#")#"></TD>
<cfelse>
<Td class="two"><cfinput type="text" name="tripstart#idx#"></TD>
</cfif>
<cfif isdefined ("form.tripend#idx#")>
<Td class="two"><cfinput type="text" name="tripend#idx#" value="#evaluate("form.tripend#idx#")#"></TD>
<cfelse>
<Td class="two"><cfinput type="text" name="tripend#idx#"></TD>
</cfif>
<cfif isdefined ("form.tripthru#idx#")>
<Td class="two"><cfinput type="text" name="tripthru#idx#" value="#evaluate("form.tripthru#idx#")#"></TD>
<cfelse>
<Td class="two"><cfinput type="text" name="tripthru#idx#"></TD>
</cfif>
<cfif isdefined ("form.tripstate#idx#")>
<Td class="two"><cfinput type="text" name="tripstate#idx#" value="#evaluate("form.tripstate#idx#")#"></TD>
<cfelse>
<Td class="two"><cfinput type="text" name="tripstate#idx#"></TD>
</cfif>
<cfif isdefined ("form.triptotal#idx#")>
<Td class="two"><cfinput type="text" name="triptotal#idx#" value="#evaluate("form.triptotal#idx#")#"></TD>
<cfelse>
<Td class="two"><cfinput type="text" name="triptotal#idx#"></TD>
</cfif>
</tr>
</cfloop>
</cfoutput>
<tr>
<td class="two" colspan="8"><input type="button" name="add" value="Add Another" onClick="javascript:getnutherline();"></td>
</tr>

Then the input:
<cfloop from="1" to="#form.numba#" index="idx">
<cfset getdate = evaluate("form.tripdate" & idx)>
<cfset getcust = evaluate("form.tripcust" & idx)>
<cfset getdest = evaluate("form.tripdest" & idx)>
<cfset getstart = evaluate("form.tripstart" & idx)>
<cfset getend = evaluate("form.tripend" & idx)>
<cfset getthru = evaluate("form.tripthru" & idx)>
<cfset getstate = evaluate("form.tripstate" & idx)>
<cfset gettotal = evaluate("form.triptotal" & idx)>
<cfquery name="data" datasource="intranet">
insert into mileage (tripdate,tripcust,tripdest,tripstart,tripend,tripthru,tripstate,triptotal)
values (#getdate#,'#getcust#','#getdest#','#getstart#','#getend#','#getthru#','#getstate#','#gettotal#')
</cfquery>
</cfloop>
 
The only piece that is not really working is the tripdate field is inserting as a 1/1/1900 even though I have a date entered.

John
 
instead of
value="#evaluate("form.tripdate#idx#")#

perhaps, you can use this instead

<cfif StructKeyExists(FORM, 'tripDate'&i)>
StructFindKey(FORM,'tripDate'&i)
</cfif>

not that anything is wrong with the first one...


 
The only piece that is not really working is the tripdate field is inserting as a 1/1/1900 even though I have a date entered.

You are not passing the value as a datetime object. So I suspect the date string is treated as a number. Example, 12/02/2008 would be converted as MM divided by DD divided by YYYY. The database then interprets that number value 0.000853... as a date. In your case 01/01/1900.

I use cfqueryparam for form values. To insert a date string into a nullable datetime field I use something like this. It inserts the given value if it is a valid date. Otherwise NULL is inserted.

Code:
<cfloop index="idx" ...>
   <cfset getdate = form["tripdate" & idx]>
   ... populate other variables 
   
   <cfquery name="insertRecord" datasource="MyDatasource">
      INSERT INTO MyTable (MyDateTimeColumn, ...OtherColumns ...)
	  VALUES (
		<cfqueryparam value="#getDate#" cfsqltype="cf_sql_date" null="#not IsDate(getDate)#">
		, ... other columns ...
	  )	
   </cfquery>
</cfloop>

There are other date functions available such as CreateODBCDate and ParseDateTime. Be aware many of the date functions use the formatting conventions of the US locale. If your locale is not the US, try the LS date functions instead.


----------------------------------
 
ok..I am stuck again.

I know that this is simple but I can figure it out. I have added some rows that output a projectid and a taskid.

<tr>
<td colspan="2">RO:</td>
<td colspan="2"><cfoutput query="gettask">#gettask.ProjectID#</cfoutput></td>
<td>&nbsp;</td>
<td>TaskID:</td>
<td><cfoutput query="gettask">#gettask.TaskID#</cfoutput>
</td>
<td>&nbsp;</td>
<td colspan="2">&nbsp;</td>
<td width="244" colspan="5">&nbsp;</td>
</tr>

I would like to also insert these on my action page but I can't figure out how to add them to insert query. I can get the code to pass the pages but it does not insert the projectid and taskid into the database. The other forms insert correctly but those two pass blanks, not even NULLS.

Any suggestions? Thanks John
 
I actually got it to work. Just needed to tweek my form variables.

Thanks Though

John
 
I have one more question with this cfloop.

Can I make the following

<cfinput type="text" name="tasktype#idx#" value="#evaluate("form.tasktype#idx#")#" size="5" maxlength="5">

reference a cfselect like this

<cfselect name="tasktype" query="jobtypes" value="jobtypevalue" display="jobtype"></cfselect>

I would like a couple of these form fields to be a drop down, but I don't know how to make the evaluate work with the cfselect or if I even could.

Thanks John



 
I have playing trying to figure out how to do this and have come up with the following...although it still does not work, but am I on the right track??

so I tried this

<cfif isdefined ("jobtypes.tasktype#idx#")>
<cfselect name="tasktype#idx#" query="jobtypes" value="#evalute(jobtypes.jobtypevalue#idx#)#" display="jobtype"></cfselect>
<cfelse>
<cfinput type="text" name="tasktype#idx#" size="5" maxlength="5">
</cfif>

any suggestions?? it is the value part that I am having a problem with.
 
In the jobtypes query, I am just displaying the name column in a drop down. That is all.

 
Yes, but what exactly are you trying test with your <cfif> condition? Are you trying to display a select list if a certain form field exists, otherwise display a text box?



----------------------------------
 
oh okay.

The <cfif> is the condition that checks to see if it needs to dynamically add rows to the form.

so I have my little query for my cfselect
<CFQUERY NAME="JobTypes" DATASOURCE="intranet" DBTYPE="ODBC">
SELECT *
FROM JobTypes
ORDER BY JobType
</CFQUERY>

the I check for additional rows.

<cfif isdefined ("form.numba")>
<cfset getnumba = form.numba + 1>
<cfelse>
<cfset getnumba = 1>
</cfif>

then
<!--- If there is more than one record, it returns the cursor to the new row --->
<cfif getnumba GT 1>
<cfset focusonme = " document.all.osform.partno" & getnumba & ".focus();">
<cfelse>
<cfset focusonme = "document.all.osform.partno.focus();">
</cfif>

then
<cfoutput>
<!--- function dofocusthing essentially puts the cursor either on the original first row (if nothing has been entered) or if "Add Another" has been clicked and a new row created it returns it to the first field of the new row --->
<!--- fuction submitform submits the form --->
<!--- function getnutherline reloads the page with the information stored in it--->
<script type="text/javascript">
function dofocusthing(){
#focusonme#
}
function submitform(){
document.all.osform.action = '/intranet/projecttracking/partsaction2.cfm?ProjectID=#gettask.ProjectID#&TaskID=#gettask.TaskID#';
document.all.osform.submit();
}
function getnutherline(){
document.all.osform.action = '/intranet/projecttracking/partstaskdetails3.cfm?ProjectID=#gettask.ProjectID#&TaskID=#gettask.TaskID#';
document.all.osform.submit();
}
</script>
</cfoutput>

then
<cfform>
<cfoutput>
<!--- Gets the value of getnumba from above --->
<input type="hidden" name="numba" value="#getnumba#">
<!--- Loop Through from 1 to the number above --->
<cfloop from="1" to="#getnumba#" index="idx">
<!--- At first, it displays the original one row form --->
<!--- User enters the data and hits "Add Another" --->
<!--- The page reloads with the data already entered and creates a new blank row that is ready for data entry --->
<tr>
<cfif isdefined ("form.tasktype#idx#")>
<Td class="two"><cfinput type="text" name="tasktype#idx#" value="#evaluate("form.tasktype#idx#")#" size="5" maxlength="5"></TD>
<cfelse>
<Td class="two"><cfinput type="text" name="tasktype#idx#" size="5" maxlength="5"></TD>
</cfif>

......
</cfform>


 
Just check if the form field is defined. If it is, save the value in variable. Then use the value as the "selected" attribute.

Code:
<cfloop ...>
   <!--- set a default value for new lists --->
   <!--- this will do nothing if "" does not exist in your query --->
   <cfset preselectListValue = "" />
   <cfif isdefined ("form.tasktype#idx#")>
      <cfset preselectListValue = form["taskType"& idx]>
   </cfif>
   <cfselect name="tasktype#idx#" query="jobtypes" 
	value="jobtypevalue" 
	selected="#preselectListValue#"
	display="jobtype" />

...
</cfloop>

I think you could use <cfform preservedata="yes"..>


----------------------------------
 
The variable. But I may try preservedata on my test page as I am now trying to learn about preservedata and preselectListValue.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top