我在phpmyadmin中有3个表。 disease_table,medicine_table,disease_medicine_table。
病例:这种疾病可以有多种药物,这就是为什么我将第三张表格命名为疾病表和药物表的疾病表。
我的问题是如何获得疾病的所有记录。
我正在使用codeigniter框架
我想要的数据结果是:
这是我当前的结果
或者你有更好的想法来解决我的问题吗?
这是我的疾病表
这是我的药桌
这是我的疾病医疗表
我将在DataTable服务器端显示数据,这是我的代码
Ajax控制器
public function get_diseases_for_table() {
$data = [];
if(($ajax_data = $this->input->get()) && $this->input->is_ajax_request()) {
extract($ajax_data);
$data = [
'draw' => $draw,
'recordsTotal' => $this->diseases->get_diseases_records_total(),
'recordsFiltered' => $this->diseases->get_diseases_records_filtered($ajax_data)
];
$data['data'] = $this->diseases->get_diseases($ajax_data);
}
$this->view = FALSE;
echo json_encode($data);
}
模型
public function get_diseases_records_total() {
$sql = 'SELECT d.id, d.disease_name, m.med_name, d.created_at ';
$sql .= 'FROM diseases d ';
$sql .= 'INNER JOIN diseases_medicines dm ON d.id = dm.disease_id ';
$sql .= 'INNER JOIN medicines m ON dm.medicine_id = m.id ';
$query = $this->db->query($sql);
return $query->num_rows();
}
public function get_diseases_records_filtered($data) {
extract($data);
$params = [];
$sql = 'SELECT d.id, d.disease_name, m.med_name, d.created_at ';
$sql .= 'FROM diseases d ';
$sql .= 'INNER JOIN diseases_medicines dm ON d.id = dm.disease_id ';
$sql .= 'INNER JOIN medicines m ON dm.medicine_id = m.id ';
$sql .= 'WHERE 1 ';
if(!empty($search['value'])){
$sql .= 'AND (d.id = ? ';
$sql .= 'OR d.disease_name LIKE ? ';
$sql .= 'OR m.med_name LIKE ? ';
$sql .= 'OR d.created_at LIKE ?) ';
}
if(!empty($search['value'])){
$params[] = $search['value'];
$params[] = $search['value'];
$params[] = $search['value'];
$params[] = $search['value'];
}
$query = $this->db->query($sql, $params);
return $query->num_rows();
}
public function get_diseases($data, $details = false) {
extract($data);
$params = [];
$sql = 'SELECT d.id, d.disease_name, m.med_name, d.created_at ';
$sql .= 'FROM diseases d ';
$sql .= 'INNER JOIN diseases_medicines dm ON d.id = dm.disease_id ';
$sql .= 'INNER JOIN medicines m ON dm.medicine_id = m.id ';
$sql .= 'WHERE 1 ';
if(!empty($search['value'])){
$sql .= 'AND (d.id = ? ';
$sql .= 'OR d.disease_name LIKE ? ';
$sql .= 'OR m.med_name LIKE ? ';
$sql .= 'OR d.created_at LIKE ?) ';
}
if(isset($order)){
$sql .= 'ORDER BY ' . $columns[$order[0]['column']]['data'] . ' ' . strtoupper($order[0]['dir']) . ' ';//$order[0]['column']
}
$sql .= 'LIMIT ?, ?';
if(!empty($search['value'])){
$params[] = $search['value'];
$params[] = '%' . $search['value'] . '%';
$params[] = '%' . $search['value'] . '%';
$params[] = '%' . $search['value'] . '%';
}
$params[] = (int)$start;
$params[] = (int)$length;
$query = $this->db->query($sql, $params);
return ( $query->num_rows() ) ? $query->result() : FALSE;
}
脚本
$('#disease_list').DataTable({
'processing': true,
'serverSide': true,
'responsive': true,
'ajax': 'dis/diseases',
'deferRender': true,
'order': [[0, 'asc']],
'columns': [
{ 'data': 'id', 'sClass': 'text-center' },
{ 'data': 'disease_name' },
{ 'data': 'med_name' },
{ 'data': 'created_at' },
{ 'data': 'options' },
],
'columnDefs': [
{
'data': 'options',
'targets': -1,
'sortable': false,
'render' : function ( data, type, row ) {
var html = '<div class="text-center"> \
<div class="btn-group"> \
<button type="button" class="btn btn-default btn-xs dropdown-toggle btn-hc_app" data-toggle="dropdown"> \
<i class="fa fa-chevron-down fa-fw"></i> \
</button> \
<ul class="dropdown-menu pull-right" role="menu" style="padding: 0 0 7px 0"> \
<li> \
<span class="dropdown-title text-center">Action Bar</span> \
</li> \
<li> \
<a href="#" title="Edit" data-id="' + row.id + '"><i class="fa fa-edit fa-fw fg-green"></i> Edit Entry</a> \
</li> \
<li> \
<a href="#" title="Delete" title="Edit" data-id="' + row.id + '"><i class="fa fa-trash fa-fw fg-red"></i> Delete Entry</a> \
</li> \
</ul> \
</div> \
</div>';
return html;
}
}
],
'language': {
"processing": '<div class="processing-wrapper"> \
<div><i class="fa fa-spinner fa-spin"></i> Fetching ... Please wait...</div> \
</div>',
'emptyTable': 'No medicine type(s) available in the database!',
'zeroRecords': 'No medicine type(s) found!',
"infoFiltered": ""
}
});