【问题标题】:Data doesn't fetch from the Database using DataTable数据不使用 DataTable 从数据库中获取
【发布时间】:2021-10-03 19:54:03
【问题描述】:

我是jQuery DataTable 的新手。这里只是我试图使用DataTabledatabase 获取records。我也使用custom filter 提前searching option

但情况是 records 不会从 database 获取。它始终显示在表格底部,例如: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: [] 绘制:1 条记录过滤:0 条记录总数:2

我不知道我哪里出错了。其实我是新手。任何帮助都将受到高度赞赏。

【问题讨论】:

    标签: php mysql datatable datatables


    【解决方案1】:

    您的连接查询是否正确? 看到你不应该直接使用ajax数据/数据表>>>首先看看你的页面在你的案例中生成了什么输出:url:“adminquery/fetch/appointment/fetch_appointment.1.php”

    并为数据表使用静态值,如果两者都工作正常,那么就走得更远

    【讨论】:

    • 显示如下:{draw: 1, recordsTotal: 2, recordsFiltered: 0, data: []} data: [] draw: 1 recordsFiltered: 0 recordsTotal: 2
    • 看到你的输出不正确或不完整,它应该是这样的api.jsonserve.com/zAj8b7
    • 正确的数据会产生这样的输出jsfiddle.net/kd1q3sz9
    • 感谢您的及时回复。你能帮我解决这个问题吗?
    • 可能是,但更正你的代码静态值,意味着不要从数据库中获取值,手动使用 json 并创建一个表
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 2012-03-04
    • 2016-11-21
    • 2014-04-10
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多