A Basic PHP Grid Example

A basic PHP grid 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.

  • parameter 1: SQL query
  • parameter 2: SQL primary key
  • parameter 3: SQL table name

 
PHGRID CONSTRUCT

The third parameter is ALWAYS equals to the database table name (illustrated above). In addition, do not include “WHERE” clause in $sql. Instead use set_query_filter() to set query filter.
X

Complete code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
use phpCtrl\C_DataGrid;

require_once("../conf.php");      
?>
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>A Basic PHP Datagrid</title>
</head>
<body>
<?php
$dg = new C_DataGrid("SELECT * FROM orders", "orderNumber", "orders");
$dg -> display();
?>
</body>
</html>

See Live Demo!

How to obtain demo database ‘sampledb’

The demo database is available inside the folder “examples\SampleDB“. The file name suggests its associated database type. The following demo databases are included for your convenience: MySQL, MS SQL Server, DB2, SQLite, MS Access, Oracle, Postgres.

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.

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.

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

Common issue:

1
"Fatal error: Class "C_DataGrid" not found in ..."

phpGrid namespace on KB:
https://phpgrid.uservoice.com/knowledgebase/articles/896817-laravel-fatal-error-class-c-datagrid-not-found