尝试从 codeigniter 代码中的 2 个表中获取值

Trying to fetch the value from 2 tables in codeigniter code

正在处理 2 table 秒(家庭作业和交付)。我能够从 Deliveries Table 中获取“homework_code”的值。现在家庭作业 table 中也有相同的字段,但没有主键。 我正在尝试从 table “作业”中获取“标题”字段的值。两个 table 的共同值都是 student_id。代码是:

<?php
$invoices = $this->db->get_where('deliveries', array('student_id' => $row['student_id']))->result_array();
foreach($invoices as $row2): ?>
<tr>
<td>
<?php echo $row2['homework_code'];?>
</td>
<td>
<?php echo $this->db->get_where('homework' , array('homework_code'=>'class_id'))->row()->title; ?>&nbsp;&nbsp;<?php echo $row2['title'];?>
     </td> 
</tr>
<?php endforeach;?>

根据你上面的问题,如果你的数据库结构是这样的,那么使用:-

作业Table:-

title
student_id

交货 Table :-

homework_code  
student_id 

然后更改为:-

<td>
<?php echo $this->db->get_where('homework' , array('homework_code'=>'class_id'))->row()->title; ?>&nbsp;&nbsp;<?php echo $row2['title'];?>
</td>

这样:-

<td>
<?php
$student_id = $row2['student_id'];
$homework = $this->db->get_where('homework',array('student_id'=>$student_id))->result_array(); 
echo $homework['title'];
?>
</td>

根据你上面的代码,你可以像这样使用Controller

public function get()
    {
        $data = $this->db->get_where('deliveries', array('student_id' => $row['student_id']))->result_array();
        $fetch['fetchdata'] = $this->db->get_where('homework', array($data[0]['homework_code'] => 'class_id'))->result_array();

        $this->load->view('yourview', $fetch);
    }

并通过将其放在 View 上得到输出:

<?= $fetchdata[0][title] ?>

然而 result_array() 函数将为结果生成数组,即使它只有一个