Невозможно правильно загрузить динамические данные в datatable с использованием jQuery и PHP - PullRequest
0 голосов
/ 03 мая 2018

Я не могу отобразить динамические данные, поступающие из базы данных в таблицу, используя jQuery DataTable и PHP. Мой код ниже.

report.html:

    <table class="table table-striped table-bordered " id="dtblApplicationDetail">
        <thead>
            <tr>
                <th class="text-center">#</th>
                <th class="text-center">Regd. No.</th>
                <th class="text-center">Name</th>
                <th class="text-center">Paper 1</th>
                <th class="text-center">Paper 2</th>
                <th class="text-center">Paper 3</th>
                <th class="text-center">Paper 4</th>
            </tr>
        </thead>
        <tbody>

        </tbody>
  </table>

Выше приведена таблица, в которой я хочу отобразить динамические данные.

report.js:

function load_table(cmbExamgrp,cmbExamtype,cmbExamName,session,cmbExamZone,cmbExamCentre,cmbExamInstitute){
    $.ajax({
        url:"exam_registrant_report_db.php?type=GET_APPLICANTS",
        mType:"POST",
        data:{cmbExamgrp:cmbExamgrp,cmbExamtype:cmbExamtype,cmbExamName:cmbExamName,_s:session,cmbExamZone:cmbExamZone,cmbExamCentre:cmbExamCentre,cmbExamInstitute:cmbExamInstitute},
        success:function(response){  
            console.log('filter response',response);            
            //console.log(response);    
            var res1 = JSON.parse(response);                    
            var table = $('#dtblApplicationDetail').DataTable();
            table.clear().draw();
            table.rows.add(res1.aaData).draw(); 
        },
        error:function()
        {
            alert("We are unable to Process.Please contact Support");
        }
    });
}

Здесь я отправляю запрос на сервер, и ниже приведен PHP-скрипт.

report.php:

$sQuery="select em.exam_group,em.exam_type,em.exam_code,em.exam_name,aps.regn_no,aps.paper_code,aps.exam_code,aps.subject_code,ram.reg_no,ram.appl_no,aao.reg_user_id,aao.appl_no,arm.reg_user_id,arm.first_name,arm.last_name,icm.exam_group_code,icm.zone_code,icm.exam_centre_code,icm.institute_code from exam_master as em inner join exam_applicant_subject as aps on em.exam_code=aps.exam_code inner join registered_applicant_master as ram on aps.regn_no=ram.reg_no inner join applicant_appl_overview as aao on aao.appl_no=ram.appl_no inner join applicant_reg_master as arm on aao.reg_user_id=arm.reg_user_id inner join institute_to_centre_mapping as icm on em.exam_group=icm.exam_group_code where em.exam_group='".$cmbExamgrp."' and em.exam_type='".$cmbExamtype."' and em.exam_code='".$cmbExamName."' and icm.zone_code='".$cmbExamZone."'";
        $rResult = mysqli_query($con,$sQuery ) or fatal_error( 'MySQL Error: ' . mysqli_errno() );
        while ($row=mysqli_fetch_array($rResult)) {
            $output[]=$row;
        }
        $result = []; // Initialize result array
        foreach ($output as $key => $value) {
            $name = $value['first_name'] . ' ' . $value['last_name'];

            // check if same name already has entry, create one if not
            if (!array_key_exists($name, $result)) {
                $result[$name] = array(
                    'regn_no' => $value['reg_no'],
                    'name' => $name,
                    'paper1' => '',
                    'paper2' => '',
                    'paper3' => '',
                    'paper4' => ''
                    );
            }

            // count array elements with value, then set paper number and value
            $paper = 'paper' . (count(array_filter($result[$name])) - 1);
            $result[$name][$paper] = $value['paper_code'];
        }

        $result = array_values($result); // reindex result array 
        $slno = 1;
        foreach ($result as $key => $value) {
            array_unshift($value,$slno);
            $result1['aaData'][]=$value;        
            $slno++;
        }
        echo json_encode( $result1 );

После завершения работы с базой данных я получаю вывод, который выводится на консольное сообщение, приведенный ниже.

//console.log('filter response',response);
filter response {"aaData":[{"0":1,"regn_no":"MN1710366","name":"RUPENDRA GOND","paper1":"BA101","paper2":"BA102","paper3":"","paper4":""}]}

Приведенные выше данные мне нужно отобразить в dataTable, что не происходит в моем случае. Как я могу решить эту проблему?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...