SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Non-Member
    Join Date
    Oct 2005
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    arrays of checkboxes

    Thanks for your help!

    Here's my deal:

    I have two related tables in my database. One stores events and the other stores a list of key topics.

    I have an interface set up where users can add / edit events.

    In the add event page, I am querying the "key topics" table and returning them as options (array of checkboxes). The user makes their pick, and submits. All the data saves in the 'event" table, including the primary id of the "topics".

    That part works great. The problem I am having is when the EDIT event page resolves, I am trying to "check" the options that were previously saved.

    First thing I do (at the top of the page) is to query the events table to pull all of the data out. I use the 'events" table to do this.

    For the checkboxes, I am once again looping through them in a query that uses the "key topics" table. So the boxes are displayed, but I can't seem to get the logic right to have the appropriate ones checked. here is my current code:

    Code:
    SQL = "SELECT * FROM tbl_event_topic"
    
    'Create a Recordset object, populate it with the query results
    
    set rs = cnTeachingTechniciansApps.Execute(sql)
    
    Do While Not rs.EOF
    
    var_topic_id = rs.Fields("id")
    var_topic_name = rs.Fields("topic_name")
    
    
    IF var_topic_id = var_topic1 OR var_topic_id = var_topic2 OR var_topic_id = var_topic3 Then
    
    %>
    <div id="checkbox"><input type="checkbox" class="" name="var_key_topics" value="<%=var_topic_id%>" checked><%Response.Write(var_topic_name)%></div>
    <%
    Else
    %>
    <div id="checkbox"><input type="checkbox" class="" name="var_key_topics" value="<%=var_topic_id%>" ><%Response.Write(var_topic_name)%></div>
    <%
    End If
    
    rs.MoveNext 
    Loop
    The page renders with nothing checked. Apparently , my condition never tests true. Can anyone help?

    By the way, the user is limited to 3 topics.

  2. #2
    SitePoint Guru SSJ's Avatar
    Join Date
    Jan 2007
    Posts
    828
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use Trim Function while comparing the variables

  3. #3
    Non-Member
    Join Date
    Oct 2005
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help, but I am not sure why I would want to use the trim function.

    The values of what I am comparing here are single digit numbers......

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe try wrapping all your variables in CLng() to make sure they are converted to (long) integers.

  5. #5
    Non-Member
    Join Date
    Oct 2005
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    It works

    Okay,

    I changed my comparison to this:

    IF Trim(var_topic_id) = Trim(var_topic1) OR Trim(var_topic_id) = Trim(var_topic2) OR Trim(var_topic_id) = Trim(var_topic3) Then

    And it works great, but I am not sure why?

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe in your database the field type is char with a fixed width, which I believe appends spaces to the value.

  7. #7
    SitePoint Guru SSJ's Avatar
    Join Date
    Jan 2007
    Posts
    828
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ya you are right jimfraser. If your field type is other than int then it will automatically append spaces so you have to use Trim & after all it is a good programming habit.

    Happy Programming.!!
    -SSJ


Bookmarks

Posting Permissions

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