Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The reason why this doesn't work is that your SQL queries are in the wrong order. You first want to get the <code>courses_by_academy</code> and then get the respective course from the <code>courses_selection_list</code>.</p> <p>Also your approach to using Javascript in this context is a bit unreasonable, so I have removed it for displaying the dropdowns.</p> <pre><code>&lt;?php $id = 15; //get the course list $db_select = $db_con-&gt;prepare(" SELECT academy_id, course_name, course_id, course_start_date, course_end_date FROM courses_by_academy WHERE academy_id = :id "); if (!$db_select) return false; if (!$db_select-&gt;execute(array(':id' =&gt; $id))) return false; $results = $db_select-&gt;fetchAll(\PDO::FETCH_ASSOC); if (empty($results)) return false; foreach ($results as $value) { $course_query = $db_con-&gt;prepare("SELECT course_id, course_name FROM courses_selection_list;"); $course_query-&gt;execute(); $data = $course_query-&gt;fetchAll(); $i = 0; foreach ($data as $row){ $i += 1; echo '&lt;div class="courses"&gt;'; echo '&lt;label&gt;Course # &lt;span&gt;', $i, '&lt;/span&gt;&lt;/label&gt;&lt;br /&gt;'; echo '&lt;label&gt;Course Name:&lt;/label&gt;'; echo '&lt;select name="coursename[]" class="course_list"&gt;'; echo '&lt;option&gt;--- Select ---&lt;/option&gt;"'; echo "&lt;option value='".$row["course_id"].":".$row["course_name"]."'"; if ($value['course_id'] == $row['course_id']) { echo " selected='selected'"; } echo "&gt;". $row['course_name'] ."&lt;/option&gt;"; } echo '&lt;/select&gt;&lt;/div&gt;&lt;/br&gt;'; </code></pre> <p>Note how I used <code>coursename[]</code> as the name for the SELECT: using this the PHP will receive this as an array in the POST request, as <code>$_POST["coursename"]</code>, so that you can use <code>foreach</code> to go through them.</p> <p>I assume the Javascript is meant to be able to change the number of courses? I would take a different approach here:</p> <pre><code>$("#courses_offered").on("change", function() { var number_of_courses_offered = $('#courses_offered').val(); var course_divs = $("div.courses"); if (number_of_courses_offered &lt; 1) { course_divs.hide(); return; } course_divs.show(); // to few courses displayed: add another one until the number fits while (course_divs.length &lt; number_of_courses_offered) { // create an empty course div by using the first one and removing the // selected attribute so that the first item "--- Select" is displayed var single_course_div = course_divs.first().clone(true); single_course_div.find("option:selected").removeAttr("selected"); // fix the course number single_course_div.find("span").text(course_divs.length + 1); // insert it after the last of the divs course_divs.last().after(single_course_div); course_divs = $("div.courses"); // update for the next while loop } // too many courses displayed: remove the last ones while (course_divs.length &gt; number_of_courses_offered) { // remove the last div course_divs.last().remove(); course_divs = $("div.courses"); // update for the next while loop } }); </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