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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is the best way to dynamically assign field names? 2

Status
Not open for further replies.

programmher

Programmer
May 25, 2000
235
US
I need to allow my users to create their own field names. The users are limited to 2 choices (i.e. - City, State). My problem is that as the user creates the new fields, I need the database to convert the field names to "City1", "State1", "City2", "State2", etc. The list and combinations of cities and states could be as short as one or as long as 99999.

Will an array accomplish this for me? If so, do I reference the array in my query that updates the database?

Here is what I have so far: (This does not perform ANY of the dynamic numbering, etc.)

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<form action=&quot;AddingFields.cfm&quot; method=&quot;post&quot;>New City:

<input type=&quot;text&quot; name=&quot;New City&quot;>

<br>

<input type=&quot;button&quot; value=&quot;add it now&quot;>

</form>

<cfquery name=&quot;addmynewfield&quot; datasource=&quot;MyDatabase&quot;>

ALTER TABLE tbl_City

ADD NewCity1 VARCHAR(50) NULL

</cfquery>

I would appreciate ANY insight!
 
I'm not sure I understand what the problem is exactly. Are you asking how to do a dynamic alter statement or how to convert a field name the user enters into a standard format?

A dynamic alter statement would just be something like

ALTER TABLE tbl_City ADD #NewCity1# VARCHAR(50) NULL

but I suspect this isn't what you're asking about. If you can give me a specific scenario, that would help as I don't see anything that appears to be difficult to do.

GJ
 
GJ,

My requirements have now changed. My client wants the user to click the &quot;add&quot; button and have a set of fields (&quot;city&quot; and &quot;state&quot;) appear on the form - one form. I have never done this - I (and my users) have always known exactly how many fields are needed.

What is the best way to do this? Below is what I have so far:


<th align=&quot;left&quot;><font size=&quot;-2&quot; color=&quot;Black&quot;>First City/State:</font></th>
<td><font size=&quot;-1&quot; color=&quot;Red&quot;>#First_city#</font></td>
<td><font size=&quot;-1&quot; color=&quot;Red&quot;>#First_state#</font></td>

</tr>

<cfform action=&quot;Add.cfm&quot; method=&quot;POST&quot; enablecab=&quot;Yes&quot;>

<table align=&quot;center&quot;><input type=&quot;submit&quot; value=&quot;Add&quot; width=&quot;1&quot; height=&quot;1&quot;>

</cfform>

When the &quot;Add&quot; button is selected, another set of city/state fields should appear under the first set.
 
&quot;When the &quot;Add&quot; button is selected, another set of city/state fields should appear under the first set. &quot; --> you mean, each time you want to display : first the &quot;input&quot; for both state and city, then the already existing states and cities - if any. And if your user adds a city(state) in the inputs, you want to refresh the page with the newly added city(state). Right ? or did i get it all wrong ? let me know, if i understood what you want to do, it's not tough at all

 
If you mean how do you let them add an unknown number of fields to a form, try something like this on the main page.

For the starting form..

<cfparam name=&quot;fieldCount&quot; default=&quot;1&quot;>

<cfloop index=&quot;x&quot; from=&quot;1&quot; to=#fieldCount#>

<form ....
<cfoutput>
<input name=&quot;city#x#&quot; ....>
<input name=&quot;state#x#&quot; ....>
</cfoutput>
</form>

</cfloop>

Then to add more...
<form ....>
<input type=&quot;hidden&quot; value=&quot;#evaluate(fieldCount+1)#&quot;>
<input type=&quot;submit&quot; value=&quot;add&quot;>
</form>

If you are wanting to know how to process an unknown number of fields, just label them in a consistent manner such as &quot;city1&quot;, &quot;city2&quot;, &quot;city3&quot;..... Then, in the receiving script, you could check these several ways but here's one that I think is fairly simple.

<cfset x=1>
<cfloop condition=&quot;isdefined(&quot;city#x#&quot;)&quot;>

.... code here to use each form variable in succession...
<cfset myNewVariable = evaluate(&quot;city#x#&quot;)>
..............

<cfset x=x+1>

Optional code to prevent runaway loops
<cfif x gt 100><cfbreak></cfif>
</cfloop>


Let me know if this helps,
GJ
 
Iza,

You are correct - that is what I want to do. Does your method greatly differ from GJ's?

GJ,

I am trying your suggestion now. I will post my progress.

Many thanks to you both!

 
Iza,

What is your suggested method of refreshing the same form after each added field? So far, I have been unable to achieve this...
 
i'm sorry i'm running out of time so i'll try to briefly explain - hope this will remain clear !!!!
the idea here is to submit the form to the same page it's on, and to run the &quot;add&quot; query only if we have values from the form. The query to read all existing cities and states have to be done every time, and the form and this query results as well.
the code is like this (not tested, ie only, but should work)

<cfif isdefined(&quot;form.city&quot;) and isdefined(&quot;form.state&quot;)> //then we know the form was submitted and we have values to insert
<cfquery name=&quot;add_one&quot;>
here is the query to ADD form.city and form.state in the db
</cfquery>
// you can add error handling stuff here
</cfif>

// now do the query to get existing cities &amp; states if any - this will read what has just been added as well !
<cfquery name=&quot;read_all&quot;>
this one is supposed to READ the table with cities / states - as it is always needed (even the first time) we do it every time
</cfquery>

// here is the form - your_page.htm is the page we are coding right now !!!!
<form name=&quot;the_form&quot; action=&quot;your_page.htm&quot; method=... >
<input type=text name=city ...>
<input type=text name=state ...>
<input type=submit value=&quot;add&quot;>
</form>

// now display the existing states and cities
<cfif the_query.recordcount gt 0> //this is for the &quot;if any&quot;
<cfoutput query=&quot;read_all&quot;>
output the way you want
</cfoutput>
</cfif>

and that's all
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top