2

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;

      ...
2
  • 1
    and is $dbModuleNo ['ModuleName'] really defined? Commented Nov 23, 2012 at 17:09
  • Im trying to link $dbModuleNo to ['ModuleNo'] which is name of field in db Commented Nov 23, 2012 at 17:46

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.