Jump to content

Storing Random Generated Forms Into A Database


rmpotter

Recommended Posts

I am creating a form that pulls questions from a database and populates select boxes for each question. Each question has a number ID and 4 possible answers associated with it.What I am having trouble with is finding a way to properly store the answers so that they may be reloaded back onto this form for later use. Does anyone have any suggestions as to how I could go about this? There are far to many variables to try and store each in it's own field.

Link to comment
Share on other sites

You need to have a database table that stores the question ID and the answer they chose. It will be best if you can also associate a specific user with it, so user ID, question ID, answer. You can also add a timestamp or other information if you want to track it.

Link to comment
Share on other sites

You need to have a database table that stores the question ID and the answer they chose. It will be best if you can also associate a specific user with it, so user ID, question ID, answer. You can also add a timestamp or other information if you want to track it.
Like I was saying I don't think that is very feasible considering the amount of variables possible. I was kinda thinking about serializing a php array... I am just a little unsure if this is the correct direction
Link to comment
Share on other sites

You could serialize an array if you want. One of the systems I put together has a test engine to create any sort of multiple choice, T/F, or essay tests, and it uses one database table to store the tests, one table to store the questions for each test, one table to store the choices for each question, and one table to store the answers.

CREATE TABLE IF NOT EXISTS `test_questions` (  `id` int(11) unsigned NOT NULL auto_increment,  `cid` int(11) unsigned NOT NULL,  `question_text` text collate utf8_unicode_ci,  `disp_order` int(11) unsigned NOT NULL,  `text_entry` tinyint(1) unsigned NOT NULL,  PRIMARY KEY  (`id`),  KEY `cid` (`cid`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;CREATE TABLE IF NOT EXISTS `test_question_choices` (  `id` int(11) unsigned NOT NULL auto_increment,  `qid` int(11) unsigned NOT NULL,  `answer_text` varchar(255) collate utf8_unicode_ci NOT NULL,  `disp_order` int(11) unsigned NOT NULL,  `correct` tinyint(1) unsigned NOT NULL,  PRIMARY KEY  (`id`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;CREATE TABLE IF NOT EXISTS `test_answers` (  `uid` int(11) unsigned NOT NULL,  `cid` int(11) unsigned NOT NULL,  `qid` int(11) unsigned NOT NULL,  `session` int(10) unsigned NOT NULL,  `choice` int(11) unsigned NOT NULL,  `text_ans` text collate utf8_unicode_ci,  KEY `uid` (`uid`),  KEY `qid` (`qid`),  KEY `cid` (`cid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

The CID in those tables is the test ID (content ID). In one installation of that, the test_questions table has 151 rows (not many tests set up I guess), the the test_question_choices table has 489 rows (~3 choices per question avg.), and the test_answers table has over 31,000 records of people answering questions.

Link to comment
Share on other sites

This is approximately what I ended up doing. Basically, I took the $_POST information and serialized it. I then have the form rebuild itself on reload and refill in the already existing answers, seems to be working well. The only odd catch is that the $_POST seems to add a whitespace to the end of each string. does anyone know why?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...