Note that there are some explanatory texts on larger screens.

plurals
  1. POPopulating Multiple Combo Boxes with PHP from MySQL database
    text
    copied!<p>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.</p> <pre><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) &amp;&amp; $row2=mysql_fetch_array($result_code_query)) for($i=1;$i&lt;$column_count;$i++) { $db_q="q$i"; $fill=$row[$db_q]; $fill_code=$row2[$db_q]; print('&lt;tr style="background:navy;color:white"&gt;&lt;td width="60px"&gt;Question '.$i.'&lt;/td&gt; &lt;td&gt; &lt;select name="'.$db_q.'_code" id="'.$db_q.'_code" onchange="showQuestion(this)"&gt; &lt;option value=""&gt;Select a question&lt;/option&gt; for ($j=1;$j&lt;$column_count;$j++) { $name=mysql_field_name($selected_query,$j); if ($fill_code==$name) { $selected="selected"; } else { $selected=""; } print('&lt;option value="'.$name.'" '.$selected.'&gt;'.$name.'&lt;/option&gt;'); } print('&lt;/select&gt; &lt;/td&gt; &lt;td&gt;&lt;input type="text" value="'.$fill.'" name="'.$db_q.'" id="'.$db_q.'"&gt;&lt;/td&gt;&lt;/tr&gt;'); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload