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?
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
Creat to tables in DB, eg.
primary_questionsandsub_questionsCreate 1:M relationship with the tables;
primary_questionsHAS MANYsub_questions. Maintain theprimary_que_idas foreign keyIn your CMS while adding questions, add an action/button to add a subquery to the primary question.
On submit, check if POST has subquery added. If Yes, insert the main question in
primary_questionstable and then in response to the same insert, Insert intosub_questionstable. You will get foreign keyprimary_que_idby insert query of primary question.Footnote: To get the last inserted ID Exaple Queries
Hope this helps!