简体   繁体   中英

Displaying questions & answers from mysql database with php into a html form in the frontend

I have 3 tables in mySQL database.

questions_table with these fields:

  1. question_id (unique ID for the question)
  2. question_name (the name of the question)
  3. question_positionID (the position of the question, I need this because the admins are able to change the positions of the questions in the html form in the frontend. I use this to display the questions in the form appropriately)

questions_users_relationship_table with these fields:

  1. questions_answers (this is the field where it stores what the users answered in the frontend form)
  2. user_id (this is used to connect to the users table, when loading the specific user's answers to the form)
  3. question_id (this is used to reference the question in the questions_table)

users_table with these fields:

  1. user_id (unique id)
  2. username (as the name implies, username)
  3. password (as the name implies, password)

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

  • answer_id
  • question_id
  • answer_text

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM