Ann Ann - 6 months ago 15
HTML Question

Join tables and fetch the data to a table using codeigniter

I have two db tables named as

verification_details
and
verification_questions
.
verification_id
is common in two tables. 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.
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.

Answer

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>