douyi3767 2018-12-24 05:45 采纳率: 0%
浏览 78
已采纳

如何修复PHP,HTML中无法在DataTables上显示的数据

I've just recently discovered DataTables and would like to implement in my web. Followed the instructions for SSP data but it failed to output my data.

I've tried some of the solutions from several other websites but none works.

index.php

<script>
function() {
$('#staff').DataTable( {
    processing: true,
    serverSide: true,
    ajax: {
        url: 'data.php',
        type: 'POST'
    };
} );
}
</script>

data.php

<?php

$table = 'staff';

$primaryKey = 'staff_id';

 $columns = array(
array( 'db' => 'staff_id', 'dt' => 0 ),
array( 'db' => 'grade',  'dt' => 1 ),
array( 'db' => 'name',   'dt' => 2 ),
array( 'db' => 'position',     'dt' => 3 ),
array( 'db' => 'cost_centre',     'dt' => 4 ),
array( 'db' => 'station',     'dt' => 5 ),
array( 'db' => 'ic_number',     'dt' => 6 ),
array( 'db' => 'status',     'dt' => 7 )
);

// SQL server connection information
$sql_details = array(
'user' => 'root',
'pass' => '',
'db'   => 'feldatransport',
'host' => 'localhost'
);

require( 'ssp.class.php' );

echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
?>   

I would like the data to display in my staff table.

EDIT: I uploaded my file to my repo. Github

UPDATE: Issue has been solved. It was my arrangement of scripts. I put Jquery script lastly thus DataTables isn't able to function properly. Thanks for the help everyone

  • 写回答

2条回答 默认 最新

  • dongliuzi3410 2018-12-24 06:55
    关注

    Does your tag look something like this? The order of which scripts you import first (or missing one) can throw the entire datatables plug-in off.

    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=1">
        <title>DataTables Server-Side Processing</title>
    
       <!--       -->
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
         <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    
        <!-- datatable lib -->
        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
    
    </head>
    

    I have a running datatables server-side here: https://databasetable-net.000webhostapp.com/ Sometimes (I do not know why), you have to use $_POST not $_GET in the server.php even though the datatables example uses $_GET. That could be one issue. Anyway, here is my working code. Server.php

    <?php
    $table = 'employees';
    $primaryKey = 'id'; // Table's primary key
    
    $columns = array(
        array( 'db' => 'id', 'dt' => 0 ),
        array( 'db' => 'first_name', 'dt' => 1 ),
        array( 'db' => 'last_name',  'dt' => 2 ),
        array( 'db' => 'zip',   'dt' => 3 ),
        array( 'db' => 'date',     'dt' => 4 ),
         array( 'db' => 'updated',     'dt' => 5 ),
    );
    
    $sql_details = array(
        'user' => 'username',
        'pass' => 'password',
        'db'   => 'database',
        'host' => 'localhost'
    );
    
    require( 'ssp.class.php' );
    
    echo json_encode(
        SSP::simple( $_POST, $sql_details, $table, $primaryKey, $columns )
    );
    ?>
    

    Lastly, posting on datatables.net forums is very helpful as this is very specialized code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。