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

Php 使用codeigniter连接表并将数据提取到表中,php,html,mysql,codeigniter,Php,Html,Mysql,Codeigniter,我有两个db表,分别命名为verification\u details和verification\u questions验证id在两个表中很常见。在verification\u details表中,有一个user\u id字段,基于此user\u id字段,一个或多个验证详细信息插入到验证详细信息表中,并基于验证id一个或多个验证问题插入到验证问题表中。 我在模型中使用此代码进行连接查询 function get_records() { $this->db->select(

我有两个db表,分别命名为
verification\u details
verification\u questions
<代码>验证id在两个表中很常见。在
verification\u details
表中,有一个
user\u id
字段,基于此
user\u id
字段,一个或多个验证详细信息插入到
验证详细信息
表中,并基于
验证id
一个或多个验证问题插入到
验证问题
表中。 我在模型中使用此代码进行连接查询

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

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

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

型号:

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>
您可以在控制器中使用此函数来获取结果数组

控制器:

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>
最后,您必须将结果变量传递给视图,以便在表中显示

查看:

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>
问题>
如果(计数($result)>0){
foreach($result作为$row){?>
在模型中

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>

问题:
答复
验证

您的代码几乎正确。但是您让它变得有点长,我的意思是不需要使用foreach循环两次,您也可以使用一次来实现它!!!但是我感谢您的指导!!