SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Working with Arrays

    I have a table of questions for a survey, each one with its identifying number. I have a form that populates with all the questions (this is not an exact number, as more can be added) but I need to store the results into one field, using arrays, because the number of columns for a table holding the results would need to change otherwise whenever a new question is added). I have never worked with arrays but I was told this was the way to do it. I need to store the yes or no answer for a question with its number identifying the question. I also need to know how do I pull it back when I need to display the results of a particular survey, as I would need to link the number of the question to the questions table to display the actual question, and then get the answer and display it too. I have no idea where to start.
    My questions table(TA_Questions) has three fields: CategoryID, QuestionID, Question and the table that holds the results(TA_Surveys) has 3 fields: EventId, UserId, Answers(this is where I need to store all the answers to the survey which can be yes,no, or blank). So far there are 29 questions in the survey. In order to display the results I need to access the questions table to get the question using the QuestionID which ideally was stored with its answer in the answers field. Can anybody help me with this code?

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Why not have a UserAnswers table?

    UserID | QuestionID | Answer

    No primary keys, and you can have multiple rows with the same UserID - i.e. with 29 questions you would have up to 29 rows for each UserID. If you add another question to the survey then when that User answers that question you simply add their answer to this table.

    So you could have these 3 tables ...

    Users (UserID | UserName | Whatever other fields you like) [To define the user]

    Questions (QuestionID | Question) [To define your survey questions]

    UserAnswers (UserID | QuestionID | Answer) [To record the responses to the questions]

    You could also have an Answers table (if there are specific answer options for each question) ...

    QuestionID | ValidAnswer
    1 | Yes
    1 | No
    1 |

    [Last one is an empty filed, meaning Blank]

    So you probably don't need to use arrays at all.
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think this approach would be too efficient as I will have about 2k submissions each day, all questions are required so server side validation is difficult with this approach. Also, there are more components to the survey that are not basic yes or no answers, so these need to be in the same row as the rest of the entries for the specific event and user.
    I really need to be able to take all the answers from the yes/no questions and store them into one field, and I need to be able to retrieve them and present them with their corresponding description. The yes/no questions are just one component of the whole survey.

  4. #4
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is an example of the portion that has these questions:I am using radio button groups for each yes/no for each question and the id of the question is a hidden field.

    Yes No Accident Kit
    Yes No Company Issued Fuel Card
    Yes No Valid Drivers License
    Yes No Drug Testing Kit (if applicable)
    Yes No Personal Protection Equipment Bag

  5. #5
    SitePoint Zealot gidday's Avatar
    Join Date
    Jan 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi pinkiguana

    First up changing your input and output requirements isn't helping anyone but has actually made no real difference to what you trying to achieve. siteguru has supplied you with probably the best way of accomplishing what you want. I think you are confusing what you want to do with what is possible and good coding practice.

    You need to research a bit more about database design. When I design a database I keep 1 to 1 information in a single table and this is the type of data you are storing (from your descriptions and examples). siteguru has given you the start for this and it is a way forward. Storing Yes/No/Other in one field is easy if you set up your table data types correctly. Based on what you have written so far database design is an important design step for you to understand.

    As to server side validation, you should consider some client side validation followed up with robust server side checking.

    So is an array the best way to go? It's one way. I'd use a database.

    Feel free to ask lots of questions but we aren't going to write your code for you.
    Joe
    "All that is necessary for the triumph of evil
    is that good men do nothing."

  6. #6
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    gidday:
    I am of course using a database, and the table that holds the event survey information for the users(who exist in a separate normalized table)has columns for all the text entry fields in the form, as well as one column for the answers of that piece of the form (which is actually an update on the original record that gets inserted in the previous step of the survey, with different types of input...
    I am not sure if I understand what you mean by changing my input and output requirements...they have not changed? and I am certainly not asking for someone to write the code for me, but rather for an example of how I can accomplish what I am trying to accomplish here, which is saving the portion of the form with the yes/no replies into one field and then being able to retrieve them and display the question itself (rather than the stored Id) with the stored answer. I have never worked with arrays and I am not a seasoned coder, but rather focus on db administration and design. And yes, siteguru's suggestion is a way of doing this, but due to the nature of the application itself it would not be the BEST way. The whole application is already using a normalized relational db structure, as it is much more complex than the question I have posted. Anyway, I am hoping someone can share an example so I can apply it to what I need,but thank you anyway for your input.

  7. #7
    SitePoint Guru
    Join Date
    Jun 2007
    Posts
    689
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are a couple of choices for you to do it your way.

    1] give the form fields all the same name. When they are retrieved in the processing page they will automatically be concatenated with comma delimiter .

    or

    2] retrieve each value from the form in the processing page and programmatically concatenate them into a single string using a delimiter of your choice.

    The delimited string can then be stored into the single field of a database.

    When retrieving the data use the split function of vbscript to convert the delimited string into an array.

    Once you have the array then you can work with it as desired.

    =============================

    The details of making one of these work would require knowing more about the code/database/output requirements.


Tags for this Thread

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
  •