Note that there are some explanatory texts on larger screens.

plurals
  1. POwhile loop is not working when I add an extra field inside the loop
    text
    copied!<p>I have this code below:</p> <pre><code> while ( $qrystmt-&gt;fetch() ) { // data array $dataArray[$dbCourseId]['CourseName'] = $dbCourseName; $dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleName'] = $dbModuleName; // session data $_SESSION['idcourse'] = $dbCourseNo; $_SESSION['namecourse'] = $dbCourseName; } </code></pre> <p>Now the above code works fine when I am trying t display 2 fields in a while loop. But when I try to add a third field in the while loop, then it doesn't seem to work and it give me an error stating undefined <code>ModuleName</code>.</p> <p>Is the it the way I have laid it out that is causing the error in the while loop below:</p> <pre><code>while ( $qrystmt-&gt;fetch() ) { // data array $dataArray[$dbCourseId]['CourseName'] = $dbCourseName; $dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleNo'] = $dbModuleNo ['ModuleName'] = $dbModuleName; // session data $_SESSION['idcourse'] = $dbCourseNo; $_SESSION['namecourse'] = $dbCourseName; } </code></pre> <p>The full code is below where it selects a course from the database and then it selects the modules which corresponds with the course chosen:</p> <pre><code> $sql = "SELECT CourseId, CourseNo, CourseName FROM Course"; $sqlstmt=$mysqli-&gt;prepare($sql); $sqlstmt-&gt;execute(); $sqlstmt-&gt;bind_result($dbCourseId, $dbCourseNo, $dbCourseName); $courses = array(); // easier if you don't use generic names for data $courseHTML = ""; $courseHTML .= '&lt;select name="courses" id="coursesDrop"&gt;'.PHP_EOL; $courseHTML .= '&lt;option value=""&gt;Please Select&lt;/option&gt;'.PHP_EOL; while($sqlstmt-&gt;fetch()) { $courseid = $dbCourseId; $course = $dbCourseNo; $coursename = $dbCourseName; $courseHTML .= "&lt;option value='".$courseid."'&gt;" . $course . " - " . $coursename . "&lt;/option&gt;".PHP_EOL; } $courseHTML .= '&lt;/select&gt;'; ?&gt; &lt;?php if (isset($_POST['submit'])) { $submittedCourseId = $_POST['courses']; $query = " SELECT cm.CourseId, cm.ModuleId, c.CourseNo, m.ModuleNo, c.CourseName, m.ModuleName FROM Course c INNER JOIN Course_Module cm ON c.CourseId = cm.CourseId JOIN Module m ON cm.ModuleId = m.ModuleId WHERE (c.CourseId = ?) ORDER BY c.CourseName, m.ModuleId "; $qrystmt=$mysqli-&gt;prepare($query); // You only need to call bind_param once $qrystmt-&gt;bind_param("s",$submittedCourseId); // get result and assign variables (prefix with db) $qrystmt-&gt;execute(); $qrystmt-&gt;bind_result($dbCourseId,$dbModuleId,$dbCourseNo,$dbModuleNo, $dbCourseName,$dbModuleName); $qrystmt-&gt;store_result(); $num = $qrystmt-&gt;num_rows(); if($num ==0){ echo "&lt;p style='color: red'&gt;Please Select a Course&lt;/p&gt;"; } else { $dataArray = array(); while ( $qrystmt-&gt;fetch() ) { // data array $dataArray[$dbCourseId]['CourseName'] = $dbCourseName; $dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleName'] = $dbModuleName; // session data $_SESSION['idcourse'] = $dbCourseNo; $_SESSION['namecourse'] = $dbCourseName; } foreach ($dataArray as $foundCourse =&gt; $courseData) { $output = ""; $output .= "&lt;p&gt;&lt;strong&gt;Course:&lt;/strong&gt; " . $foundCourse . " - " . $courseData['CourseName'] . "&lt;/p&gt;"; $moduleHTML = ""; $moduleHTML .= '&lt;select name="module" id="modulesDrop"&gt;'.PHP_EOL; $moduleHTML .= '&lt;option value=""&gt;Please Select&lt;/option&gt;'.PHP_EOL; foreach ($courseData['Modules'] as $moduleId =&gt; $moduleData) { $moduleHTML .= "&lt;option value='$moduleId'&gt;" . $moduleId . " - " . $moduleData['ModuleName'] ."&lt;/option&gt;".PHP_EOL; } } $moduleHTML .= '&lt;/select&gt;'; echo $output; ... </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