Results 1 to 6 of 6

Thread: multiple answers in one database field

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    Thanks
    0
    Thanked
    0 times in 0 posts

    multiple answers in one database field

    hi there.

    i have a competition whereby users are asked to select a maximum of 5 correct things out of a choice of 12. those things need to be displayed in a database where i can see if the things that were selected are right or wrong. i have a value for the checkbox 'ckb' which i needed for the javascript to work (select 5, more than 5 will bring up an alert) but i need to display all 5 results. how to i do this?

    i have gotten the javascript to work so people could only select a maximum of 5. that was based on all the check boxes having the same id. now i need to display the 5 results in the database

    this is the script that only allows the user to select up to 5 checkbox values
    Code:
    <script type="text/javascript">
    function chkcontrol(j) {
    var total=0;
    for(var i=0; i < document.form1.ckb.length; i++){
    if(document.form1.ckb[i].checked){
    total =total +1;}
    if(total > 5){
    alert("Please Select only five")
    document.form1.ckb[j].checked = false ;
    return false;
    }
    }
    } </script>
    this is the section of code that deals with the checkboxes. they have the same name but the values are different.

    Code:
    <table border='0' width='471' cellspacing='0' cellpadding='1' align=center>
    <tr bgcolor="#ffffcc"><td width="59"> </td><td width="290" ><b>Choice</b></td></tr>
    <tr bgcolor="#f1f1f1"><td align="center"><input type=checkbox name=ckb value="Sweet shop" onclick="chkcontrol(0)";></td><td >In the Sweetshop</td></tr>
    <tr bgcolor="#ffffff"><td align="center"><input type=checkbox name=ckb value="Playing Chess" onclick="chkcontrol(1)";></td><td >Playing Chess </td></tr>
    <tr bgcolor="#f1f1f1"><td align="center"><input type=checkbox name=ckb value="Sweet Peas Dolls" onclick="chkcontrol(2)";></td><td >With the Sweet Peas Dolls</td></tr>
    <tr bgcolor="#ffffff"><td align="center"><input type=checkbox name=ckb value="Hiding between the Hamleys Bears" onclick="chkcontrol(3)" ;></td><td >Hiding between the Hamleys Bears</td></tr>
    <tr bgcolor="#f1f1f1"><td align="center"><input type=checkbox name=ckb value="Racing cars on the Scalextric track" onclick="chkcontrol(4)";></td><td >Racing cars on the Scalextric track</td></tr>
    <tr bgcolor="#ffffff"><td align="center"><input type=checkbox name=ckb value="Having lunch in the Regal Tea Cafe" onclick="chkcontrol(5)";></td><td >Having lunch in the Regal Tea Cafe</td></tr>
    <tr bgcolor="#f1f1f1"><td align="center"><input type=checkbox name=ckb value="Trying on a Hamleys Medieval Dress Costume" onclick="chkcontrol(6)";></td><td >Trying on a Hamleys Medieval Dress Costume</td></tr>
    <tr bgcolor="#ffffff"><td align="center"><input type=checkbox name=ckb value="Flying a remote controlled plane" onclick="chkcontrol(7)";></td><td >Flying a remote controlled plane </td></tr>
    <tr bgcolor="#ffffff"><td align="center"><input type=checkbox name=ckb value="Playing Nerf Tag with Alice" onclick="chkcontrol(8)";></td><td> Playing Nerf Tag with Alice</td></tr>
    <tr bgcolor="#ffffff"><td align="center" ><input type=checkbox name=ckb value="Playing with an Infinite Bubble Gun" onclick="chkcontrol(9)";></td><td >Playing with an Infinite Bubble Gun</td></tr>
    <tr bgcolor="#ffffff"><td align="center" ><input type=checkbox name=ckb value="Peeking out behind the Lego Clone Trooper" onclick="chkcontrol(10)";></td><td >Peeking out behind the Lego Clone Trooper</td>
    </tr>
    </table>
    now in the database i want to store all 5 results. therefore each row will have the intro info (name, address, email) but it will then have the values of the 5 checkbox values. how can i achieve this?

    any helpers on this please?
    Last edited by dmcHamleys; 05-03-2009 at 12:32 PM.

  2. #2
    One Great Ordeal!
    Join Date
    Feb 2005
    Posts
    1,764
    Thanks
    201
    Thanked
    56 times in 49 posts

    Re: multiple answers in one database field

    A 2 dimensional array is I think is a potential solution. Using a for loop, you'd store in the relevant part within the array a particular piece of data you have gathered from the end user?

    Pseudo code:
    To gather data:
    for i = 0, i < 5, i++
    array[i][0] = checkbox1
    array[i][1] = checkbox2
    array[i][2] = checkbox3
    array[i][3] = name
    end for

    To insert into the database
    for y = 0, y < 5, y++
    sql = "insert into table_name values (array[y][0],array[y][1],array[y][2])"
    end for

    Not sure that is any help...
    Last edited by digit; 05-03-2009 at 03:23 PM.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    Thanks
    0
    Thanked
    0 times in 0 posts

    Re: multiple answers in one database field

    hi there

    the checkboxes relates to a competition that has 12 answers. the value of the checkbox is the worded answer rather than a number. hence
    Code:
    <td align="center"><input type=checkbox name=ckb value="Sweet shop" onclick="chkcontrol(0)";></td><td >In the Sweetshop</td>
    the onclick relates to a javascript that counts how many boxes have been clicked. once over 5 an alert box comes up.

    now what i am stuck on is how to get the 5 answers into the database. atm the checkbox name is 'ckb'. is this ok or does it need changing? each person will have 5 answers therefore the database needs to have 5 columns (1 for each answer) but each answer is down to the individual.

    how do i code this so the database reflects this?

  4. #4
    Senior Member
    Join Date
    Jul 2004
    Location
    London
    Posts
    2,456
    Thanks
    100
    Thanked
    75 times in 51 posts
    • Mblaster's system
      • Motherboard:
      • ASUS PK5 Premium
      • CPU:
      • Intel i5 2500K
      • Memory:
      • 8gb DDR3
      • Storage:
      • Intel X25 SSD + WD 2TB HDD
      • Graphics card(s):
      • Nvidia GeForce GTX 570
      • PSU:
      • Corsair HX520
      • Case:
      • Antec P180
      • Operating System:
      • Windows 7 Professional x64
      • Monitor(s):
      • HP w2207 (22" wide)
      • Internet:
      • Rubbish ADSL

    Re: multiple answers in one database field

    Are you looking for the database structure that will allow you to store this? Or the method to save data to a database? Or both?

    Depending on what you want to do, the easiest way to store them in the database would be to merge them into a single string, separated by ';' or something, then you can just save to a single database field like any other entry. Although this approach offers less flexibility, it may suit your needs.

    Could also just have field names such as 'name', 'address', 'email', 'choice1', choice2', 'choiceN', etc if you'd like to keep them separated in the database. Basically, it depends on what you want to do with the data after it's been saved.

    Digits suggestion of a 2d array would be a good way to store all the questionnaire data within the Javascript/PHP/etc code as a single object, but not sure it's going to get you any closer to what you want.
    I don't mean to sound cold, or cruel, or vicious, but I am so that's the way it comes out.

  5. #5
    bored out of my tiny mind malfunction's Avatar
    Join Date
    Jul 2003
    Location
    Lurking
    Posts
    3,923
    Thanks
    191
    Thanked
    187 times in 163 posts
    • malfunction's system
      • Motherboard:
      • Gigabyte G1.Sniper (with daft heatsinks and annoying Killer NIC)
      • CPU:
      • Xeon X5670 (6 core LGA 1366) @ 4.4GHz
      • Memory:
      • 48GB DDR3 1600 (6 * 8GB)
      • Storage:
      • 1TB 840 Evo + 1TB 850 Evo
      • Graphics card(s):
      • 290X
      • PSU:
      • Antec True Power New 750W
      • Case:
      • Cooltek W2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Dell U2715H

    Re: multiple answers in one database field

    In my opinion the 'proper' way of doing this is to use multiple tables; one to hold the user information (name, address, e-mail address, etc) and another to record the options that each user selected - with each option (each answer) being a separate record. You could also use a a reference table for each option so that you could store less data per choice, e.g.

    user table:

    user_id
    user_name
    user_address
    user_email

    (obviously you may need or prefer to split both the name and address into multiple fields)

    user_option table:

    user_id
    option_id

    option table:

    option_id
    option_description

    This way the value of each checkbox would become the option_id - an abstract number - e.g. "1" to "12" which would be marginally less data to pass back to the server and store in the database. This also works better if you ever need to run a different competition - i.e. you could use the same database with new options (such as "13" to "24") without worrying about the text value of each option being unique. If you were going to re-use the DB for other competitions I'd suggest you have an additional "competition" table - that describes the competiton itself - e.g.

    comp_id
    comp_description
    comp_startdate
    comp_enddate
    comp_winners (so you can say if there's 1 winner or > 1)

    though in this case you'd probably also want to tie each option to the specific competition and maybe create a separate 'entry' table to log the user id, comp id and the time and date at which the entry was made (to ensure it fell within the start and end date and just for general auditing purposes - you may even want to log the IP address from which the entry was made too so you can see if someone's making a million entries from the same 'PC' - talking of which do you want to allow multiple entries per person or limit it to one per e-mail address?)
    Last edited by malfunction; 06-03-2009 at 06:40 PM.

  6. #6
    Senior Member
    Join Date
    Dec 2004
    Posts
    360
    Thanks
    27
    Thanked
    2 times in 2 posts
    • piggeh's system
      • Motherboard:
      • ASUS AiLife IP35
      • CPU:
      • Intel Q6600 G0
      • Memory:
      • OCZ 2x2GB DDR2 (5-4-4-18)
      • Storage:
      • 500GB Spinpoint
      • Graphics card(s):
      • Inno3D 8800GT
      • PSU:
      • Corsair HX520W
      • Case:
      • Antec 900 Case
      • Operating System:
      • Vista 64
      • Monitor(s):
      • Samsung 24" SM2494HS
      • Internet:
      • Virgin Cable

    Re: multiple answers in one database field

    Bit of a php noob myself, but isn't the easiest way to serialize an array and store this into the database?

    Unsure you should be using javascript as a method to control user input though, it should be done server side. What happens if users have javascript disabled?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. having multiple IP addresses?
    By latrosicarius in forum Networking and Broadband
    Replies: 7
    Last Post: 19-11-2008, 06:05 AM
  2. Database Visualisation Tool ? Developing in SQL
    By Sinizter in forum Software
    Replies: 5
    Last Post: 29-04-2007, 12:20 PM
  3. V3 Cardbox database app promises power and accessibility
    By Bob Crabtree in forum HEXUS News
    Replies: 3
    Last Post: 05-12-2005, 07:10 PM
  4. Multiple action listeners in one class
    By Kezzer in forum Software
    Replies: 11
    Last Post: 22-04-2005, 05:20 PM
  5. hexus database
    By ingouk in forum HEXUS Suggestions
    Replies: 4
    Last Post: 08-09-2003, 09:46 PM

Posting Permissions

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