Class: jqGrid
Source Location: /jqGrid.php
Class jqGrid
Class Overview
Located in /jqGrid.php [line 53]
Author(s):
- Tony Tomov, (tony@trirand.com)
API Tags:
Abstract: | A PHP class to work with jqGrid jQuery plugin. The main purpose of this class is to provide the data from database to jqGrid, simple subgrid and export to excel. Also can be used for search provided from toolbarFiletr and searchGrid methods How to use Note that the jqGrid object should be created in the html/php page with jsonReader set as jsonReader:{repeatitems:false}. or xmlReader set as xmlReader:{repeatitems:false}. Optional in the reader a id row can be set, but we prefer to set it in colModel using the parameter key:true Using table require_once 'jqGridPdo.php'; $dsn = "mysql:host=localhost;dbname=griddemo"; $db = new PDO($dsn, 'username', 'password'); $mygrid = new jqGrid($db); $mygrid->table = "invoices"; $mygrid->queryGrid(); Using custom SQL $mygrid = new jqGrid($db); $mygrid->SelectCommand ="SELECT * FROM invoices"; $mygrid->queryGrid(); Using xml file where the sql commands are stored $mygrid = new jqGrid($db); $mygrid->readFromXML = true $mygrid->SelectCommand ="xmlfile.getInvoiceTable"; $mygrid->queryGrid(); Using summary fields. Note that in this case in jqGrid footerrow and userDataOnFooter should be set $mygrid = new jqGrid($db); $mygrid->table = "invoices"; $mygrid->queryGrid(array("amount"=>"amount"); |
Information Tags:
Version: | 3.8.0.1 |
Copyright: | TriRand Ltd |
|
Properties
|
Methods
|
Property Summary
mixed |
$customClass |
Custom call can be used again with custom function customFunc. We can call |
function |
$customFunc |
Custom function which can be called to modify the grid output. Parameters |
string |
$dataType |
The output format for the grid. Can be json or xml |
array |
$datearray |
Store the names which are dates. The name should correspond of the name |
string |
$dbdateformat |
Date format accepted in the database. See getDbDate |
string |
$dbtimeformat |
Datetime format accepted in the database. See getDbTime |
string |
$dbtype |
This is detected automatically from the passed connection. Used to |
boolean |
$debug |
Enable disable debuging |
string |
$encoding |
Default enconding passed to the browser |
string |
$ExportCommand |
Set the sql command for excel export. If not set a _setSQL |
array |
$GridParams |
Holds the parameters that are send from the grid to the connector. |
integer |
$gSQLMaxRows |
Maximum number of rows to be exported for the excel export |
string |
$I |
Used to perform case insensitive search in PostgreSQL. The variable is |
boolean |
$jsonencode |
If set to true uses the PHP json_encode if available. If this is set to |
boolean |
$logtofile |
Determines if the log should be written to file or echoed. |
boolean |
$optimizeSearch |
Optimizes the search SQL when used in MySQL with big data sets. |
resourse |
$pdo |
Stores the connection passed to the constructor |
string |
$select |
Holds the modified select command used into grid |
string |
$SelectCommand |
In case if no table is set, this holds the sql command for |
string |
$table |
set a table to display a data to the grid |
|
$userdata |
Used to store the additional userdata which will be transported |
string |
$userdateformat |
The date format used by the user when a search is performed and CRUD operation |
string |
$usertimeformat |
The datetime format used by the user when a search is performed and CRUD operation |
boolean |
$xmlCDATA |
Defines if the xml otput should be enclosed in CDATA when xml output is enabled |
Method Summary
void |
addUserData() |
Add a custom data to the grid footer row if it is enabled. |
void |
debugout() |
Prints all executed SQL queries to file or console |
boolean |
execute() |
Executes a prepared sql statement. Also if limit is set to true is used |
string |
getDbDate() |
Return the current date format used in the undelayed database |
string |
getDbTime() |
Return the current datetime format used in the undelayed database |
array |
getGridParams() |
Return the associative array which contain the parameters that are sended from the grid to request, search, update delete data. |
mixed |
getSqlElement() |
Read a xml file and the SelectCommand and return the sql string Return string if the query is found false if not. |
string |
getUserDate() |
Return the current date format used from the client |
string |
getUserTime() |
Return the current datetime format used from the client |
string |
parseSql() |
Prepares a $sqlElement and binds a parameters $params |
mixed |
queryGrid() |
Return the result of the query to jqGrid. Support searching |
void |
querySubGrid() |
Return the result of the query for the simple subgrid |
string |
rs2excel() |
From a given recordset returns excel xml file. If the summary array is defined add summary formula at last row. |
object |
selectLimit() |
Will select, getting rows from $offset (1-based), for $nrows. |
void |
setDbDate() |
Set a new database date format using PHP convensions |
void |
setDbTime() |
Set a new database datetime format using PHP convensions |
void |
setGridParams() |
Set a grid parameters to identify the action from the grid Note that these should be set in the grid - i.e the parameters from the grid should equal to the GridParams. |
void |
setUserDate() |
Set a new user date format using PHP convensions |
void |
setUserTime() |
Set a new user datetime format using PHP convensions |
array |
_buildSearch() |
Builds the search where clause when the user perform a search Return arrray the first element is a strinng with the where clause, the second element is array containing the value parameters passed to the sql. |
object |
_getcount() |
Returns object which holds the total records in the query and optionally |
void |
_gridResponse() |
Check in which format data should be returned to the grid based on dataType property |
mixed |
_setSQL() |
Bulid the sql based on $readFromXML, $SelectCommand and $table variables |
Properties
Custom call can be used again with custom function customFunc. We can call
this using static defined functions in class customClass::customFunc - i.e $grid->customClass = Custom, $grid->customFunc = myfunc or $grid->customClass = new Custom(), $grid->customFunc = myfunc API Tags:
Custom function which can be called to modify the grid output. Parameters
passed to this function are the response object and the db connection API Tags:
The output format for the grid. Can be json or xml
API Tags:
Store the names which are dates. The name should correspond of the name
in colModel. Used to perform the conversion from userdate and dbdate API Tags:
Date format accepted in the database. See getDbDate
and setDbDate and datearray. Also this format is used to automatically convert the date for CRUD and search operations API Tags:
Datetime format accepted in the database. See getDbTime
and setDbTime and datearray. Also this format is used to automatically convert the date for CRUD and search operations API Tags:
This is detected automatically from the passed connection. Used to
construct the appropriate pagging for the database and in case of PostgreSQL to set case insensitive search API Tags:
Enable disable debuging
API Tags:
Default enconding passed to the browser
API Tags:
Set the sql command for excel export. If not set a _setSQL
function is used to set a sql for export API Tags:
Holds the parameters that are send from the grid to the connector.
Correspond to the prmNames in jqGrid Java Script lib API Tags:
Information Tags:
Todo: | these parameters should be changed according to the jqGrid js |
Maximum number of rows to be exported for the excel export
API Tags:
Used to perform case insensitive search in PostgreSQL. The variable is
detected automatically depending on the griver from jqGrid{driver}.php API Tags:
If set to true uses the PHP json_encode if available. If this is set to
false a custom encode function in jqGrid is used. Also use this to false if the encoding of your database is not utf-8 API Tags:
Deprecated: | this not needed anymore also the related option is $encoding |
Access: | public |
Determines if the log should be written to file or echoed.
Ih set to created is a file jqGrid.log in the directory where the script is API Tags:
Optimizes the search SQL when used in MySQL with big data sets.
Use this option carefully on complex SQL API Tags:
Stores the connection passed to the constructor
API Tags:
Obtain the SQL qurery from XML file.
In this case the SelectCommand should be set as xmlfile.sqlid. The xmlfile is the name of xml file where we store the sql commands, sqlid is the id of the required sql. The simple xml file can look like this < ?xml version="1.0" encoding="UTF-8"?> <queries> <sql Id="getUserById"> Select * From users Where Id = ? </sql> <sql Id="validateUser"> Select Count(Id) From users Where Email = ? AND Password = ? </sql> </queries> Important note: The class first look for readFromXML, then for selectCommand and last for a table. API Tags:
Holds the modified select command used into grid
API Tags:
In case if no table is set, this holds the sql command for
retrieving the data from the db to the grid API Tags:
Set a sql command used for the simple subgrid
API Tags:
set a table to display a data to the grid
API Tags:
Used to store the additional userdata which will be transported
to the grid when the request is made. Used in addRowData method API Tags:
The date format used by the user when a search is performed and CRUD operation
See setUserDate and getUserDate. Also this format is used to automatically convert the date passed from grid to database. Used in CRUD operations and search API Tags:
The datetime format used by the user when a search is performed and CRUD operation
See setUserTime and getUserTime. Also this format is used to automatically convert the datetime passed from grid to database. Used in CRUD operations and search API Tags:
Get te current version
API Tags:
Defines if the xml otput should be enclosed in CDATA when xml output is enabled
API Tags:
Methods
jqGrid __construct(
[resource
$db = null]
)
|
|
Constructor
Parameters:
resource |
$db: |
- $db the database connection passed to the constructor |
void addUserData(
array
$adata
)
|
|
Add a custom data to the grid footer row if it is enabled.
Also can be used to transport additional data in userdata array to be used later at client side. The syntax is $grid->addUserData(array("Name1"=>"Data1",...)); The method is executed after the sumarry rows are executed, so it can overwrite some summary data which is palced on the footer.
Parameters:
API Tags:
Prints all executed SQL queries to file or console
API Tags:
boolean execute(
string
$sqlId, array
$params,
&$sql, [boolean
$limit = false], [integer
$nrows = -1], [integer
$offset = -1], [
$order = ''], [
$sort = ''], resource
$sql
)
|
|
Executes a prepared sql statement. Also if limit is set to true is used
to return limited set of records Return true on success
Parameters:
string |
$sqlId: |
- sql to pe executed |
array |
$params: |
- array of values which are passed as parameters to the sql |
resource |
$sql: |
- pointer to the constructed sql |
boolean |
$limit: |
- if set to true we use a pagging mechanizm |
integer |
$nrows: |
- number of rows to return |
integer |
$offset: |
- the offset from which the nrows should be returned |
|
&$sql: |
|
|
$order: |
|
|
$sort: |
|
API Tags:
string exportToExcel(
[
$summary = null], [
$params = null], [
$colmodel = null], [boolean
$echo = true], [string
$filename = 'exportdata.xml']
)
|
|
Export the recordset to excel xml file.
Can use the ExportCommand. If this command is not set uses _setSQL to set the query. The number of rows exported is limited from gSQLMaxRows variable
Parameters:
array |
$summary: |
- set which columns should be sumarized in order to be displayed to the grid By default this parameter uses SQL SUM function: array("colmodelname"=>"sqlname"); It can be set to use the other one this way array("colmodelname"=>array("sqlname"=>"AVG")); By default the first field correspond to the name of colModel the second to the database name |
array |
$params: |
parameter values passed to the sql array(value) |
array |
$colmodel: |
- different description for the headers - see rs2excel |
boolean |
$echo: |
determines if the result should be returned or echoed |
string |
$filename: |
the filename to which the sheet can be saved in case if $echo is true |
API Tags:
Return the current date format used in the undelayed database
API Tags:
Return the current datetime format used in the undelayed database
API Tags:
Return the associative array which contain the parameters that are sended from the grid to request, search, update delete data.
API Tags:
mixed getSqlElement(
string
$sqlId
)
|
|
Read a xml file and the SelectCommand and return the sql string Return string if the query is found false if not.
Parameters:
string |
$sqlId: |
the string of type xmlfile.sqlId |
API Tags:
Return the current date format used from the client
API Tags:
Return the current datetime format used from the client
API Tags:
void logQuery(
string
$sql, [array
$data = null], [array
$types = null], [
$input = null], [
$fld = null], [
$primary = '']
)
|
|
Log query
Parameters:
string |
$sql: |
|
array |
$data: |
|
array |
$types: |
|
|
$input: |
|
|
$fld: |
|
|
$primary: |
|
API Tags:
string parseSql(
string
$sqlElement, array
$params, [
$bind = true]
)
|
|
Prepares a $sqlElement and binds a parameters $params
Return prepared sql statement
Parameters:
string |
$sqlElement: |
sql to be prepared |
array |
$params: |
- parameters passed to the sql |
|
$bind: |
|
API Tags:
object queryForObject(
string
$sqlId, array
$params, [boolean
$fetchAll = false]
)
|
|
Return the object from the query
Parameters:
string |
$sqlId: |
the sql to be queried |
array |
$params: |
- parameter values passed to the sql |
boolean |
$fetchAll: |
- if set to true fetch all records |
API Tags:
mixed queryGrid(
[
$summary = null], [
$params = null], [boolen
$echo = true]
)
|
|
Return the result of the query to jqGrid. Support searching
Parameters:
array |
$summary: |
- set which columns should be sumarized in order to be displayed to the grid By default this parameter uses SQL SUM function: array("colmodelname"=>"sqlname"); It can be set to use the other one this way array("colmodelname"=>array("sqlname"=>"AVG")); By default the first field correspond to the name of colModel the second to the database name |
array |
$params: |
- parameter values passed to the sql |
boolen |
$echo: |
if set to false return the records as object, otherwiese json encoded or xml string depending on the dataType variable |
API Tags:
void querySubGrid(
array
$params, [boolean
$echo = true]
)
|
|
Return the result of the query for the simple subgrid
The format depend of dataType variable
Parameters:
array |
$params: |
parameters passed to the query |
boolean |
$echo: |
if set to false return object containing the data |
API Tags:
string rs2excel(
pdo
$rs, [array
$colmodel = false], [boolean
$echo = true], [string
$filename = 'exportdata.xls'], [array
$summary = false]
)
|
|
From a given recordset returns excel xml file. If the summary array is defined add summary formula at last row.
Return well formated xml excel string
Parameters:
array |
$colmodel: |
diffrent descriptions for the headars, width, hidden cols This array is actually a colModel array in jqGrid. The array can look like Array( [0]=>Array("label"=>"Some label", "width"=>100, "hidden"=>true, "name"=>"client_id", "formatter"=>"select", editoptions=>...), [1]=>Array("label"=>"Other label", "width"=>80, "hidden"=>false, "name"=>"date",... ), ... ) |
boolean |
$echo: |
determines if the result should be send to browser or returned as string |
string |
$filename: |
filename to which file can be saved |
array |
$summary: |
- set which columns should be sumarized in order to be displayed to the grid By default this parameter uses SQL SUM function: array("colmodelname"=>"sqlname"); It can be set to use the other one this way array("colmodelname"=>array("sqlname"=>"AVG")); By default the first field correspond to the name of colModel the second to the database name |
pdo |
$rs: |
recordset $rs recordset from pdo execute command |
API Tags:
object selectLimit(
[string
$limsql = ''], [integer
$nrows = -1], [integer
$offset = -1], [
$params = null], [
$order = ''], [
$sort = '']
)
|
|
Will select, getting rows from $offset (1-based), for $nrows.
This simulates the MySQL "select * from table limit $offset,$nrows" , and the PostgreSQL "select * from table limit $nrows offset $offset". Note that MySQL and PostgreSQL parameter ordering is the opposite of the other. eg. Also supports Microsoft SQL Server SelectLimit('select * from table',3); will return rows 1 to 3 (1-based) SelectLimit('select * from table',3,2); will return rows 3 to 5 (1-based) Return object containing the limited record set
Parameters:
string |
$limsql: |
- optional sql clause |
integer |
$nrows: |
is the number of rows to get |
integer |
$offset: |
is the row to start calculations from (1-based) |
array |
$params: |
array of bind variables |
|
$order: |
|
|
$sort: |
|
API Tags:
void setDbDate(
string
$newformat
)
|
|
Set a new database date format using PHP convensions
Parameters:
string |
$newformat: |
- the new database format |
API Tags:
void setDbTime(
string
$newformat
)
|
|
Set a new database datetime format using PHP convensions
Parameters:
string |
$newformat: |
- the new database format |
API Tags:
void setGridParams(
array
$_aparams
)
|
|
Set a grid parameters to identify the action from the grid Note that these should be set in the grid - i.e the parameters from the grid should equal to the GridParams.
Parameters:
array |
$_aparams: |
set new parameter. |
API Tags:
void setUserDate(
string
$newformat
)
|
|
Set a new user date format using PHP convensions
Parameters:
string |
$newformat: |
- the new format |
API Tags:
void setUserTime(
string
$newformat
)
|
|
Set a new user datetime format using PHP convensions
Parameters:
string |
$newformat: |
- the new format |
API Tags:
array _buildSearch(
string
$sqlEx, [
$prm = null]
)
|
|
Builds the search where clause when the user perform a search Return arrray the first element is a strinng with the where clause, the second element is array containing the value parameters passed to the sql.
Parameters:
string |
$sqlEx: |
- the sql on which a serch should be applied |
array |
$prm: |
- parameters passed to the sql |
API Tags:
object _getcount(
string
$sql, [
$params = null], [
$sumcols = null]
)
|
|
Returns object which holds the total records in the query and optionally
the sum of the records determined in sumcols
Parameters:
string |
$sql: |
- string to be parsed |
array |
$params: |
- parameters passed to the sql query |
array |
$sumcols: |
- array which holds the sum of the setted rows. The array should be associative where the index corresponds to the names of colModel in the grid, and the value correspond to the actual name in the query |
API Tags:
void _gridResponse(
string
$response
)
|
|
Check in which format data should be returned to the grid based on dataType property
Add the appropriate headers and echo the result
Parameters:
string |
$response: |
can be xml or json |
API Tags:
Bulid the sql based on $readFromXML, $SelectCommand and $table variables
The logic is: first we look if readFromXML is set to true, then we look for SelectCommand and at end if none of these we use the table varable Return string or false if the sql found
API Tags:
|
|