877-GRID-666 24/7 online support 604, Tower A, New Trade Plaza, N.T., Hong Kong

A Basic PHP Datagrid Example

A basic PHP datagrid requires only as little as TWO lines of code. First of all, always create a phpGrid object in the first line; then call its methods to define properties, and lastly always call display() to render to screen.

Please note in some databases, such as Firebird, MS Access, the fields name are case-sensitive. Make sure the name used in the code matches the case when you are using those type of databases.
  • parameter 1: SQL query
  • parameter 2: SQL primary key
  • parameter 3: SQl table name

Do NOT include WHERE clause in $sql. Instead use set_query_filter method (version 4.2+) to set query filter.

Note that the 3rd parameter is ALWAYS equals to the database table name, not alias or any arbitrary name (see below).


phpgrid constructor guide


See Live Example!

Tip: Database Naming Standards
It’s generally not recommended to have hyphen or space character in naming database tables, or you must add surrounding quotes, or back ticks to the table names or columns that have those characters in SQL query. Underscore character, on the other hand, is generally accepted for naming convention.

SELECT * FROM `my-table` -- requires ` around table name
SELECT * FROM my_table  -- does not require surrounding quotes.

Common issue:

“Fatal error: Class “C_DataGrid” not found in …”

To fix, in any new phpGrid code, start with the following:

use phpGrid\C_DataGrid;

Read KB about this fix: https://phpgrid.uservoice.com/knowledgebase/articles/896817-laravel-fatal-error-class-c-datagrid-not-found