我能够成功获取下表中的数据。但是,在“班级老师”栏中,我想要教师姓名而不是教师ID(外键)
这是数据库表
以下是我在类视图中获取数据的方法
if( !empty($class_data) ) {
foreach ($class_data AS $row) {
?>
<?php echo $row->class_id; ?><?php echo $row->grade; ?> <?php echo $row->name; ?><?php echo $row->capacity; ?> <?php echo $row->teacher_id; ?>我的控制器
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));
}
教师数据库表
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
模型fetch_class_data函数
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;
}
}