I have 3 tables in mySQL database.
questions_table with these fields:
questions_users_relationship_table with these fields:
users_table with these fields:
So, erm.. The reason why have these 3 tables in the database is because I have to build an admin section on the website which allows the administrators to switch and change the position of the questions in a form. Therefore the reason for the question_positionID field in the questions_table. I have no problem in listing down the questions and its respective answers in the admin section for the administrators to do the position change ( i used ajax drag n drop to update the mysql table on the question_positionID field)..
My problem lies on the frontend html form itself. Because In the database, I only stored the questions and also its answers in its respective tables.. and did not store what type of questions it is (for example: a gender question might use radio buttons for its answers (Male/Female) or maybe address field which has the Country field with answers in the form of a drop down box).. So, in the frontend html form, I am not sure How to list down the questions and its answers for the respective user_id sorted based on the question_positionID and still would properly display the correct form elements (example: questions with dropdown with list the value in the dropdown boxes, questions with answers in radio buttons would be properly checked in the respective radio buttons, and etc etc)..
I believe many people would love to know how to do it.. I not an expert in PHP and mySQL and slowly learning it through examples and online tutorials.. So I would appreciate it if you guys could shed some light on this question of mine..
You should add another table with answers:
Answer
If there are multiple correct answers you will have to add a "is_correct" column to the table as well.
A simple approach to decide how to display the possible answers in the frontend to add a column allow_multiple_answers to your questions_table and decide whether to use dropdown or radio buttons depending on the number of possible answers.
$question//the question
$answers //an array with the possible answers
$maxAnswersForRadio//maximum numbers of oprions to be displayed with radio buttons
if($question['allow_multiple_answers'){
//use checkboxes to allow mulltiple answers.
$html = generateCheckboxes($answers);
}
else{
if (count($answers<=$maxAnswersForRadio){
$html= generateRadioButtons($answers);
}
else{
$html=generateDropDownList($answers);
}
}
This is just a example. The sollution depends on the framework/template engine you use.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.