简体   繁体   English

使用codeigniter连接表并将数据提取到表中

[英]Join tables and fetch the data to a table using codeigniter

I have two db tables named as verification_details and verification_questions . 我有两个名为verification_detailsverification_questions数据库表。 verification_id is common in two tables. verification_id在两个表中很常见。 In verification_details table, there is a user_id field, based on this user_id field, one or more verification details inserted into the verification_details table and based on the verification_id one or more verification questions inserted into the verification_questions table. verification_details表中,有一个user_id场,在此基础上user_id字段,一个或多个验证信息插入到verification_details表以及基于verification_id插入一个或多个验证问题verification_questions表。 I use this code in model for join query 我在模型中使用此代码进行连接查询

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();    
}

I want the controller code for fetching all datas from both tables and display it in a table. 我想要控制器代码从两个表中获取所有数据并将其显示在表中。

In Model 在模型中

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;
}

In contoller 在控制器中

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

and in view 在视野中

<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>

You can create array of result in model and use it in controller. 您可以在模型中创建结果数组并在控制器中使用它。

Model: 模型:

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;
}

You can use this function in controller to get result array. 您可以在控制器中使用此函数来获取结果数组。

Controller: 控制器:

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

And, finally you have to pass result variable to view to display in table. 最后,您必须将结果变量传递给视图才能在表格中显示。

View: 视图:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM