Ask a Question related to Coldfusion - Getting Started, Design and Development.

  1. #1

    Default Problem Looping

    I am trying to create an array of structures using data collected from a form.
    The form contains 5 sets of seven fields each set containing various
    information for a different person. The fields in each set are named with a
    corresponding prefix identifying that particular person's role (e.g., role_1,
    role_2, etc.), and a corresponding suffix identifing what part of that person'd
    data it contains (e.g., first name, middle name, last name, ID, etc.) so that a
    set of fields for any given person would look like this:

    roleX_ID
    roleX_FName
    roleX_LName
    roleX_EMail
    etc.

    For the purpose of inserting this data into a database, I want to separate the
    prefix from the suffix and creat a CF Data Structure for each set of fields so
    that the keys of each structure are generically renamed such as:

    ID
    FName
    LName
    EMail
    etc.

    Then insert each of these structures into an Array ending up with an array of
    data structures.

    So far, I am able to creat one giant structure containng all of the form
    fields with their field names (roleX_ID, etc.) as the keys and the field values
    as the values in the structure.

    From here I dump all of the keys of this structure into an Array using --
    structKeyArray(aryPersonnel) -- and loop through that array looking for
    particular elements (ID, EMail, etc.) to be contained in the key. If it
    matches a condition, I set a new value in a new structure equal to the valuel
    that matches the key I'm examining.

    Nice plan, but unfortunatly it does not render my desired result. I end up
    with an array that contains 35 different structures instead of an array
    containing 5 structures each with the seven key/value pairs of information.

    I believe the problem may be in the way I am looping through the array, since
    the conditions I have set would be met more than once through each iteration of
    the loop.

    I'm almost out of ideas so any help would be greatly appreciated. I have
    attached some code for review.

    -- Thanks in advance

    <!--- Initialize a new structure --->
    <cfset aryPersonnel= StructNew()>

    <!--- Loop through the form and extract field values into one big structure
    --->

    <cfloop collection="#form#" item="var">

    <cfif var CONTAINS "ID1">
    <cfset ID1Field = "Form." & var>
    <cfparam name="#ID1Field#" default="">
    <cfset ID1 = StructInsert(aryPersonnel, "#var#",
    "#Evaluate(ID1Field)#")>
    </cfif>

    <cfif var CONTAINS "ID2">
    <cfset ID2Field = "Form." & var>
    <cfparam name="#ID2Field#" default="">
    <cfset ID2 = StructInsert(aryPersonnel, "#var#",
    "#Evaluate(ID2Field)#")>
    </cfif>

    <cfif var CONTAINS "FName">
    <cfset FNameField = "Form." & var>
    <cfparam name="#FNameField#" default="">
    <cfset FName = StructInsert(aryPersonnel, "#var#",
    "#Evaluate(FNameField)#")>
    </cfif>

    <cfif var CONTAINS "MName">
    <cfset MNameField = "Form." & var>
    <cfparam name="#MNameField#" default="">
    <cfset MName = StructInsert(aryPersonnel, "#var#",
    "#Evaluate(MNameField)#")>
    </cfif>

    <cfif var CONTAINS "LName">
    <cfset LNameField = "Form." & var>
    <cfparam name="#LNameField#" default="">
    <cfset LName = StructInsert(aryPersonnel, "#var#",
    "#Evaluate(LNameField)#")>
    </cfif>

    <cfif var CONTAINS "Phone">
    <cfset PhoneField = "Form." & var>
    <cfparam name="#PhoneField#" default="">
    <cfset Phone = StructInsert(aryPersonnel, "#var#",
    "#Evaluate(PhoneField)#")>
    </cfif>

    <cfif var CONTAINS "EMail">
    <cfset EMailField = "Form." & var>
    <cfparam name="#EMailField#" default="">
    <cfset EMail = StructInsert(aryPersonnel, "#var#",
    "#Evaluate(EMailField)#")>
    </cfif>
    <cfset "#var#" = Trim(Evaluate(var))> <!--- commonizer --->

    </cfloop>

    <!--- Collect all of the key of the above structure into an array --->
    <cfset keys = structKeyArray(aryPersonnel)>

    <cfoutput>
    <!--- Initialize a new array for a new structure --->
    <cfset aryPeople = ArrayNew(1)>

    <!--- Loop through the array of key names and determine the value for each
    key and insert it into an array of structures --->
    <cfloop from="1" to="#ArrayLen(keys)#" index="i">

    <!--- initialize a new structure --->
    <cfset aryPeople[i] = StructNew()>

    <cfif keys[i] CONTAINS "ID1">
    <cfset aryPeople[i].ID1 = "#structFind(aryPersonnel, keys[i])#">
    </cfif>

    <cfif keys[i] CONTAINS "ID2">
    <cfset aryPeople[i].ID2 = "#structFind(aryPersonnel, keys[i])#">
    </cfif>

    <cfif keys[i] CONTAINS "FName">
    <cfset aryPeople[i].FName = "#structFind(aryPersonnel, keys[i])#">

    </cfif>

    <cfif keys[i] CONTAINS "MName">
    <cfset aryPeople[i].MName = "#structFind(aryPersonnel, keys[i])#">

    </cfif>

    <cfif keys[i] CONTAINS "LName">
    <cfset aryPeople[i].LName = "#structFind(aryPersonnel, keys[i])#">

    </cfif>

    <cfif keys[i] CONTAINS "Phone">
    <cfset aryPeople[i].Phone = "#structFind(aryPersonnel, keys[i])#">

    </cfif>

    <cfif keys[i] CONTAINS "EMail">
    <cfset aryPeople[i].EMail = "#structFind(aryPersonnel, keys[i])#">

    </cfif>

    </cfloop>
    <!---
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~ ---->
    <!--- When the above code is dumped out, the end result is a array with 35 (7
    fields X 5 people) indivual sturctures (one for each field value) --->
    <!--- All of the values are correct and the key is correct except there is
    only one key/value pair per structure (instead of 7) --->
    <!--- What I am trying to achieve is an array with 5 structures each with 7
    key/value pairs --->
    <!---
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~ ---->

    mushBug Guest

  2. Similar Questions and Discussions

    1. problem looping through queries
      I am attempting to create a loop that will give me all records in a table whose ids are set to a certain value in a related table. The logic I am...
    2. Problem with looping over cfhttp
      Hi, I am trying to get some image files from a remote server and save them to my server. I have all the paths in a query but when I loop over...
    3. looping over a list problem
      I am having a problem looping over a list with some elements that are empty. Here is a sample of the list: address_street=8349+White+Dr custom=...
    4. [PHP] problem looping through records
      Huzz wrote: Yeah, that's all it should show. It's going to show you the value for the 'en' column. Try a print_r($row1) so you can see what...
    5. Looping Music - Problem!!
      I have edited a piece of music that I know will loop properly, however when I try to use it in my movie looped, there is a slight pause at the end...
  3. #2

    Default Re: Problem Looping

    If you have 35 form variables that you want to insert into a database, why not
    just do it? What do you need to create arrays, structures, or anything else at
    all?

    Originally posted by: mushBug
    I am trying to create an array of structures using data collected from a form.
    The form contains 5 sets of seven fields each set containing various
    information for a different person. The fields in each set are named with a
    corresponding prefix identifying that particular person's role (e.g., role_1,
    role_2, etc.), and a corresponding suffix identifing what part of that person'd
    data it contains (e.g., first name, middle name, last name, ID, etc.) so that a
    set of fields for any given person would look like this:

    roleX_ID
    roleX_FName
    roleX_LName
    roleX_EMail
    etc.

    For the purpose of inserting this data into a database, I want to separate the
    prefix from the suffix and creat a CF Data Structure for each set of fields so
    that the keys of each structure are generically renamed such as:

    ID
    FName
    LName
    EMail
    etc.

    Then insert each of these structures into an Array ending up with an array of
    data structures.

    So far, I am able to creat one giant structure containng all of the form
    fields with their field names (roleX_ID, etc.) as the keys and the field values
    as the values in the structure.

    From here I dump all of the keys of this structure into an Array using --
    structKeyArray(aryPersonnel) -- and loop through that array looking for
    particular elements (ID, EMail, etc.) to be contained in the key. If it
    matches a condition, I set a new value in a new structure equal to the valuel
    that matches the key I'm examining.

    Nice plan, but unfortunatly it does not render my desired result. I end up
    with an array that contains 35 different structures instead of an array
    containing 5 structures each with the seven key/value pairs of information.

    I believe the problem may be in the way I am looping through the array, since
    the conditions I have set would be met more than once through each iteration of
    the loop.

    I'm almost out of ideas so any help would be greatly appreciated. I have
    attached some code for review.

    -- Thanks in advance



    Dan Bracuk Guest

  4. #3

    Default Re: Problem Looping

    That's a really great question, I probably should have clarified a bit more.
    The reason I need them all in an array of data structures is so I can
    manipulate how I enter them into the database. They aren't going to just be
    placed into just one table. They will all need to be inserted into several
    different tables and checks would have to be run to see if they are in the
    table first before an insert is made. Having them in an array of structures
    will enable me to run the appropriate checks and write an INSERT statement only
    once per table by looping through the array and depositing the desired
    information into the appropriate fields.

    mushBug Guest

  5. #4

    Default Re: Problem Looping

    You can do all that with the original form fields, well, I can anyway.

    Originally posted by: mushBug
    That's a really great question, I probably should have clarified a bit more.
    The reason I need them all in an array of data structures is so I can
    manipulate how I enter them into the database. They aren't going to just be
    placed into just one table. They will all need to be inserted into several
    different tables and checks would have to be run to see if they are in the
    table first before an insert is made. Having them in an array of structures
    will enable me to run the appropriate checks and write an INSERT statement only
    once per table by looping through the array and depositing the desired
    information into the appropriate fields.



    Dan Bracuk Guest

  6. #5

    Default Re: Problem Looping

    Not sure I quite understand, care to share?
    mushBug Guest

  7. #6

    Default Re: Problem Looping

    Do this for each table.

    <query name ="q1">
    select count(fieldname) records
    from table1
    where fieldname = #form.fieldname# <!--- you'll need quotes if field is char
    --->
    </query>

    <cfif q1.records is 0>
    insert the record
    <cfelse>
    do something else, update the record maybe
    </cfif>


    Dan Bracuk Guest

  8. #7

    Default Re: Problem Looping

    Thanks for this!

    This is very helpful. I have actually thought about something like earlier,
    my caviot is that my form field names aren't named the same as the database
    fields. But this gives me another idea that may work. I believe I could loop
    through my already existing structure or even the form itself, using the same
    type using the same type of query and if the form.fieldName name contains a
    desired element do whatever.

    Thanks much for your thoughts!

    It may take a little refining but something like this:

    <cfloop collection="#form#" item="var">
    <cfif var CONTAINS "roleX">
    <query name="q1">
    INSERT INTO users (
    roleX_ID,
    roleX_FName,
    roleX_LName
    )
    VALUES (
    '#Form.roleX_ID#',
    '#Form.roleX_FName#',
    '#Form.roeX_LName#'
    )
    </query>
    </cfif>
    </cfloop>

    mushBug Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139