如何合并DataTable服务器端的多行显示

时间:2016-07-22 15:31:46

标签: php jquery mysql codeigniter datatables

我在phpmyadmin中有3个表。 disease_table,medicine_table,disease_medicine_table。

病例:这种疾病可以有多种药物,这就是为什么我将第三张表格命名为疾病表和药物表的疾病表。

我的问题是如何获得疾病的所有记录。

我正在使用codeigniter框架

我想要的数据结果是:

  • id - disease_name - 药物
  • 1 - 牙痛 - med1,med2,med3,med4
  • 2 - 头痛 - med4,med8,med2,med5

这是我当前的结果

datatable result

或者你有更好的想法来解决我的问题吗?

这是我的疾病表

  • ID(INT11)
  • disease_name(varchar255)

这是我的药桌

  • ID(INT11)
  • med_name(varchar255)
  • dosage_strength(varchar255)
  • 价(decimal65,2)

这是我的疾病医疗表

  • ID(INT11)
  • disease_id(INT11)
  • medicine_id(INT11)
  • 剂量(varchar255)
  • 量(的int64)

我将在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": ""
    }
});

0 个答案:

没有答案