【问题标题】:Join tables and fetch the data to a table using codeigniter使用 codeigniter 连接表并将数据提取到表中
【发布时间】:2015-12-12 12:47:44
【问题描述】:

我有两个名为verification_detailsverification_questions 的数据库表。 verification_id 在两个表中很常见。在verification_details表中,有一个user_id字段,基于此user_id字段,插入verification_details表中的一个或多个验证细节,并基于verification_id插入一个或多个验证问题@ 987654329@ 表。 我在模型中使用此代码进行连接查询

function get_records() {

    $this->db->select("a.criteria_question,a.criteria_answer,a.validation_statement");
        $this->db->from("verification_questions as a");
        $this->db->join('verification_details as b', 'a.verification_id = b.verification_id');
        $query = $this->db->get();
        return $query->result();    
}

我想要控制器代码用于从两个表中获取所有数据并将其显示在一个表中。

【问题讨论】:

    标签: php html mysql codeigniter


    【解决方案1】:

    在模型中

    function get_records() {
    
        $this->db->select("a.criteria_question,a.criteria_answer,a.validation_statement");
        $this->db->from("verification_questions as a");
        $this->db->join('verification_details as b', 'a.verification_id = b.verification_id');
        $query = $this->db->get();
        $result = $query->result_array();
        return $result;
    }
    

    在控制器中

    $data['table'] = $this->Model_name->get_records();
    $this->load->view('view_name',$data);
    

    在视野中

    <table>
        <tr>
            <th>Question</th>
            <th>Answer</th>
            <th>Validation</th>
        </tr>
        <?php
            foreach ( $table as $new_item )
            {
                ?>
                <tr>
                    <td><?php echo $new_item['table field1']?></td>
                    <td><?php echo $new_item['table field2']?></td>
                    <td><?php echo $new_item['table field3']?></td>
                </tr>
            <?php
            }
    
        ?>
    </table>
    

    【讨论】:

      【解决方案2】:

      您可以在模型中创建结果数组并在控制器中使用它。

      型号:

      function get_records()
      {
              $this->db->select("a.criteria_question,a.criteria_answer,a.validation_statement");
              $this->db->from("verification_questions as a");
              $this->db->join('verification_details as b', 'a.verification_id = b.verification_id');
              $query = $this->db->get();
              $select = array();
              foreach ($query->result() as $row) {
                  $select[] = $row;
              }
              if (count($select) > 0)
                  return $select;
              return NULL;
      }
      

      您可以在控制器中使用此函数来获取结果数组。

      控制器:

      $data['result']=$this->modelname->get_records();
      $this->load->view('your_view', $data);
      

      最后你必须将结果变量传递给视图以显示在表格中。

      查看:

      <table><tr><td>Question></td></tr>
          if(count($result)>0){
              foreach($result as $row){ ?>
                  <tr><td><?= $row->criteria_question; ?></td></tr>
              <?php}
          } ?>
      </table>
      

      【讨论】:

      • 您的代码几乎是正确的。但是你让它有点长我的意思是不需要使用 foreach 循环两次你也可以使用一次来实现它!但我感谢您的指导!!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多