I will be honest here – I am pretty sure I am not approaching this from the best method
I have a survey software I am working on that allows the owner to build questions of different input types (text, checkbox, radio…) text was very easy it searched question table and found the field ans_type which is what contains text, checkbox, radio… and output
switch ($rowq['ans_type']) {
case "text":
echo '<tr bgcolor="#DDDDDD" align="left" valign="middle"><td width="30"> </td> <td width="350">'.$rowq['question'].'</td><td width="350"><input class="tb8" type="text" name="quest[]" value="'.$rowa['answer'].'" size="40"></td></tr>';
break;
so as you can see – rowq contains my question information while rowa contains answer information.
my problem has arrisen at checkbox and radio where we may have multiple options currently my table is set up like this to allow up to 15 options for checkbox and radio
CREATE TABLE `question` (
`sid` int(10) NOT NULL,
`qid` double(10,1) NOT NULL,
`question` varchar(500) NOT NULL,
`ans_type` varchar(100) NOT NULL,
`opt_one` varchar(200) DEFAULT NULL,
`opt_two` varchar(200) DEFAULT NULL,
`opt_three` varchar(200) DEFAULT NULL,
`opt_four` varchar(200) DEFAULT NULL,
`opt_five` varchar(200) DEFAULT NULL,
`opt_six` varchar(200) DEFAULT NULL,
`opt_seven` varchar(200) DEFAULT NULL,
`opt_eight` varchar(200) DEFAULT NULL,
`opt_nine` varchar(200) DEFAULT NULL,
`opt_ten` varchar(200) DEFAULT NULL,
`opt_eleven` varchar(200) DEFAULT NULL,
`opt_twelve` varchar(200) DEFAULT NULL,
`opt_thirteen` varchar(200) DEFAULT NULL,
`opt_fourteen` varchar(200) DEFAULT NULL,
`opt_fifteen` varchar(200) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
CREATE TABLE `answers` (
`surveyid` int(10) NOT NULL,
`question` double(10,1) NOT NULL,
`answer` varchar(50000) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
so I need to determine how many options are available for any given question for these types first – I figured I could have it only add the checkbox if rowq[‘…’] != NULL on each of the opt_numbers this doesnt fix my second problem though – If this survey has been filled out already and the person is editing it I need the boxes they already selected to be checked (for checkboxes this might be all 15 it might be 1 for radio naturally it will only be one) rowa[‘answer’] contains my answers which will be separated by a “,” would it be easier to just add opt_one-fifteen to answers and have them contain an int 1 or 0 based off checked or not checked.
I honestly don’t know where to go from this any help is appreciated – including changing my database around if there is a better method.
case "radio":
break;
I have a system that looks a bit like yours. The way I go about it, is letting each question row have a field
answerTypesand a fieldanswers.answerTypesis an array that tells which answers can be given to the question.The system loops through the
answerTypesarray and fetches the appropriate answers that can be given fromanswers, e.g. ifanswerTypesis array(1,3), the answerTypes areradiobuttonandtext input.answersthen has all the “specific” settings for the differentanswerTypes, such as the max length of text inputs, the different names for checkboxes and different values for radiobuttons.A little code snippet of how I do it:
The data format is: