简体   繁体   中英

while loop is not working when I add an extra field inside the loop

I have this code below:

 while ( $qrystmt->fetch() ) { 
          // data array
          $dataArray[$dbCourseId]['CourseName'] = $dbCourseName; 
          $dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleName'] = $dbModuleName; 
           // session data
          $_SESSION['idcourse'] = $dbCourseNo;
          $_SESSION['namecourse'] = $dbCourseName;

    }

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 ModuleName .

Is the it the way I have laid it out that is causing the error in the while loop below:

while ( $qrystmt->fetch() ) { 
          // data array
          $dataArray[$dbCourseId]['CourseName'] = $dbCourseName; 
          $dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleNo'] = $dbModuleNo ['ModuleName'] = $dbModuleName; 
           // session data
          $_SESSION['idcourse'] = $dbCourseNo;
          $_SESSION['namecourse'] = $dbCourseName;

    }

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:

 $sql = "SELECT CourseId, CourseNo, CourseName FROM Course"; 

 $sqlstmt=$mysqli->prepare($sql);

 $sqlstmt->execute(); 

 $sqlstmt->bind_result($dbCourseId, $dbCourseNo, $dbCourseName);

 $courses = array(); // easier if you don't use generic names for data 

 $courseHTML = "";  
 $courseHTML .= '<select name="courses" id="coursesDrop">'.PHP_EOL; 
 $courseHTML .= '<option value="">Please Select</option>'.PHP_EOL;  

 while($sqlstmt->fetch()) 
 {   
     $courseid = $dbCourseId;
     $course = $dbCourseNo;
     $coursename = $dbCourseName; 
     $courseHTML .= "<option value='".$courseid."'>" . $course . " - " . $coursename . "</option>".PHP_EOL;  
  } 

  $courseHTML .= '</select>'; 

    ?>

        <?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->prepare($query);
    // You only need to call bind_param once
    $qrystmt->bind_param("s",$submittedCourseId);
    // get result and assign variables (prefix with db)

    $qrystmt->execute(); 

    $qrystmt->bind_result($dbCourseId,$dbModuleId,$dbCourseNo,$dbModuleNo, $dbCourseName,$dbModuleName);

     $qrystmt->store_result();

    $num = $qrystmt->num_rows();

    if($num ==0){
        echo "<p style='color: red'>Please Select a Course</p>";
    } else { 

        $dataArray = array();

 while ( $qrystmt->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 => $courseData) {

          $output = ""; 

          $output .= "<p><strong>Course:</strong> " . $foundCourse .  " - "  . $courseData['CourseName'] . "</p>";

       $moduleHTML = ""; 
       $moduleHTML .= '<select name="module" id="modulesDrop">'.PHP_EOL;
       $moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;      
            foreach ($courseData['Modules'] as $moduleId => $moduleData) {        

            $moduleHTML .= "<option value='$moduleId'>" . $moduleId . " - " . $moduleData['ModuleName'] ."</option>".PHP_EOL;        
  } 
            }
            $moduleHTML .= '</select>';

      echo $output;

      ...

When you call

$qrystmt->bind_result($dbCourseId,$dbModuleId,$dbCourseNo,$dbModuleNo, $dbCourseName,$dbModuleName);

You're assigning a row to $dbModuleNo. So it's probably one value and not an array. This would cause issues when you call:

$dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleNo'] = $dbModuleNo ['ModuleName'] = $dbModuleName; 

$dbModuleNo (probably) isn't defined for 'ModuleName'.

Did you try testing array with a echo of the array value before the loop? maybe you're trying to asign a value to a variable that is not well defined.

It looks like you never defined $dbModuleNo to be an array. Add $dbModuleNo = array() before your loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM