【问题标题】:Trying to fetch the value from 2 tables in codeigniter code尝试从 codeigniter 代码中的 2 个表中获取值
【发布时间】:2021-01-03 11:40:59
【问题描述】:

在 2 张桌子上工作(家庭作业和交付)。我能够从交付表中获取“homework_code”的值。现在 Homework 表中也有相同的字段,没有主键。 我正在尝试从表“作业”中获取“标题”字段的值。两个表都有共同的 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;?>

【问题讨论】:

    标签: php html mysql codeigniter


    【解决方案1】:

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

    家庭作业表:-

    title
    student_id
    

    交货表:-

    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>
    

    【讨论】:

      【解决方案2】:

      根据您上面的代码,您可以像这样使用控制器

      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() 函数会为结果生成数组,即使它只有一个

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-18
        • 1970-01-01
        • 1970-01-01
        • 2021-07-22
        相关资源
        最近更新 更多