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.
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.