mercredi 21 août 2019

Database returning an empty Array() in PHP Codeigniter

I want to return and join 4 tables and produce a table for my view in a codeigniter application but it is returning Array().And to display the records I am using a foreach loop in my view but unable to display the records.

I have tried to change the query but then it does not return the desired results.

This is the Controller Function :-

   public function student_list()
    {
    // get Student list
    $this->data['student_list'] = $student_list = $this->Teachers_Model- 
    >getStudentList($_SESSION['user_id']);
    //$this->data['student_list'] =  $this->Teachers_Model- 
   >getStudents($_SESSION['user_id']);

    print_r($this->data['student_list']);
    // get courses 
    $this->data['courses'] = $courses = $this->Courses_Model- 
   >getCourses(null, $_SESSION['user_id']);

    $this->render('student_list/index');
}

This is the Model :-

    public function getStudentList($teacher_uid, $student_list_id = null){

    $this->db->select('teachers_student.*, students.student_uid, 
    courses.course_id, users.username');
    $this->db->from('teachers_student');
    $this->db->join('students', 'students.student_uid = 
    teachers_student.student_uid');
    $this->db->join('courses', 'courses.id = teachers_student.course_id');
    $this->db->join('users', 'users.id = teachers_student.student_uid');


    if(!is_null($student_list_id)):

        $where = array(
            'teachers_student.id' => $student_list_id,
            'teachers_student.status' => 1,
        );

        $this->db->where($where);
        $query = $this->db->get();
        $result = $query->row_array();

    else:

        $where = array(
            'teachers_student.teacher_uid' => $teacher_uid,
            'teachers_student.status' => 1,
        );

        $this->db->where($where);
        $query = $this->db->get();
        $result = $query->result_array();

    endif;

    return $result;
}

And this is the view :-

      <?php
      foreach ($student_list as $key => $item) {
      ?>
         <tr role="row" class="odd">
            <td class="sorting_1"><?= $item->student_uid; ?></td>
            <td class="sorting_1"><?= $item->username; ?></td>
            <td class="sorting_1"><?= $item->course_id; ?></td>

          </tr>
          <?php } ?>




Aucun commentaire:

Enregistrer un commentaire