Aggrid Php Example Updated

CREATE DATABASE IF NOT EXISTS company_db; USE company_db; CREATE TABLE IF NOT EXISTS employees ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, role VARCHAR(100) NOT NULL, department VARCHAR(100) NOT NULL, salary INT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; INSERT INTO employees (name, role, department, salary) VALUES ('Alice Smith', 'Senior Developer', 'Engineering', 95000), ('Bob Jones', 'UI Designer', 'Design', 72000), ('Charlie Brown', 'Product Manager', 'Product', 88000), ('Diana Prince', 'QA Engineer', 'Engineering', 68000), ('Evan Wright', 'Data Analyst', 'Analytics', 75000); Use code with caution. 2. The PHP Backend ( data.php )

: If expanding this pattern to include data writing or CRUD updates, always filter inputs before pushing updates back to MySQL.

We load the latest major version of AG Grid via a reliable Content Delivery Network (CDN) and set up a target wrapper div where the grid will initialize. Use code with caution. 3. The Application Logic: app.js aggrid php example updated

$stmt = $this->get('db')->prepare("$sql LIMIT :offset, :limit"); $stmt->bindValue(':offset', $startRow, PDO::PARAM_INT); $stmt->bindValue(':limit', $limit, PDO::PARAM_INT); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Fetch the data $data = []; while ($row = $result->fetch_assoc()) $data[] = $row; CREATE DATABASE IF NOT EXISTS company_db; USE company_db;

// Apply filters foreach ($filterModel as $field => $filter) if ($filter['filterType'] === 'text') $sql .= " AND $field LIKE :$field"; $params[":$field"] = '%' . $filter['filter'] . '%';

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(['success' => false, 'error' => 'Database connection failed']); exit; // 2. Read Request Payload $input = json_decode(file_get_contents('php://input'), true); $startRow = isset($input['startRow']) ? (int)$input['startRow'] : 0; $endRow = isset($input['endRow']) ? (int)$input['endRow'] : 20; $sortModel = isset($input['sortModel']) ? $input['sortModel'] : []; $filterModel = isset($input['filterModel']) ? $input['filterModel'] : []; $limit = $endRow - $startRow; // 3. Build Base Queries $whereClauses = []; $bindings = []; // Parse AG Grid text filters securely foreach ($filterModel as $column => $filterData) // Only allow alphanumeric and underscore column names for security if (!preg_match('/^[a-zA-Z0-9_]+$/', $column)) continue; if ($filterData['filterType'] === 'text') if ($filterData['type'] === 'contains') $whereClauses[] = "`$column` LIKE :filter_$column"; $bindings["filter_$column"] = '%' . $filterData['filter'] . '%'; elseif ($filterData['filterType'] === 'number') if ($filterData['type'] === 'equals') $whereClauses[] = "`$column` = :filter_$column"; $bindings["filter_$column"] = $filterData['filter']; $whereSql = ''; if (count($whereClauses) > 0) $whereSql = ' WHERE ' . implode(' AND ', $whereClauses); // 4. Determine Dynamic Sorting Structure Safely $orderSql = ''; if (!empty($sortModel)) $sortParts = []; foreach ($sortModel as $sort) $col = $sort['colId']; $dir = strtoupper($sort['sort']) === 'DESC' ? 'DESC' : 'ASC'; if (preg_match('/^[a-zA-Z0-9_]+$/', $col)) $sortParts[] = "`$col` $dir"; if (!empty($sortParts)) $orderSql = ' ORDER BY ' . implode(', ', $sortParts); // 5. Query the Total Matching Row Count $countQuery = "SELECT COUNT(*) FROM employees" . $whereSql; $stmtCount = $pdo->prepare($countQuery); $stmtCount->execute($bindings); $totalRows = (int)$stmtCount->fetchColumn(); // 6. Query the Specific Data Window (Pagination) $dataQuery = "SELECT id, name, role, department, salary FROM employees" . $whereSql . $orderSql . " LIMIT :limit OFFSET :offset"; $stmtData = $pdo->prepare($dataQuery); // Bind limit and offset as integers explicitly $stmtData->bindValue(':limit', $limit, PDO::PARAM_INT); $stmtData->bindValue(':offset', $startRow, PDO::PARAM_INT); // Bind filtering params foreach ($bindings as $key => $value) $stmtData->bindValue(':' . $key, $value); $stmtData->execute(); $rows = $stmtData->fetchAll(); // 7. Output Final Combined Results echo json_encode([ 'success' => true, 'rows' => $rows, 'totalRows' => $totalRows ]); Use code with caution. Essential Optimizations for Large Datasets We load the latest major version of AG

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 —