问题描述
我需要帮助,我有2个表Client和Product。我想基于客户表从产品表中获取数据,并整体显示。
productTable
productId productName productPrice
1 Abc 100
2 Bcd 200
3 Cde 300
clientTable
clientId productNameId clientName
4 2 A
5 3 B
6 1 C
我希望表格将记录显示为:
Client Name Product Name
A Bcd
B Cde
C Abc
如何使用MVC CI显示它们。
型号
class Client_model extends CI_model
{
function All()
{
return $client = $this->db->get('client')->result_array();
}
}
控制器
class Client extends CI_Controller
public function index()
{
$this->load->model('Client_model');
$invoice = $this->Client_model->All();
$data = array();
$data['client'] = $client;
$this->load->view('admin/ViewClient',$data);
}
}
解决方法
客户端控制器代码:-
class Client extends CI_Controller
public function index()
{
$this->load->model('Client_model');
$data['clientData'] = $this->Client_model->All();
$this->load->view('admin/ViewClient',$data);
}
}
Client_model型号代码:-
class Client_model extends CI_model
{
function All()
{
return $client = $this->db->get('client')->result_array();
}
}
ViewClient查看页面:-
<table>
<thead>
<tr>
<th>S.NO</th>
<th>client Name</th>
<th>Product Name</th>
</tr>
</thead>
<tbody>
<?php if(!empty($clientData)) {
$count=1;
foreach($clientData as $client){
?>
<tr>
<td><?php echo $count; ?></td>
<td><?php echo $client['clientName']; ?></td>
<td><?php
$pid = $client['productNameId'];
$pdata = $this->db->get_where('productTable',array('productId '=>$pid))->row();
echo $pdata->productName; ?>
</td>
</tr>
<?php $count++;
} }
?>
</tbody>
</table>