【发布时间】:2021-03-11 10:42:31
【问题描述】:
谁能帮助我如何在数据表中显示我的数据?我正在尝试服务器端数据表,但我无法得到它。无论如何,我可以以 JSON 格式返回我的数据,但我总是收到此错误:DataTables 警告:表 id=show-employee-table - 无法重新初始化 DataTable。
这些是我尝试过的:
ajax.js
var url = '../../class/functions.php';
function show_employee() {
var data = { action : 'Show Employee Table' };
$("#show-employee-table").dataTable({
ajax: {
type: 'POST', url: url, data: data
}
})
}
functions.php
<?php
include 'config.php';
switch($_POST['action']) {
case 'Show Employee Table':
$data->show_employee_table();
break;
}
?>
class.php
<?php
include 'controller.php';
class db extends Controller {
//Get all employees
public function get_employee() {
$query = $this->db->query("SELECT * FROM tbl_accounts WHERE employee_role_id = 2");
return $query;
}
public function show_employee_table() {
foreach($this->get_employee() as $row): ?>
<?php $data = $row; ?>
<?php $employee_status = ($row['employee_status'] == "Active") ? '<span class="badge badge-success">Active</span>' : '<span class="badge badge-danger">Not Active</span>';?>
<?php endforeach;
echo json_encode(['data' => $data]);
}
}
?>
controller.php
<?php
class Controller {
public $host = 'localhost';
public $database_username = 'root';
public $database_password = '';
public $database_name = 'try_db';
public $db;
function __construct() {
$this->connection();
}
private function connection() {
$this->db = new mysqli($this->host, $this->database_username, $this->database_password, $this->database_name);
if($this->db->connect_error) {
echo 'Could not connect to the database!';
}
}
}
?>
config.php
<?php
session_start();
ob_start();
include 'class.php';
//include 'init.php';
$data = new db();
?>
【问题讨论】:
-
为什么class.php上有
<?php标签?如果您发布完整的 class.php 文件,我可能会为您提供帮助。 -
我会编辑它。等一下。
-
已编辑。你能检查一下吗?非常感谢! @ThamiraMadusanka
标签: php ajax datatables datatables-1.10