数据不使用 DataTable 从数据库中获取
Data doesn't fetch from the Database using DataTable
我是 jQuery DataTable
的新手。在这里,我正在尝试使用 DataTable
从 database
中获取 records
。我还使用 custom filter
提前 searching option
。
但情况是 records
没有从 database
中获取。它总是显示在 table 的底部,如:Showing 0 to 0 of 0 entries (filtered from 2 total entries)
。处理时未发生任何错误。
这是 HTML
代码。
<div class="col-sm-4">
<div class="form-group">
<label>Enter Appointment Date </label>
<input class="form-control" type="date" id="dates" name="dates">
<span id="type" class="info text-danger"></span><br />
</div>
</div>
<table id="example" style="width:100%" class="table table-hover">
<thead>
<tr>
<th>Apt ID</th>
<th>Doctor</th>
<th>Specialist</th>
<th>Patient</th>
<th>Type</th>
<th>Apt.Date</th>
<th>Status</th>
<th>Change</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<th>Apt ID</th>
<th>Doctor</th>
<th>Specialist</th>
<th>Patient</th>
<th>Type</th>
<th>Apt.Date</th>
<th>Status</th>
<th>Change</th>
<th>Action</th>
</tr>
</tbody>
</table>
这里是 query
;
$(document).ready(function () {
fill_datatable();
function fill_datatable(dates = '')
{
var dataTable = $('#example').DataTable({
"processing" : true,
"serverSide" : true,
"order" : [],
"searching" : false,
"ajax" : {
url:"adminquery/fetch/appointment/fetch_appointment.1.php",
type:"POST",
data:{
dates:dates
}
}
});
}
$('#dates').change(function(){
var dates = $('#dates').val();
if(dates != '')
{
$('#example').DataTable().destroy();
fill_datatable(dates);
}
else
{
$('#example').DataTable().destroy();
fill_datatable();
}
});
下面是fetch.php
$conn = new PDO("mysql:host=localhost;dbname=hmsproject", "root", "");
$columns= array('apt_id','username','specilization','patient_name','type','apt_date','admin_status','Change');
// $query = "SELECT * FROM appointment as a,users as u WHERE a.user_id= u.user_id";
$query = " SELECT * FROM appointment as a INNER JOIN doctor_schedule as d ON a.user_id=d.user_id";
if(isset($_POST['dates'] ))
{
$query .= 'AND a.apt_date = "'.$_POST['dates'].'"
';
}
if(isset($_POST['order']))
{
$query .= 'ORDER BY '.$column[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' ';
}
else
{
$query .= 'ORDER BY No DESC ';
}
$query1 = '';
if($_POST["length"] != -1)
{
$query1 = 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
$statement = $conn->prepare($query);
$statement->execute();
$number_filter_row = $statement->rowCount();
$statement = $conn->prepare($query . $query1);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
foreach($result as $row)
{
$sub_array = array();
$sub_array[] = $row['apt_id'];
$sub_array[] = $row['doctor_name'];
$sub_array[] = $row['specilization'];
$sub_array[] = $row['patient_name'];
$sub_array[] = $row['type'];
$sub_array[] = $row['apt_date'];
$sub_array[] =' <span class="custom-badge status-red">Cancelled</span>';
$data[] = $sub_array;
}
function count_all_data($conn)
{
$query = "SELECT * FROM appointment as a INNER JOIN doctor_schedule as d ON a.user_id=d.user_id";
$statement = $conn->prepare($query);
$statement->execute();
return $statement->rowCount();
}
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => count_all_data($conn),
"recordsFiltered" => $number_filter_row,
"data" => $data
);
echo json_encode($output);
调试显示如下:
{draw: 1, recordsTotal: 2, recordsFiltered: 0, data: []} data: []
draw: 1 recordsFiltered: 0 recordsTotal: 2
我不知道我哪里做错了。其实我对此很陌生。非常感谢任何帮助。
您的连接查询是否正确?
看看你不应该直接使用 ajax data / datatables >>> 首先看看你的页面在你的案例中生成了什么输出检查:url:"adminquery/fetch/appointment/fetch_appointment。 1.php
并为数据使用静态值 table,如果两者都工作正常,那么只需要更进一步
我是 jQuery DataTable
的新手。在这里,我正在尝试使用 DataTable
从 database
中获取 records
。我还使用 custom filter
提前 searching option
。
但情况是 records
没有从 database
中获取。它总是显示在 table 的底部,如:Showing 0 to 0 of 0 entries (filtered from 2 total entries)
。处理时未发生任何错误。
这是 HTML
代码。
<div class="col-sm-4">
<div class="form-group">
<label>Enter Appointment Date </label>
<input class="form-control" type="date" id="dates" name="dates">
<span id="type" class="info text-danger"></span><br />
</div>
</div>
<table id="example" style="width:100%" class="table table-hover">
<thead>
<tr>
<th>Apt ID</th>
<th>Doctor</th>
<th>Specialist</th>
<th>Patient</th>
<th>Type</th>
<th>Apt.Date</th>
<th>Status</th>
<th>Change</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<th>Apt ID</th>
<th>Doctor</th>
<th>Specialist</th>
<th>Patient</th>
<th>Type</th>
<th>Apt.Date</th>
<th>Status</th>
<th>Change</th>
<th>Action</th>
</tr>
</tbody>
</table>
这里是 query
;
$(document).ready(function () {
fill_datatable();
function fill_datatable(dates = '')
{
var dataTable = $('#example').DataTable({
"processing" : true,
"serverSide" : true,
"order" : [],
"searching" : false,
"ajax" : {
url:"adminquery/fetch/appointment/fetch_appointment.1.php",
type:"POST",
data:{
dates:dates
}
}
});
}
$('#dates').change(function(){
var dates = $('#dates').val();
if(dates != '')
{
$('#example').DataTable().destroy();
fill_datatable(dates);
}
else
{
$('#example').DataTable().destroy();
fill_datatable();
}
});
下面是fetch.php
$conn = new PDO("mysql:host=localhost;dbname=hmsproject", "root", "");
$columns= array('apt_id','username','specilization','patient_name','type','apt_date','admin_status','Change');
// $query = "SELECT * FROM appointment as a,users as u WHERE a.user_id= u.user_id";
$query = " SELECT * FROM appointment as a INNER JOIN doctor_schedule as d ON a.user_id=d.user_id";
if(isset($_POST['dates'] ))
{
$query .= 'AND a.apt_date = "'.$_POST['dates'].'"
';
}
if(isset($_POST['order']))
{
$query .= 'ORDER BY '.$column[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' ';
}
else
{
$query .= 'ORDER BY No DESC ';
}
$query1 = '';
if($_POST["length"] != -1)
{
$query1 = 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
$statement = $conn->prepare($query);
$statement->execute();
$number_filter_row = $statement->rowCount();
$statement = $conn->prepare($query . $query1);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
foreach($result as $row)
{
$sub_array = array();
$sub_array[] = $row['apt_id'];
$sub_array[] = $row['doctor_name'];
$sub_array[] = $row['specilization'];
$sub_array[] = $row['patient_name'];
$sub_array[] = $row['type'];
$sub_array[] = $row['apt_date'];
$sub_array[] =' <span class="custom-badge status-red">Cancelled</span>';
$data[] = $sub_array;
}
function count_all_data($conn)
{
$query = "SELECT * FROM appointment as a INNER JOIN doctor_schedule as d ON a.user_id=d.user_id";
$statement = $conn->prepare($query);
$statement->execute();
return $statement->rowCount();
}
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => count_all_data($conn),
"recordsFiltered" => $number_filter_row,
"data" => $data
);
echo json_encode($output);
调试显示如下:
{draw: 1, recordsTotal: 2, recordsFiltered: 0, data: []} data: [] draw: 1 recordsFiltered: 0 recordsTotal: 2
我不知道我哪里做错了。其实我对此很陌生。非常感谢任何帮助。
您的连接查询是否正确? 看看你不应该直接使用 ajax data / datatables >>> 首先看看你的页面在你的案例中生成了什么输出检查:url:"adminquery/fetch/appointment/fetch_appointment。 1.php
并为数据使用静态值 table,如果两者都工作正常,那么只需要更进一步