glazilla/datatable

Class generating JSON output form mysql/pdo pgsql/pdo usable for Javascript library DataTables

Installs: 2 981

Dependents: 0

Suggesters: 0

Security: 0

Type:project

1.3.0 2025-07-08 10:13 UTC

This package is auto-updated.

Last update: 2025-07-08 10:15:52 UTC


README

Packagist

Glazilla\DataTable is a PHP class to simplify server-side integration of jQuery DataTables with MariaDB and PostgreSQL, with advanced features: filtering, ordering, pagination and ES6 compatibility.

Installation

composer require glazilla/datatable

Usage

Create PDO Connection

$pdo = new PDO('mysql:host=127.0.0.1;dbname=testdb', 'test', 'test'); // MariaDB
$pdo = new PDO('pgsql:host=127.0.0.1;dbname=testdb', 'test', 'test'); // PostgreSQL

If you wan't to search using UNACCENT postgreSQL extension, activate the extension and insert this constant in your code :

define("UNACCENT_QUERY_PG", "1");

PHP

use Glazilla\DataTable\DataTable;

$dt = new DataTable($pdo);
echo $dt->query(['name', 'email'], 'id', 'users', '1=1', [], 2);

Or for PostgreSQL:

echo $dt->queryPG(['name', 'email'], 'id', 'users', '1=1', [], 2);

JavaScript

<table id="myTable"><thead><tr><th>Name</th><th>Email</th></tr></thead></table>
<script>
$('#myTable').DataTable({
  serverSide: true,
  ajax: '/data.php',
  columns: [{ data: 0 }, { data: 1 }]
});
</script>

Docker demo

Run:

docker compose up -d

Open http://localhost:8080

History

  • 2025-07-08 - Added support for the ES6 dataTables.mjs format (by default, the old format remains in place): the new $retourFormat parameter allows you to choose the return format (1: old, 2: ES6)
  • 2025-06-11 - Fixed bug in postgresql filter
  • 2025-03-31 - Modified QueryPG to use PreparedStatement
  • 2024-02-08 - UNACCENT evaluated as constant or env var
  • 2023-12-21 - Add constant to search with PostgreSQL using UNACCENT PG extension (has to be initialized prior tu use) : Use define("UNACCENT_QUERY_PG", "1");
  • 2022-12-04 - Add possibility to combine searches with | (or) in input fields
  • 2022-03-03 - Fixed typo
  • 2021-10-16 - Fixed search on numeric types
  • Use QueryPG for PosgreSQL
  • Use Query for MySQL / MariaDB

License

MIT