0
votes

I was able to successfully fetch data in the following table. However, in the 'Class Teacher' column, I want the teacher name instead of the teacher id (foreign key) enter image description here

This is the database table

enter image description here

Here is how I fetch data in the class view

if( !empty($class_data) ) {
                                    foreach ($class_data AS $row) {
                                        ?>
                                        <tr>
                                            <td><?php echo $row->class_id; ?></td>
                                            <td><?php echo $row->grade; ?> </td>
                                            <td><?php echo $row->name; ?></td>
                                            <td><?php echo $row->capacity; ?></td>

                                            <td> <?php echo $row->teacher_id; ?></td>

my controller

function index()
{

    $teacher_data= $this->Tableview_model->fetch_teacher_data();
    $class_data = $this->Tableview_model->fetch_class_data();

    $data["teacher_data"]  =  $teacher_data;
    $data["class_data"]  =  $class_data;


    $this->load->view('classes',$data);
}

function add_class()
{
    $class_data = array( 
        'class_id' => '',
        'grade' => $this->input->post('grade'),
        'name' => $this->input->post('class_name'),
        'capacity' => $this->input->post('capacity'),
        'teacher_id' => $this->input->post('class_teacher'),

    );


    $insert = $this->Model_Action->insertTable('class',$class_data);

    echo json_encode(array("status" => TRUE));


}

teacher database table

CREATE TABLE teacher ( teacher_id int(11) NOT NULL AUTO_INCREMENT, t_f_name varchar(250) NOT NULL, t_last_name varchar(250) NOT NULL, DOB date NOT NULL, t_email varchar(250) NOT NULL, t_address varchar(500) NOT NULL, t_tel varchar(150) NOT NULL, t_username varchar(250) NOT NULL, t_password varchar(500) NOT NULL, qualifications varchar(5000) NOT NULL, t_date_of_join date NOT NULL, t_date_of_leaving date NOT NULL, t_current_status int(1) NOT NULL, PRIMARY KEY (teacher_id) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1

Model fetch_class_data function

  function fetch_class_data()
  {
    $this->db->select("*");
            $this->db->from('class');

            $query=$this->db->get();

        if($query->num_rows() > 0) 
        {
            return $query->result();


        }else{
            return false;
        }


  }
1
Can you share teachers table structure and model function code? - Satish Saini
I did update the question with teachers table and model function code - deeva
Answered below. You need to use a join to get teacher name. - Satish Saini

1 Answers

2
votes

You need to use JOIN between class and teacher table. Please change your model code to:

$this->db->select('c.*, t.t_f_name, t.t_last_name')
         ->from('class c')
         ->join('teacher t', 'c.teacher_id = t.teacher_id');
$query = $this->db->get();

Use t_f_name and t_last_name in your view as full name by concatenating them

So, replace

<td><?php echo $row->teacher_id; ?></td>

with

<td><?php echo $row->t_f_name . " " . $row->t_last_name; ?></td>