在php中加入mysql语句中的4个表

i dont know how to explain it well but what i want is that i want to display the instrutors name and courses title in my subject table and the day&time of the subject and at first i can display the subjects with its instructor name and course title but when i display the day&time of it.it dont fit on my desired output..

here is my sample code:

//subjectClass.php
public function subjects(){
            global $db;

    $sql = "
    SELECT s.*
         , i.first_name
         , i.mid_name
         , i.last_name
         , c.course_title
         , d.sub_day
         , d.start_time
         , d.end_time 
      FROM subjects_tbl s
      LEFT 
      JOIN instructors_tbl i
        ON i.instructor_id = s.instructor_id 
      LEFT 
      JOIN courses_tbl c
        ON c.course_id = s.course_id
      LEFT 
      JOIN subject_day_tbl d
        ON d.subject_id = s.subject_id;
      ";

            $query = $db->query($sql);
            if($query->num_rows > 0){
                while($row = $query->fetch_assoc()){
                    $list[] = $row;
                }
            }else{
                $list = NULL;
            }

            return $list;
        }
//subjects.php
//include 'subjectsClass.php';
<table class="table table-condensed table-bordered">
        <thead>
          <tr>
            <th>Subject Code</th>
            <th>Subject Title</th>
            <th>Unit</th>
            <th>Section</th>         
            <th>Course</th>
            <th>Instructor</th>
            <th>Day/Time</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <?php
            $subjectsClass = new subjectsClass;
            $subjects = $subjectsClass->subjects();

            foreach ($subjects as $key => $value) {

          ?>
          <tr>
            <td><?php echo $value['subject_code']; ?></td>
            <td><?php echo $value['subject_title']; ?></td>
            <td><?php echo $value['unit']; ?></td>
            <td><?php echo $value['section']; ?></td>
            <td><?php echo $value['course_title']; ?></td>
            <td><?php echo $value['first_name'] . " " . $value['mid_name'] . ". " . $value['last_name'] ; ?></td>
            <td>
              <?php echo $value['sub_day'] . " [" . $value['start_time'] . " - " . $value['end_time'] . "]<br />"; ?>
            </td>
            <td><a href="#">Edit</a> | <a href="#">Delete</a></td>
          </tr>
          <?php
              }

          ?>
        </tbody>

and here is the output:

Subject Code Subject Title                  Unit Section Course Instructor              Day/Time
ITE 131      Security Issues and Principles    3 IT-R    BSIT   Darwin Paradela. Galudo Monday    [07:30:00 - 09:00:00]
ITE 131      Security Issues and Principles    3 IT-R    BSIT   Darwin Paradela. Galudo Wednesday [08:30:00 - 10:00:00]
ITE 050      Database Management System 2      3 IT-R    BSIT   Ronnie Pitt. Cambangay  Tuesday   [07:00:00 - 08:30:00]
ITE 050      Database Management System 2      3 IT-R    BSIT   Ronnie Pitt. Cambangay  Thursday  [07:00:00 - 08:30:00]

my desired output is this one:

+--------------+--------------------------------+------+---------+--------+-------------------------+---------------------------------+
| Subject Code | Subject Title                  | Unit | Section | Course | Instructor              | Day/Time                        |
+--------------+--------------------------------+------+---------+--------+-------------------------+---------------------------------+
| ITE 131      | Security Issues and Principles |    3 | IT-R    | BSIT   | Darwin Paradela. Galudo | Monday    [07:30:00 - 09:00:00] |
|              |                                |      |         |        |                         | Wednesday [08:30:00 - 10:00:00] |
+--------------+--------------------------------+------+---------+--------+-------------------------+---------------------------------+
| ITE 050      | Database Management System 2   |    3 | IT-R    | BSIT   | Ronnie Pitt. Cambangay  | Tuesday   [07:00:00 - 08:30:00] |
|              |                                |      |         |        |                         | Thursday  [07:00:00 - 08:30:00] |
+--------------+--------------------------------+------+---------+--------+-------------------------+---------------------------------+

my tables: subjects_tbl enter image description herecourses_tbl enter image description hereinstructors_tbl enter image description heresubject_day_tbl enter image description here

I hope I can help you.

Your issue seems to be the left join of subject_day_tbl. It is a one-to-many relationship (ie, there can be many records in subject_day_tbl for each record in subjects_tbl), and when you left join a one-to-many you'll get a duplicate of the "one" side for each row in the "many" side. The only way that you could do this in a single query is by using a subquery or group statement to concatenate the rows in the database engine... but that is really bad because you're mixing display with your data model.

While it is true in general that for performance one should avoid issuing too many queries to the database, premature optimization is the root of all evil. First, try to have clean, understandable code, then look for bottlenecks if you're having issues.

In this case, a second query is certainly best. This is how I would do it:

//subjectClass.php
protected function subject_days($subject_id)
{
   // I don't know what type of object $db is it looks like ezSQL,
   // but you get the idea
   global $db;
   $sql = "SELECT sub_day, start_time, end_time
           FROM subject_day_tbl
           WHERE subject_id = %s";
   $query = $db->query($db->prepare($sql, $subject_id));
   return ($query->num_rows > 0) ? $stmt->fetch_assoc() : array();
}

public function subjects()
{
    global $db;
    $sql = "SELECT s.*
              , i.first_name
              , i.mid_name
              , i.last_name
              , c.course_title
              , d.sub_day
              , d.start_time
              , d.end_time 
            FROM subjects_tbl s
            LEFT JOIN instructors_tbl i
                   ON i.instructor_id = s.instructor_id 
            LEFT JOIN courses_tbl c
                   ON c.course_id = s.course_id
            ";
    $list = array();
    $query = $db->query($sql);
    if($query->num_rows > 0){
        while($row = $query->fetch_assoc()){
            $row['course_days'] = $this->subject_days($row['subject_id']);
            $list[] = $row;
        }
    }
    return empty($list) ? NULL : $list;
}

// subjects.php
$subjectsClass = new subjectsClass;
$subjects = $subjectsClass->subjects();
foreach ($subjects as $key => $value) {
    ?><tr>
        <td><?php echo $value['subject_code']; ?></td>
        <td><?php echo $value['subject_title']; ?></td>
        <td><?php echo $value['unit']; ?></td>
        <td><?php echo $value['section']; ?></td>
        <td><?php echo $value['course_title']; ?></td>
        <td><?php echo $value['first_name'] . " "
                     . $value['mid_name'] . ". "
                     . $value['last_name'] ; ?></td>
        <td><?php foreach($value['course_days'] as $day) {
            echo $value['sub_day'] . " [" . $value['start_time'] . " - " . $value['end_time'] . "]<br />";
        }?></td>
        <td><a href="#">Edit</a> | <a href="#">Delete</a></td>
    </tr><?php
}

Also, not to be a pedant, but you should really be escaping your output before echoing it, eg with htmlspecialchars.