How to validate an array element?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmorand
    New Member
    • Sep 2007
    • 219

    How to validate an array element?

    I'm trying to validate whether or not an array element has been created.

    Here is what one of the arrays could look like:

    commentid[1] = undefined
    commentid[2] = undefined
    commentid[3] = 1
    commentid[4] = 1
    commentid[5] = undefined
    commentid[6] = 1

    I want to check if a certain array element is set to 1, but I'm getting errors in the code below because commentID[1] is undefined. How can I have the code not evaluate if it's undefined. I've tried using isDefined() and isValid() as well.

    [code=cfm]
    <cfoutput query="getComme nts">
    <cfif #commentList[commentID]# eq 1>
    Valid!!!!
    <cfelse>
    not valid!!!
    </cfif>
    </cfoutput>
    [/code]
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    isDefined() doesn't work with array elements. Set a variable to the array element and then test that instead.

    Comment

    • dmorand
      New Member
      • Sep 2007
      • 219

      #3
      I'm not able to set a variable to the array element if it's undefined. I tried it and I'm still getting CF errors. I think I'm just not thinking of the best way to handle this, but I'm stumped.

      This is what I'm trying to accomplish:
      I have a form with a section with a bunch of checkbox fields.
      I'm retrieving the fields which were selected using a query. I know which numbers were checked, but now I want to have those fields checked off when the user goes into the form to edit their records.

      I just need to either see if the query returned that specific value or not, and either check or not check the box.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Then why not just use a list and loop over it checking the checkboxes which have a number in the list? Create the list (a string of numbers delimited by commas) using the query. To determine whether to check a checkbox or not, you could use listfind.

        Comment

        • dmorand
          New Member
          • Sep 2007
          • 219

          #5
          Originally posted by acoder
          Then why not just use a list and loop over it checking the checkboxes which have a number in the list? Create the list (a string of numbers delimited by commas) using the query. To determine whether to check a checkbox or not, you could use listfind.
          I'll give that a shot. It's so easy in my head, I just cant get the sql/cf code down right.

          I'm too used to coding in proprietary systems....I need to get better acquainted with sql/cf.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            If you get stuck, post your attempted code, and I can suggest changes.

            Comment

            Working...