Example Updated - Aggrid Php
Integrating allows you to leverage a high-performance JavaScript frontend with a reliable server-side backend. While AG Grid is framework-agnostic on the frontend (supporting React, Angular, Vue, and Vanilla JS), it uses PHP on the server to handle data retrieval, filtering, and sorting for large datasets. Updated Implementation Summary The standard modern approach involves using a Server-Side Row Model (SSRM) to fetch data asynchronously via a PHP API. AG Grid Blog Frontend Setup
: Load the AG Grid library via CDN and define your grid container with a fixed height. Server-Side Logic : Your PHP script (e.g., ) queries a MySQL database and returns results as JSON. Data Exchange
: The grid sends parameters (like sort order and filter criteria) to PHP, which processes the SQL and sends back only the necessary rows. Detailed Feature Review
AG Grid remains a "gold standard" for data-intensive enterprise applications due to its extensive feature set and performance. Sample Apps, Demos, Examples & Extensions - AG Grid Blog 18 Jun 2025 —
Integrating AG Grid with PHP allows you to build high-performance, enterprise-grade data tables with features like server-side pagination, sorting, and filtering. This guide provides a modern example of connecting AG Grid to a PHP/MySQL backend for a full CRUD (Create, Read, Update, Delete) experience. 1. Database and Environment Setup
Before writing code, ensure you have a local server like XAMPP running with Apache and MySQL.
Database Preparation:Create a table named products to store your grid data:
CREATE DATABASE inventory_db; USE inventory_db; CREATE TABLE products ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, category VARCHAR(100), price DECIMAL(10, 2) ); Use code with caution. 2. The Frontend: AG Grid Implementation
Use the AG Grid Community edition via CDN for a quick setup. index.html:
Use code with caution. 3. The Backend: PHP & MySQL API
Your PHP scripts will handle data retrieval and updates using JSON as the bridge.
fetch.php (Read Operation):This script retrieves data from MySQL and returns it to the grid as a JSON array.
query("SELECT * FROM products"); echo json_encode($result->fetch_all(MYSQLI_ASSOC)); ?> Use code with caution.
update.php (Update Operation):When a cell is edited in the grid, this script receives the updated row data.
prepare("UPDATE products SET name=?, category=?, price=? WHERE id=?"); $stmt->bind_param("ssdi", $data['name'], $data['category'], $data['price'], $data['id']); $stmt->execute(); ?> Use code with caution. 4. Advanced: Server-Side Row Model (SSRM)
A solid resource for integrating AG Grid with PHP is the Server-Side Row Model (SSRM) guide from the AG Grid blog. While many official examples use Node.js, this specific walkthrough covers the Laravel (PHP) and MySQL stack, which is highly applicable to modern PHP environments. Key Components of a PHP Implementation
To set up a production-ready AG Grid with a PHP backend, you typically need:
Grid Configuration: Set rowModelType: 'serverSide' in your JavaScript options. aggrid php example updated
Datasource: A getRows function in JS that POSTs the grid's request object to your PHP endpoint.
PHP Controller: Logic to parse the startRow, endRow, sortModel, and filterModel from the incoming JSON.
SQL Generation: Dynamically building LIMIT, OFFSET, ORDER BY, and WHERE clauses based on that grid request. Updated Resources & Libraries
AG Grid Laravel Adapter: For those using Laravel, the ag-grid-laravel package provides a pre-built adapter to handle the heavy lifting of server-side requests.
Latest Versions: As of April 2026, ensure you are referencing documentation for AG Grid v35.2.1 to utilize the latest Theming API and performance enhancements.
Full-Stack Reference: While not exclusively PHP, the AG Grid Github Examples repository contains the code for the Laravel/MySQL integration mentioned above.
💡 Pro Tip: Use a prepared statement or an ORM (like Eloquent) in your PHP backend when translating AG Grid's filterModel to SQL to prevent SQL injection vulnerabilities.
If you tell me your specific PHP framework (e.g., Laravel, Symfony, or Vanilla PHP), I can provide a more tailored code snippet for your backend controller.
Integrating AG Grid with PHP is a powerful way to handle large datasets with a modern, high-performance UI. Because PHP is a server-side language and AG Grid is a client-side JavaScript library, the bridge between them is typically a RESTful API that handles data fetching and updates. The Modern Architecture
In an updated stack, you move away from rendering HTML tables on the server. Instead, PHP acts as the backend engine—using a framework like Laravel or a simple Slim app—to serve JSON. AG Grid sits on the frontend, consuming that JSON. This separation allows for "Server-Side Row Model" features, where the grid only loads the data visible to the user, making it capable of handling millions of rows without crashing the browser. Data Fetching and CRUD An effective implementation involves a few key steps:
The API Endpoint: A PHP script queries your database (like MySQL) and returns the result as json_encode($data).
The Grid Configuration: On the frontend, you define columnDefs and use the fetch() API to pull from your PHP endpoint.
Updates: By using AG Grid's onCellValueChanged event, you can send an asynchronous POST or PUT request back to a PHP script to save changes to the database instantly. Security and Performance
Modern examples prioritize prepared statements (PDO) in PHP to prevent SQL injection. Additionally, with the latest AG Grid updates, you can leverage Integrated Charts and Advanced Filtering, which requires passing complex filter objects from the grid to your PHP logic to dynamically build the SQL query.
We need a PHP script that acts as an API. AG Grid sends requests via POST (especially for the Row Model or when updating data).
File: api.php
This script handles two actions:
<?php
// api.php
header("Content-Type: application/json; charset=UTF-8");
// Database Configuration
$host = 'localhost';
$db = 'test_db';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try
$pdo = new PDO($dsn, $user, $pass, $options);
catch (\PDOException $e)
echo json_encode(["error" => $e->getMessage()]);
exit;
// Determine Action based on GET param (or use POST data parsing for stricter APIs)
$action = $_GET['action'] ?? 'fetch';
// 1. HANDLE DATA FETCHING
if ($action === 'fetch')
// Basic SQL
$sql = "SELECT * FROM employees";
$where = [];
$params = [];
// --- FILTERING (Simple Implementation) ---
// AG Grid Filter Model is usually sent via POST or GET depending on config.
// Here we check for simple query params for demonstration:
if (isset($_GET['department']) && !empty($_GET['department']))
$where[] = "department = ?";
$params[] = $_GET['department'];
if (count($where) > 0)
$sql .= " WHERE " . implode(" AND ", $where);
// --- SORTING ---
// AG Grid sends sortModel: [colId: "salary", sort: "asc"]
// We simulate sorting via GET params for this example:
if (isset($_GET['sortCol']) && isset($_GET['sortDir']))
// Validate sortDir to prevent SQL injection
$dir = strtoupper($_GET['sortDir']) === 'DESC' ? 'DESC' : 'ASC';
// Whitelist columns to prevent SQL injection
$allowedCols = ['employee_name', 'salary', 'department'];
if (in_array($_GET['sortCol'], $allowedCols))
$sql .= " ORDER BY " . $_GET['sortCol'] . " " . $dir;
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$data = $stmt->fetchAll();
echo json_encode($data);
// 2. HANDLE ROW UPDATE
elseif ($action === 'update')
// Read raw POST data (JSON)
$input = json_decode(file_get_contents('php://input'), true);
if (!$input
This updated AG Grid PHP example provides a fully functional, enterprise-ready data grid with server-side sorting, filtering, pagination, and CRUD operations. The backend uses modern PHP (8.1+) with PDO, and the frontend leverages AG Grid v31’s server-side row model for optimal performance even with thousands of rows. We need a PHP script that acts as an API
Next steps: Integrate AG Grid Enterprise features like Excel export, charting, or master/detail views, and enhance PHP with input validation, logging, and rate limiting for production deployment.
File structure recap:
aggrid-php-example/
├── index.html
├── server.php
├── db.php
└── (optional) .env for credentials
Run with php -S localhost:8000 and open http://localhost:8000. Your AG Grid will communicate seamlessly with the PHP backend, handling all dynamic data operations in real time.
Title: "Unlock the Power of Interactive Tables with AG Grid PHP Example"
Introduction:
Are you tired of using boring, static tables on your website? Do you want to provide your users with a more engaging and interactive experience? Look no further than AG Grid, a powerful and feature-rich JavaScript library for creating interactive tables. In this post, we'll explore how to use AG Grid with PHP to create a dynamic and customizable table.
What is AG Grid?
AG Grid is a popular JavaScript library for creating interactive tables. It offers a wide range of features, including:
Why Use AG Grid with PHP?
While AG Grid is a JavaScript library, it can be easily integrated with PHP to create a dynamic and interactive table. By using AG Grid with PHP, you can:
AG Grid PHP Example
In this example, we'll create a simple AG Grid table using PHP and MySQL. We'll assume that you have a basic understanding of PHP and MySQL.
Step 1: Install AG Grid
To get started, download the AG Grid library from the official website. For this example, we'll use the community edition.
Step 2: Create a MySQL Database
Create a MySQL database and add a table with some sample data. For this example, we'll use a simple table called "employees" with the following columns:
| id | name | email | department | | --- | --- | --- | --- | | 1 | John Smith | john.smith@example.com | Sales | | 2 | Jane Doe | jane.doe@example.com | Marketing| | 3 | Bob Brown | bob.brown@example.com | IT |
Step 3: Create a PHP Script
Create a PHP script called "grid.php" and add the following code:
<?php
// Configuration
$dbHost = 'localhost';
$dbUsername = 'your_username';
$dbPassword = 'your_password';
$dbName = 'your_database';
// Connect to database
$conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
// Check connection
if ($conn->connect_error)
die("Connection failed: " . $conn->connect_error);
// Fetch data from database
$sql = "SELECT * FROM employees";
$result = $conn->query($sql);
// Close database connection
$conn->close();
// Convert data to JSON
$data = array();
while($row = $result->fetch_assoc())
$data[] = $row;
// Output JSON data
header('Content-Type: application/json');
echo json_encode($data);
This script connects to a MySQL database, fetches data from the "employees" table, and outputs the data in JSON format.
Step 4: Create an HTML File
Create an HTML file called "index.html" and add the following code:
<!DOCTYPE html>
<html>
<head>
<title>AG Grid PHP Example</title>
<script src="https://unpkg.com/ag-grid-community/dist/ag-grid-community.min.noStyle.js"></script>
<link rel="stylesheet" href="https://unpkg.com/ag-grid-community/dist/styles/ag-grid.css">
<link rel="stylesheet" href="https://unpkg.com/ag-grid-community/dist/styles/ag-theme-balham.css">
</head>
<body>
<div id="grid" style="height: 200px; width: 400px;" class="ag-theme-balham"></div>
<script>
// Fetch data from PHP script
fetch('grid.php')
.then(response => response.json())
.then(data =>
// Create AG Grid
const gridOptions =
columnDefs: [
field: 'name' ,
field: 'email' ,
field: 'department'
],
rowData: data
;
new agGrid.Grid(document.getElementById('grid'), gridOptions);
);
</script>
</body>
</html>
This HTML file includes the AG Grid library and creates a simple grid with three columns. It then fetches data from the "grid.php" script and passes it to the AG Grid.
Conclusion:
In this example, we've created a simple AG Grid table using PHP and MySQL. We've demonstrated how to fetch data from a database and display it in an interactive table. AG Grid offers a wide range of features and customization options, making it a powerful tool for creating dynamic and interactive tables.
I hope this helps! Let me know if you have any questions or need further clarification.
Here is an updated version with more recent information
Title: "AG Grid PHP Example: Create Interactive Tables with PHP and MySQL"
Introduction:
AG Grid is a powerful and feature-rich JavaScript library for creating interactive tables. In this post, we'll explore how to use AG Grid with PHP and MySQL to create a dynamic and customizable table.
What is AG Grid?
AG Grid is a popular JavaScript library for creating interactive tables. It offers a wide range of features, including:
Why Use AG Grid with PHP?
While AG Grid is a JavaScript library, it can be easily integrated with PHP to create a dynamic and interactive table. By using AG Grid with PHP, you can:
AG Grid PHP Example
In this example, we'll create a simple AG Grid table using PHP and MySQL. We'll assume that you have a basic understanding of PHP and MySQL. " many legacy tutorials show simple
When searching for "AG Grid PHP examples," many legacy tutorials show simple, static JSON echoes. Modern applications, however, require dynamic data handling. Below is an updated approach using vanilla PHP (no framework dependencies) to demonstrate the core logic clearly.
