I have two tables (table A and B). I need to show them in one table, but have an issue with how I to display the records on my table. Please see my sample:
Table A:
id name
1 John
2 Mark
3 Nick`
Table b:
id job
1 Encoder
2
3 Programmer`
I made a query to connect the two tables
select a.id, a.name, b.job from table_a a
left join table_b b on b.id=a.id
order by a.id;
Using codeigniter,
This is the controller:
$this->db->select('a.id, a.name, b.job);
$this->db->from('table a');
$this->db->join('table b', 'b.id=a.id', 'left');
$this->db->order_by('a.id');
$query = $this->db->get();
$data["view_records"]=$query;
$this->load->view("table_name", $query);
This is the view
<table id="tablestyle" class="table table-bordered table-hover table-condensed">
<col width="50">
<col width="150">
<col width="150">
<col width="150">
<thead>
<tr>
<th><strong>ID</strong></th>
<th><strong>NAME</strong></th>
<th><strong>JOB</strong></th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach($view_records->result() as $row) { ?>
<tr>
<td><?php echo $row->a.id; ?></td>
<td><?php echo $row->a.name; ?></td>
<td><?php echo $row->b.job; ?></td>
<td><input type="submit" value="Modify" id="btnModify" class="btn btn-block btn-success btn-xs" onclick="btnModify('<?php echo $row->a.id; ?>');"/></td>
</tr>
<?php } ?>
</tbody>
</table>
In Model
public function get_user()
{
$query = $this->db->query("select a.id, a.name, b.job from table_a a
left join table_b b on b.id=a.id
order by a.id");
$result = $query->result_array();
return $result;
}
In Controller
$data['get_user'] = $this->model_name->get_user();
$this->load->view("view_name", $data);
In View
<table id="tablestyle" class="table table-bordered table-hover table-condensed">
<col width="50">
<col width="150">
<col width="150">
<col width="150">
<thead>
<tr>
<th><strong>ID</strong></th>
<th><strong>NAME</strong></th>
<th><strong>JOB</strong></th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach($get_user as $rowItem) { ?>
<tr>
<td><?php echo $rowItem['a.id'] ?></td>
<td><?php echo $rowItem['a.name'] ?></td>
<td><?php echo $rowItem['b.job'] ?></td>
<td><input type="submit" value="Modify" id="btnModify" class="btn btn-block btn-success btn-xs" onclick="btnModify('<?php echo $rowItem['a.id'] ?>');"/></td>
</tr>
<?php } ?>
</tbody>
</table>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments