0

I am making an application where I have to ask questions and depending on answers given, I have to ask other questions until the end of the process.

Example: Next question depends on A1

Scenario 1:

Q1: Are you married ? Y / N

A1: Y

Q2: Enter the date of marriage? ...

Scenario 2:

A1: N

Q2: Do you intend to get married? Y/N

A2: Y

Q3: In which country you like to get married? 1.USA 2.Canada 3.Others ...

End scenarios

Questions can be simple Yes/No, input value, Selection from multiple choices

How do I save these in a database together with the flow control of questions and answers?

Anand G
  • 3,130
  • 1
  • 22
  • 28
aliirfaan
  • 156
  • 5
  • Do you have the CMS do add question and answer in DB or these are fixed static questions? – Anand G Feb 12 '18 at 09:04
  • See [this](https://stackoverflow.com/questions/16727608/for-an-online-questionnaire-how-to-design-a-database-for-keeping-track-of-all-u) and [this](https://stackoverflow.com/questions/4270776/can-you-recommend-a-database-design-for-quiz-questions-and-answers-that-would-al) – Jomoos Feb 12 '18 at 09:05
  • Questions and answers will be saved in the database. – aliirfaan Feb 12 '18 at 09:12

1 Answers1

0

Assuming you have a CMS backend to add questions and answers, the approach I would suggest as per my experience of working on such functionality is

  1. Creat to tables in DB, eg. primary_questions and sub_questions

  2. Create 1:M relationship with the tables; primary_questions HAS MANY sub_questions. Maintain the primary_que_id as foreign key

  3. In your CMS while adding questions, add an action/button to add a subquery to the primary question.

  4. On submit, check if POST has subquery added. If Yes, insert the main question in primary_questions table and then in response to the same insert, Insert into sub_questions table. You will get foreign key primary_que_id by insert query of primary question.

Footnote: To get the last inserted ID Exaple Queries

Hope this helps!

Anand G
  • 3,130
  • 1
  • 22
  • 28