dbGrid Extreme is my first attempt at a PHP Class! My aim was to create an object that would take an SQL Statement and turn it into a well formed and fully functional data grid.
There were many such classes on the PHP Classes site, but none that had all the functionality that I required, and none that I was able to extend easily (though this may have been my inexperience with classes).
Of course since I started this some weeks ago it has grown into a beast that I am finding hard to tame. and there are some shortfalls which I shall list here:
the Grid can be called in one line:
$objGrid1 = new dbGrid($GridName,$Connection_object,$database,$SQL,debug(true or false));
So for example:
$hostname = "localhost"; $username = "root"; $password = ""; $Conn = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR); $GridName = 'MyGrid2'; $database = "test"; $GridSQL = 'SELECT newsitems.id, category.name,author.short_name,title, body, status, posted'; $GridSQL .= ' FROM newsitems, category, author WHERE newsitems.category = category.id' AND newsitems.Author=Author.id'; $objGrid1 = new dbGrid($GridName,$Conn,$database,$GridSQL,false); // Instatiate Object
May display something like this:
| # | Action | Name | Short Name | Title | Body | Status | Posted ![]() |
|---|---|---|---|---|---|---|---|
| 1 | Edit | Delete | View | General | Sarah | Plaza Plaza | Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur sapien.......more | 1 | 2006-03-06 |
| 2 | Edit | Delete | View | General | Red | A boring title | Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur sapien.......more | 1 | 2006-03-05 |
| 3 | Edit | Delete | View | Martial Arts | Jet | anotherStupid title | Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur sapien.......more | 1 | 2006-03-04 |
| 4 | Edit | Delete | View | General | Sarah | My mth title | Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur sapien.......more | 1 | 2006-03-03 |
| 5 | Edit | Delete | View | Books | Red | Running out of titles 2 | Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur sapien.......more | 1 | 2006-03-03 |
The preferred method to call this object is to instantiate the object, set certain parameters, and then execute it's main function (called incredibly 'execute')
as in the following code:
$hostname = "localhost";
$username = "root";
$password = "";
$Conn = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);
$GridName = 'MyGrid1';
$database = "test";
$GridSQL = 'SELECT *';
$GridSQL .= ' FROM author';
$GridSQL .= ' ORDER BY short_name ';
$objGrid1 = new dbGrid();
$objGrid1->setMyName($GridName); // Set the Name
$objGrid1->setConn($Conn); // Pass the connection
$objGrid1->setDb($database); // Pass the database name
$objGrid1->setSQLMain($GridSQL); // Pass the SQL Statement
## Optional Parameters
$objGrid1->setMainDbTable('author'); // Main Table to be used for Insert /Edit - DEFAULTS to first table in SQL Statement
$objGrid1->setFieldNameList('ID, Author, Full Name');// Defaults to none - Column Headings
$objGrid1->setTitle('Authors'); // Defaults to MainDbTable -Title - Above the GridSQL
$objGrid1->setChildren('id=newsitems.author'); // Defaults to none - For childGrids syntax: '=.'
$objGrid1->setActionSeeChildrenText('News Items'); // Defaults to 'Display ' - this is the text display in the action column for viewing 'children'. If not set the text will be 'Display '
$Validation ="'short_name','#q','0',Field \'short_name\' is required'|'long_name','#q','0','Field \'long_name\' is required.'";
$objGrid1->setValidation($Validation); // the Validation for update/insert See here for explanation of how to use this string;
$objGrid1->setRowsPerPage(2); // Defaults to 20 - How many Rows per page (invalid if HasPaging is false)
$objGrid1->setBlobNumWords(12); // Defaults to 25 - How many words to display whern a clumn is 'Blob' Data type
$objGrid1->setstrDateFormat('d-m-Y H:i'); // Defaults to 'Y-m-d' - this affects the display of datatime or Date data - but insert & update forms still show the YYY-MM-DD format required by mysql
// See here for explanation of how to use dateformats(from the mysql manual);
The Above code would display something like this:
| Property | Code to Set | Notes |
|---|---|---|
| $Conn | $objGrid1->setConn($Conn); | Pass the connection |
| $Db | $objGrid1->setDb($database); | Pass the database name |
| $SQLMain | $objGrid1->setSQLMain($GridSQL); | Pass the SQL Statement |
| Property | Code to Set | Notes |
|---|---|---|
| HasPaging | $objGrid1->setHasPaging(boolean); | Defaults to true - Has data set split into pages |
| RowsPerPage | $objGrid1->setRowsPerPage(integer); | Defaults to 20- How many Rows per page (invalid if HasPaging is false) |
| ShowPageNums | $objGrid1->setShowPageNums(boolean); | Defaults to true - Shows a box with Page Number Links |
| ShowNavigation | $objGrid1->setShowNavigation(boolean); | Defaults to true - Shows NavigationLinks (First Previous Next Last) |
| ShowRecordInfo | $objGrid1->setShowRecordInfo(boolean); | Defaults to true - Shows Page information E.g. 'Records 1 to 5 of 12' |
| ShowGoToPageBox | $objGrid1->setShowGoToPageBox(boolean); | Defaults to true - Shows an input box to put a page number in |
| PrimaryKey | $objGrid1->setPrimaryKey('ID'); | Defaults to none- If blank, the grid will try to identify the PK |
| ShowPrimaryKey | $objGrid1->setShowPrimaryKey(false); | Defaults to false- Show the Primary Key Column? |
| ShowRowCounter | $objGrid1->setShowRowCounter(true); | Defaults to true - Will Show a column with the row number |
| ColumnSort | $objGrid1->setColumnSort(true); | Defaults to true - Turns columns headings into sort links |
| ShowSortBox | $objGrid1->setShowSortBox(true); | Defaults to true - Allows a user to input his own order By clause |
| AlternateRowColors | $objGrid1->setAlternateRowColors(true); | Defaults to true - Alternate Row colouring |
| headerwrap | $objGrid1->setheaderwrap(false); | Defaults to false- Allow wrapping of Header? |
| datawrap | $objGrid1->setdatawrap(false); | Defaults to false- Allow wrapping of non blob data? |
| DefaultStyleClass | $objGrid1->setDefaultStyleClass(true); | Defaults to true - If false, must be set after ->MyName |
| ActionCol | $objGrid1->setActionCol(boolean); | Defaults to true - Shows a Column at the left for each record to Edit/View/Delete/See Children. Of course it is dependent on the individual Settings for each 'Action' and on a unique key being provided |
| ActionInsert | $objGrid1->setActionInsert(boolean); | Defaults to true - If ActionCol then will allow the 'Insert' Link |
| ActionView | $objGrid1->setActionView(boolean); | Defaults to true - If ActionCol AND Unique/Primary Key is found/Provided, then will allow the 'View' Link |
| ActionDelete | $objGrid1->setActionDelete(boolean); | Defaults to true - If ActionCol AND Unique/Primary Key is found/Provided, then will allow the 'Delete' Link |
| ActionEdit | $objGrid1->setActionEdit(boolean); | Defaults to true - If ActionCol AND Unique/Primary Key is found/Provided, then will allow the 'Edit' Link |
| ActionSeeChildren | $objGrid1->setActionSeeChildren(boolean); | Defaults to FALSE - If ActionCol AND Child Information is provided then will allow the 'See Children' Link |
| ActionSeeChildrenText | $objGrid1->setActionSeeChildrenText(string); | Defaults to 'Display child_tablename' - this is the text display in the See Children' Link |
| Children | $objGrid1->setChildren(string); | Defaults to unset - if set in the format 'keyfield=child_table.child_table_field' it will use thekeyfield to send to the 'SeeChildren' QueryString Variable. The child_table.child_table_field are only used for cosmetics, but at must be present in the string. THERE CAN BE ONLY 1 CHILD |
| ShowChildrenNoSelection | $objGrid1->setShowChildrenNoSelection(boolean); | Defaults to false - When there is a child table, Should I show this table unfiltered if a selection on the parent (me) has not been made? |
| EditPage | $objGrid1->setEditPage(string_url); | Defaults to basename($_SERVER['PHP_SELF']) - Set the page to which any Edit/View/Delete/Insert Commands should be sent |
| DeletePage | $objGrid1->setDeletePage(string_url); | Defaults to basename($_SERVER['PHP_SELF']) - Set the page to which any Edit/View/Delete/Insert Commands should be sent |
| ViewPage | $objGrid1->setViewPage(string_url); | Defaults to basename($_SERVER['PHP_SELF']) - Set the page to which any Edit/View/Delete/Insert Commands should be sent |
| InsertPage | $objGrid1->setInsertPage(string_url); | Defaults to basename($_SERVER['PHP_SELF']) - Set the page to which any Edit/View/Delete/Insert Commands should be sent |
| Validation | $objGrid1->setValidation(string); | the Validation for update/insert. See here for explanation of how to use this string; |
| BlobNumWords | $objGrid1->setBlobNumWords($BlobNumWords); | Defaults to 25- How many words to display whern a clumn is 'Blob' Data type |
| strDateFormat | $objGrid1->setstrDateFormat($DateFormat); | Defaults to 'Y-m-d'- this affects the display of datatime or Date data - but insert & update forms still show the YYY-MM-DD format required by mysql. See here for explanation of how to use dateformats(from the mysql manual); |
| DateDefaultsToNow | $objGrid1->setDateDefaultsToNow(true); | Defaults to true- Specify if the datetime column (if any ) defaults to 'Now()' when inserting a record |
| ParentLink | $objGrid1->setParentLink(string); | Defaults to unset - controls the use of Dropdows in Insert/Edit: 'Must be in the format 'field=lookuptable.field_to_display' So to show drop-downs for an author(authorname) showing shortname: 'authorname=authors.shortname' N.B. This will ALways assume that the 'lookup' table will contain a primary key and that this primary key is the data to insert into the talbe. If no primary key is found it will fall back to inserting the actual shortname into the field. THERE CAN BE MANY PARENTS LISTED FOR DROPDOWNS |
| ParentGrid | $objGrid2->setParentGrid(string); | Defaults to unset - if passed the name of a previous grid, will attempt to find a 'GridNameSeeChildren' variable in the qureystring, and limit itself via that varable to a subset showing only those records hat are children of the selected record in that previous grid. THERE CAN BE ONLY 1 LINKED GRID |
| Parents | $objGrid1->setParents(string); | Defaults to unset - Links to a previous Grid selecting the 'field_linked''Must be in the format 'table.field_linked=field_to_display'So if we used 'author.id=shortname' the grid would limit by 'WHERE author.id = variable passed from parent grid' and if 'ShowsFilter' is true, would display the shortname in the title. THERE CAN BE ONLY 1 PARENTLINK |
| ShowsFilter | $objGrid2->setShowsFilter(boolean); | Defaults to false - Shows the Where Statement as a H3 Title |
| MainDbTable | $objGrid1->setMainDbTable(string); | DEFAULTS to first table in SQL Statement - Main Table to be used for Insert /Edit |
| FieldNameList | $objGrid1->setFieldNameList(string); | Defaults to none - Column Headings to be displayed on Grid. N.B. At present does NOT affect the Insert/Update forms |
| Title | $objGrid1->setTitle(string); | Defaults to MainDbTable-Title - Above the GridSQL |
| bDebug | $objGrid1->setbDebug(false); | Defaults to false - Set Debug Mode on/Off |
The validation user the validation javascript from Yaromat
My rules state that:
| TEXTBOXES | |
| '#q','0' | Required - anything |
| 'x_y','1' | Not required but if given must be number from x to y |
| '#x_y','1' | Required and must be number from x to y |
| 'S', '2' | Not required but if given must be email |
| '#S', '2' | Required and must be email |
| 'element2','6' | these must be the same as element 2 must be the same value |
| '^\([0-9][0-9]\)\\.\([0-9][0-9]\)\\.\([0-9]{4}\)$#1#2#3','3' | Not Required but if given must be date (mm.dd.yyyy) |
| '^\([0-9][0-9]\)\/\([0-9][0-9]\)\/\([0-9]{4}\)$#2#1#3','3' | Not Required but if given must be date (mm/dd/yyyy) |
| '#^\([0-9][0-9]\)\\.\([0-9][0-9]\)\\.\([0-9]{4}\)$#1#2#3','3' | Required and must be date (mm.dd.yyyy) |
| '#^\([0-9][0-9]\)\/\([0-9][0-9]\)\/\([0-9]{4}\)$#2#1#3','3' | Required and must be date (mm/dd/yyyy) |
| '^\(0[0-2]|1[0-2]|0?[0-9]\)\:\([0-5][0-9]\)$','4' | Not Required but if given must be time (12hr) |
| '#^\(0[0-9]|1[0-9]|2[0-3]\)\:\([0-5][0-9]\)$','4' | Required and must be time (24hr) |
| '#^\(0[0-2]|1[0-2]|0?[0-9]\)\:\([0-5][0-9]\)\(p|a\)m$','4' | Required and must be time (12hr with am/pm) |
| TEXTBOXES | |
| '71','1' | Required - and must be 71 characters |
| Dropdowns & Listboxes: | |
| '#q','1' | Required - must not be the firlst entry |
| radio buttons | |
| 'rb1[0]','textbox2','2' | either rb or text box must be filled |
| 'rb1[0]','textbox2','1' | both must be filled |
So, before the above strings you would put the name of the element (quoted) and after the above strings you would put the error message to dsiplay
For example:
'USB','Portnum','2','If you check the checkbox \'USB\' then fill in the port field too.'This forces the user to fill in the portnum only if the USB Check box is Checked
'Portnum','1200_1500','1','Port Number must be between 1200 and 1500'Notice that portnum is not required
another example:
'email_address', '#S', '2', 'Email Address Must be given and must be valid'
These 3 examples above should be put together in a string and separated with '|' giving:
$validation_str=" 'USB','Portnum','2','Please fill in the port field too.'|'Portnum','12_15','1','Port # must be 12 to 15'|'email_address', '#S', '2', 'Valid Email Address Required'";DATE FORMATTING
| format character | Description | Example returned values |
|---|---|---|
| a | Lowercase Ante meridiem and Post meridiem | am or pm |
| A | Uppercase Ante meridiem and Post meridiem | AM or PM |
| B | Swatch Internet time | 000 through 999 |
| d | Day of the month, 2 digits with leading zeros | 01 to 31 |
| D | A textual representation of a day, three letters | Mon through Sun |
| F | A full textual representation of a month, such as January or March | January through December |
| g | 12-hour format of an hour without leading zeros | 1 through 12 |
| G | 24-hour format of an hour without leading zeros | 0 through 23 |
| h | 12-hour format of an hour with leading zeros | 01 through 12 |
| H | 24-hour format of an hour with leading zeros | 00 through 23 |
| i | Minutes with leading zeros | 00 to 59 |
| I (capital i) | Whether or not the date is in daylights savings time | 1 if Daylight Savings Time, 0 otherwise. |
| j | Day of the month without leading zeros | 1 to 31 |
| l (lowercase 'L') | A full textual representation of the day of the week | Sunday through Saturday |
| L | Whether it's a leap year | 1 if it is a leap year, 0 otherwise. |
| m | Numeric representation of a month, with leading zeros | 01 through 12 |
| M | A short textual representation of a month, three letters | Jan through Dec |
| n | Numeric representation of a month, without leading zeros | 1 through 12 |
| O | Difference to Greenwich time (GMT) in hours | Example: +0200 |
| r | RFC 822 formatted date | Example: Thu, 21 Dec 2000 16:01:07 +0200 |
| s | Seconds, with leading zeros | 00 through 59 |
| S | English ordinal suffix for the day of the month, 2 characters | st, nd, rd or th. Works well with j |
| t | Number of days in the given month | 28 through 31 |
| T | Timezone setting of this machine | Examples: EST, MDT ... |
| U | Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) | See also time() |
| w | Numeric representation of the day of the week | 0 (for Sunday) through 6 (for Saturday) |
| W | ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) | Example: 42 (the 42nd week in the year) |
| Y | A full numeric representation of a year, 4 digits | Examples: 1999 or 2003 |
| y | A two digit representation of a year | Examples: 99 or 03 |
| z | The day of the year | 0 through 366 |
| Z | Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. | - 43200 through 43200 |
Unrecognized characters in the format string will be printed as-is.
You can prevent a recognized character in the format string from being expanded by escaping it with a preceding backslash. If the character with a backslash is already a special sequence, you may need to also escape the backslash.