Reputation: 62
I have a maximum of 100 selections (now 79) for the combo box, and have 79 of them. Each one stands for a question and is recorded to "questions" table in MySQL. The options are recorded in a table called "question_codes" and they stand for the field names associated with some questions. I tried 2 for loops within each other but I think that will overload the server because of 79*79 iterations. I also have a text field depending upon the selection with ajax, working properly. Number 0 field in the table "question_codes" is id and I don't use that. Here is my code.
$code_query="SELECT * FROM questions WHERE id='1'";
$question_query="SELECT * FROM questions WHERE id='2'";
$result_question_query=mysql_query($question_query,$conn);
$result_code_query=mysql_query($code_query,$conn);
$selected_query=mysql_query("SELECT * FROM question_codes");
while($row_selected=mysql_fetch_row($selected_query))
{
$column_count=count($row_selected);
}
while($row=mysql_fetch_array($result_question_query) && $row2=mysql_fetch_array($result_code_query))
for($i=1;$i<$column_count;$i++)
{
$db_q="q$i";
$fill=$row[$db_q];
$fill_code=$row2[$db_q];
print('<tr style="background:navy;color:white"><td width="60px">Question '.$i.'</td>
<td>
<select name="'.$db_q.'_code" id="'.$db_q.'_code" onchange="showQuestion(this)">
<option value="">Select a question</option>
for ($j=1;$j<$column_count;$j++)
{
$name=mysql_field_name($selected_query,$j);
if ($fill_code==$name)
{
$selected="selected";
}
else
{
$selected="";
}
print('<option value="'.$name.'" '.$selected.'>'.$name.'</option>');
}
print('</select>
</td>
<td><input type="text" value="'.$fill.'" name="'.$db_q.'" id="'.$db_q.'"></td></tr>');
Upvotes: 0
Views: 2096
Reputation: 5263
There's probably a more elegant way to structure the database and PHP.
Perhaps something like this for SQL:
CREATE TABLE `questions` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`question` varchar(255) NOT NULL,
`answer` int(11) unsigned NOT NULL DEFAULT '0'
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `question_options` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`question_id` int(11) unsigned NOT NULL DEFAULT '0',
`option` varchar(64) NOT NULL
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
... and this for the PHP ...
$q_sql = mysql_query("SELECT * FROM `questions`");
while ($q = mysql_fetch_row($q_sql)) {
echo '
<tr><td>Question '. $q['id'] .'</td>
<td><select name="q'. $q['id'] .'">';
$qo_sql = mysql_query("SELECT * FROM `question_options` WHERE `question_id` = '". $q['id'] ."'");
while ($qo = mysql_fetch_row($qo_sql)) {
$selected = ($q['answer'] == $qo['id']) ? ' selected' : '';
echo '
<option value="'. $qo['id'] .'"'. $selected .'>'. $qo['option'] .'</option>';
}
echo '
</select>
</td></tr>';
}
To tighten this further (this method can generate too many queries) ... make a single query instead, something like:
SELECT q.`question`, qo.*
FROM `question_options` as `qo`
LEFT JOIN `questions` as q
ON (q.`id` = qo.`question_id`)
ORDER BY qo.`question_id`
... then with a single while
loop, check for a change in the question.
Upvotes: 1