Home > other >  Is there a way to join tables and display if the primary key of the other table is not recognize? PH
Is there a way to join tables and display if the primary key of the other table is not recognize? PH

Time:02-10

I have enlisted students in the first table and in the second table I have the status of students if they are enrolled.

All I would like to achieve is to display all Primary Keys' ID in my view so I can check if the student is not enrolled and enrolled

tbl_tt_enlistmentsecondary

enlistmentSecondaryID (PK) studentKeyID schoolYear semester
2 3560 2021-2022 Second Semester
4 3560 2021-2022 Second Semester

tbl_tt_enrollmentsecondary - The enlistmentSecondaryID id 4 is the unenrolled student, hence I would like to display it as well on my table view.

enrollmentSecondaryID (PK) enlistmentSecondaryID (FK) studentKeyID enrollmentStatus
8 2 3560 Officially Enrolled

My desired output: I would like to get these data in my table view:

enrollmentSecondaryID studentKeyID enrollmentStatus
2 3560 Officially Enrolled
4 3560

As you can see on the table above, the enlistmentSecondaryID 4 must also be shown in my table in my view. But the problem is I cannot get the Primary Key because when I fetch or display its Primary Key there's an error saying undefined index.

Here is my View:

<tbody>
    <?php foreach($shsEnlist as $enlist): ?>
    <tr>
        <th><?php echo $enlist['enlistmentSecondaryID'] ?></th>
        <th><?php echo $enlist['studentKeyID'] ?></th>
        <th><?php echo $enlist['enrollmentStatus'] ?></th>
    </tr>
    <?php endforeach; ?>
</tbody>

Here is my Model: I have used left join here.

public function getSHSenlist(){
    $this->db->join('tbl_tt_enrollmentsecondary', 'tbl_tt_enrollmentsecondary.enlistmentSecondaryID = tbl_tt_enlistmentsecondary.enlistmentSecondaryID', 'left');
    $this->db->where('tbl_tt_enlistmentsecondary.studentKeyID', 3560);
    $query = $this->db->get('tbl_tt_enlistmentsecondary');
    return $query->result_array();
}

CodePudding user response:

  •  Tags:  
  • Related