trying to access array offset on value of null

A

Anonymous

Guest
Hello everyone! I need help with my code. I can't seem to find the error.

this is the line that gives the error:

$get_courses['courses_id']; ?>"><?php echo $get_courses['courses']; ?></option>

I am trying to get all the courses from tbl_courses . My tbl_courses has only 3 column, and those are course_id, courses, and number_of_enrolled_students .
 
So this is where the code begins
session_start();

$teachers_id = $surname = $first_name = $middle_name = $work_status = $courses = $courses_id = $contact_number = $email_address = $alert_msg = '';

I've put this below the includes

//select all courses
$get_all_courses_sql = "SELECT * FROM tbl_courses ORDER BY courses Asc ";
$get_all_courses_data = $con->prepare($get_all_courses_sql);
$get_all_courses_data->execute();

I am using multiSelect on this.

<div class="control-group">
<label class="control-label" for="multiSelect">Department/s:</label>
<div class="controls">
<select multiple="multiple" id="multiSelect" class="chzn-select span5" name="department[]">
<option>
<?php while ($courses = $get_all_courses_data->fetch(PDO::FETCH_ASSOC)) { ?>
<option value="<?php echo
$get_courses['courses_id']; ?>"><?php echo $get_courses['courses']; ?></option>
<?php } ?>
</select>
<p class="help-block">Start typing to activate auto complete!</p>
</div>

Please help.Thanks!
 
Code:
<?php while ($courses = $get_all_courses_data->fetch(PDO::FETCH_ASSOC)) { ?>
<option value="<?php echo
$get_courses['courses_id']; ?>"><?php echo $get_courses['courses']; ?></option>
<?php } ?>

In this code you are assigning a value to $courses in your while loop, but you're looking at $get_courses in the loop body. $get_courses is probably null, and you're trying to treat it like an array; that's what your error means. I'm guessing this is a typo and you meant to use $courses rather than $get_courses inside the loop.
 
Back
Top