Ask a Question related to Macromedia ColdFusion, Design and Development.

  1. #1

    Default update conundrum

    Hey, I have a slight issue on my hands and don't know how to approach it.

    Here's my situation: I have a form which allows the user to update
    information based on a certain offer. On that page I display all the states
    that the offer is allowed for. Now the user can "turn off" a state to not have
    that offer active. I am using radiobuttons to display the list of all states.
    The problem is when I try to change the state's preference I get an error
    stating that the "state_id" is undefined. I know why I am getting that error,
    I am looking for help to get around it.

    If you look at my code you will see that I am using dynamic values for the
    "name" attribute for the radiobutton.

    ================================================== ====================
    CODE
    <!--- selects the states and its isActive preference from the table --->
    <cfquery name="getStates" datasource="#ds#">
    select u.*, s.*, o.*
    from updatestates u join states s on u.state_id = s.state_id join offers o
    on u.offer_id = o.offer_id
    where offer_id = #form.offer_id#
    </cfquery>

    <cfoutput query="getStates">
    <!--- 'code' displays the states name onto the screen, such as NEW YORK or
    CALIFORNIA or etc --->
    #code#:
    <!--- the 'name=' displays the numerical reference of the states from the
    table --->
    <input type="radio" name="#state_id#" value="1" <cfif isActive is
    "1">checked</cfif>>YES |
    <input type="radio" name="#state_id#" value="0" <cfif isActive is
    "0">checked</cfif>>NO
    </cfoutput>

    <!--- UPDATE statement --->
    <cfquery name="updateStates" datasource="#ds#">
    update updatestates
    set isActive = 0
    where offer_id = #form.offer_id# and state_id = WHAT GOES HERE??
    </cfquery>
    ================================================== ====================

    So basically once processed my form looks like:
    ================================================== ====================
    CODE
    Alabama:
    <input type="radio" name="1" value="1" checked> YES | <input type="radio"
    name="1" value="0"> NO

    Arizona:
    <input type="radio" name="2" value="1" checked> YES | <input type="radio"
    name="2" value="0"> NO

    California:
    <input type="radio" name="3" value="1"> YES | <input type="radio" name="3"
    value="0" checked> NO

    New York:
    <input type="radio" name="4" value="1" checked> YES | <input type="radio"
    name="4" value="0"> NO

    Florida:
    <input type="radio" name="5" value="1"> YES | <input type="radio" name="5"
    value="0" checked> NO
    ================================================== ====================

    You can see that the "name" has the dynamic value associated with it. So how
    do I update NEW YORK from 1 to a 0 in my update statement??

    Any reason why I can't use this:
    ================================================== ====================
    CODE
    <!--- UPDATE statement --->
    <cfquery name="updateStates" datasource="#ds#">
    update updatestates
    set isActive = 0
    where offer_id = #form.offer_id# and state_id = #form.state_id#
    </cfquery>
    ================================================== ====================

    So when the query is run it would look like:
    update updatestates
    set isActive = 0
    where offer_id = 123 and state_id = 1

    where it sets the 'isActive' column to 0 where offer_id is 123 and state_id is
    1.

    Wants2learn Guest

  2. Similar Questions and Discussions

    1. Module naming conundrum
      Greetings, all. I have three modules I am preparing for submission to CPAN, but I freely confess that I can't come up with decent names for them. ...
    2. XML Socket - Header conundrum...
      I'm desperately trying to found out how to add a header to my socket message sends. I'm communicating via flash to a c++ server, and of course all...
    3. color conundrum
      Is there any speedy way to replace either black or white in an image with another color? "Replace color" only gives shades of gray as options, and...
    4. Clunky Cache Code Conundrum?
      I am storing all my application data in the application cache. Anytime I have a method as part of an asp.net form, I need to access the objects in...
    5. keyboard conundrum
      Alan Coopersmith <alanc@alum.calberkeley.org> wrote in message news:<bd5tvj$2omc$3@agate.berkeley.edu>... If not using XKB, the offset is So in...
  3. #2

    Default Re: update conundrum

    In the action page before the update statement, you will need to re do the
    getStates query.

    The loop through the result set, but this time checking the form field and
    create 2 list
    <cfset isActiveList = "">
    <cfset notActiveList = "">
    <cfoutput query="getStates">
    <cfif IsDefined("form." & state_id)>
    <cfif form[state_id] Is 1>
    <cfset isActiveList = ListAppend(isActiveList, state_id)>
    <cfelse>
    <cfset isActiveList = ListAppend(notActiveList, state_id)>
    </cfif>

    Then you need 2 update queries, and qualify the lists as the state_id is text
    <cfquery name="updateStates" datasource="#ds#">
    update updatestates
    set isActive = 0
    where offer_id = #form.offer_id# and state_id = #ListQualify(notActiveList,
    "'", ",", "All")#
    </cfquery>
    <cfquery name="updateStates" datasource="#ds#">
    update updatestates
    set isActive = 1
    where offer_id = #form.offer_id# and state_id = #ListQualify(isActiveList,
    "'", ",", "All")#
    </cfquery>


    Ken

    The ScareCrow Guest

  4. #3

    Default Re: update conundrum

    Ken,

    Thanks for the help. You helped me out, I had to tweak the code a bit but the
    end result, had it working right:

    Well, as you all know my form calls the states dynamically and the results
    look like:

    ================================================== ====================
    CODE
    Alabama:
    <input type="radio" name="1" value="1" checked> YES |
    <input type="radio" name="1" value="0"> NO

    Arizona:
    <input type="radio" name="2" value="1" checked> YES |
    <input type="radio" name="2" value="0"> NO

    California:
    <input type="radio" name="3" value="1"> YES |
    <input type="radio" name="3" value="0" checked> NO

    New York:
    <input type="radio" name="4" value="1" checked> YES |
    <input type="radio" name="4" value="0"> NO

    Florida:
    <input type="radio" name="5" value="1"> YES |
    <input type="radio" name="5" value="0" checked> NO
    ================================================== ====================
    So I needed a way to determine what states need to get updated, and thus:
    ================================================== ====================
    CODE
    <!--- set a blank list for states on the active list and not on the active
    list --->
    <cfset isActiveList = "">
    <cfset notActiveList = "">

    <!--- re-query the table to pull back the info --->
    <cfquery name="getStates" datasource="#ds#">
    select u.*, s.*, o.*
    from updatestates u join states s on u.state_id = s.state_id join offers o
    on u.offer_id = o.offer_id
    where offer_id = #form.offer_id#
    </cfquery>

    <!--- go the form and its states and update accordingly --->
    <cfoutput query="getStates">
    <cfif isdefined("form.fieldnames")>
    <cfif form[state_id] is 1>
    <cfset theState = #state_id#>
    <cfset isActiveList = ListAppend(isActiveList, #state_id#)>

    <cfquery name="updateStates" datasource="#default_ds#">
    update showofferstates
    set isActive = 1
    where offer_id = #form.OfferID# and state_id = #theState#
    </cfquery>
    <cfelse>
    <cfset theState = #state_id#>
    <cfset notActiveList = ListAppend(notActiveList, #state_id#)>

    <cfquery name="updateStates" datasource="#default_ds#">
    update showofferstates
    set isActive = 0
    where offer_id = #form.OfferID# and state_id = #theState#
    </cfquery>
    </cfif>
    </cfif>
    </cfoutput>
    ================================================== ====================

    Wants2learn 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