Sample 2-2. Advanced PHP DG code.

    1. All modes (Add/Edit/Details/Delete/View).
    2. All features.
    3. Inline editing.
    4. AJAX autocomplete feature for filtering.
    etc.

   View LIVE DEMO on this sample   BACK to Examples Page

Show Plain Text »
  1. <?php
  2.    
  3. ## +---------------------------------------------------------------------------+
  4. ## | 1. Creating & Calling:                                                    |
  5. ## +---------------------------------------------------------------------------+
  6. ##  *** define a relative (virtual) path to datagrid.class.php file and "pear"
  7. ##  *** directory (relatively to the current file)
  8. ##  *** RELATIVE PATH ONLY ***    
  9.   define ("DATAGRID_DIR", "datagrid/");                     /* Ex.: "datagrid/" */
  10.   require_once(DATAGRID_DIR."datagrid.class.php");
  11.    
  12. ##  *** creating variables that we need for database connection
  13.   $DB_USER="username";            // usually like this: prefix_name
  14.   $DB_PASS="password";               // must be already enscrypted
  15.   $DB_HOST="localhost";           // often localhost
  16.   $DB_NAME="database_name";        // usually like this: prefix_name  
  17.  
  18. //  ob_start();
  19. ##  *** put a primary key on the first place
  20.  $sql=" SELECT
  21.        demo_presidents.id,
  22.        demo_presidents.region_id,
  23.        demo_presidents.country_id,
  24.        demo_presidents.name,
  25.        demo_presidents.birth_date,
  26.        demo_presidents.status,
  27.        demo_presidents.rating,
  28.        demo_countries.name as country_name
  29.    FROM demo_presidents
  30.        INNER JOIN demo_countries ON demo_presidents.country_id=demo_countries.id ";
  31.  
  32. ##  *** set needed options and create a new class instance
  33.   $debug_mode = false;        /* display SQL statements while processing */    
  34.   $messaging = true;          /* display system messages on a screen */
  35.   $unique_prefix = "prs_";    /* prevent overlays - must be started with a letter */
  36.   $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix);
  37. ##  *** set data source with needed options
  38.   $default_order = array("rating"=>"ASC");
  39.   $dgrid->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);            
  40.  
  41. ## +---------------------------------------------------------------------------+
  42. ## | 2. General Settings:                                                      |
  43. ## +---------------------------------------------------------------------------+
  44. ## +-- AJAX -------------------------------------------------------------------+
  45. ##  *** enable or disable using of AJAX (for sorting and paging ONLY)
  46.   $ajax_option = true;
  47.   $dgrid->AllowAjax($ajax_option);
  48. ##  *** set interface language (default - English)
  49. /// $dg_language = "en";  
  50. /// $dgrid->SetInterfaceLang($dg_language);
  51. ##  *** set direction: "ltr" or "rtr" (default - "ltr")
  52. /// $direction = "ltr";
  53. /// $dgrid->SetDirection($direction);
  54. ##  *** set layouts: "0" - tabular(horizontal) - default, "1" - columnar(vertical), "2" - customized
  55.   $layouts = array("view"=>"0", "edit"=>"0", "details"=>"1", "filter"=>"1");
  56.   $dgrid->SetLayouts($layouts);
  57. /// $details_template = "&lt;table&gt;&lt;tr&gt;&lt;td&gt;{field_name_1}&lt;/td&gt;&lt;td&gt;{field_name_2}&lt;/td&gt;&lt;/tr&gt;...&lt;/table&gt;";
  58. /// $dgrid->SetTemplates("","",$details_template);
  59. ##  *** set modes for operations ("type" => "link|button|image")
  60. ##  *** "view" - view mode | "edit" - add/edit/details modes
  61. ##  *** "byFieldValue"=>"fieldName" - make the field to be a link to edit mode page
  62. /// $modes = array(
  63. ///     "add"     =>array("view"=>true, "edit"=>false, "type"=>"link", "show_add_button"=>"inside|outside"),
  64. ///     "edit"    =>array("view"=>true, "edit"=>true,  "type"=>"link", "byFieldValue"=>""),
  65. ///     "cancel"  =>array("view"=>true, "edit"=>true,  "type"=>"link"),
  66. ///     "details" =>array("view"=>true, "edit"=>false, "type"=>"link"),
  67. ///     "delete"  =>array("view"=>true, "edit"=>true,  "type"=>"image")
  68. /// );
  69. /// $dgrid->SetModes($modes);
  70. ##  *** allow scrolling on datagrid
  71. /// $scrolling_option = false;
  72. /// $dgrid->AllowScrollingSettings($scrolling_option);  
  73. ##  *** set scrolling settings (optional)
  74. /// $scrolling_width = "90%";
  75. /// $scrolling_height = "100%";
  76. /// $dgrid->SetScrollingSettings($scrolling_width, $scrolling_height);
  77. ##  *** allow multirow operations
  78.   $multirow_option = true;
  79.   $dgrid->AllowMultirowOperations($multirow_option);
  80.   $multirow_operations = array(
  81.     "delete"  => array("view"=>true),
  82.     "details" => array("view"=>true),
  83.   );
  84.   $dgrid->SetMultirowOperations($multirow_operations);  
  85. ##  *** set CSS class for datagrid
  86. ##  *** "default" or "blue" or "gray" or "green" or "pink" or your own css file
  87. /// $css_class = "default";
  88. /// $dgrid->SetCssClass($css_class);
  89. ##  *** set variables that used to get access to the page (like: my_page.php?act=34&id=56 etc.)
  90. /// $http_get_vars = array("act", "id");
  91. /// $dgrid->SetHttpGetVars($http_get_vars);
  92. ##  *** set other datagrid/s unique prefixes (if you use few datagrids on one page)
  93. ##  *** format (in which mode to allow processing of another datagrids)
  94. ##  *** array("unique_prefix"=>array("view"=>true|false, "edit"=>true|false, "details"=>true|false));
  95. /// $anotherDatagrids = array("abcd_"=>array("view"=>true, "edit"=>true, "details"=>true));
  96. /// $dgrid->SetAnotherDatagrids($anotherDatagrids);  
  97. ##  *** set DataGrid caption
  98.   $dg_caption = "Presidents";
  99.   $dgrid->SetCaption($dg_caption);
  100.  
  101. ## +---------------------------------------------------------------------------+
  102. ## | 3. Printing & Exporting Settings:                                         |
  103. ## +---------------------------------------------------------------------------+
  104. ##  *** set printing option: true(default) or false
  105. /// $printing_option = true;
  106. /// $dgrid->AllowPrinting($printing_option);
  107. ##  *** set exporting option: true(default) or false and relative (virtual) path
  108. ##  *** to export directory (relatively to datagrid.class.php file).
  109. ##  *** Ex.: "" - if we use current datagrid folder
  110. /// $exporting_option = true;
  111. /// $exporting_directory = "";              
  112. /// $dgrid->AllowExporting($exporting_option, $exporting_directory);
  113. /// $exporting_types = array("excel"=>"true", "pdf"=>"true", "xml"=>"true");
  114. /// $dgrid->AllowExportingTypes($exporting_types);
  115.  
  116. ## +---------------------------------------------------------------------------+
  117. ## | 4. Sorting & Paging Settings:                                             |
  118. ## +---------------------------------------------------------------------------+
  119. ##  *** set sorting option: true(default) or false
  120. /// $sorting_option = true;
  121. /// $dgrid->AllowSorting($sorting_option);              
  122. ##  *** set paging option: true(default) or false
  123.   $paging_option = true;
  124.   $rows_numeration = false;
  125.   $numeration_sign = "N #";
  126.   $dropdown_paging = true;
  127.   $dgrid->AllowPaging($paging_option, $rows_numeration, $numeration_sign, $dropdown_paging);
  128. ##  *** set paging settings
  129.   $bottom_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", "page_size"=>true, "page_size_align"=>"right");
  130.   $top_paging = array();
  131.   $pages_array = array("5"=>"5", "10"=>"10", "25"=>"25", "50"=>"50", "100"=>"100", "250"=>"250", "500"=>"500", "1000"=>"1000");
  132.   $default_page_size = 10;
  133.   $paging_arrows = array("first"=>"|&lt;&lt;", "previous"=>"&lt;&lt;", "next"=>"&gt;&gt;", "last"=>"&gt;&gt;|");
  134.   $dgrid->SetPagingSettings($bottom_paging, $top_paging, $pages_array, $default_page_size, $paging_arrows);
  135.  
  136. ## +---------------------------------------------------------------------------+
  137. ## | 5. Filter Settings:                                                       |
  138. ## +---------------------------------------------------------------------------+
  139. ##  *** set filtering option: true or false(default)
  140.   $filtering_option = true;
  141.   $show_search_type = true;
  142.   $dgrid->AllowFiltering($filtering_option, $show_search_type);
  143. ##  *** set additional filtering settings
  144. ##  *** tips: use "," (comma) if you want to make search by some words, for ex.: hello, bye, hi
  145. /// $fill_from_array = array("0"=>"No", "1"=>"Yes");  /* as "value"=>"option" */
  146.   $filtering_fields = array(
  147.     "Country"=>array("type"=>"textbox", "autocomplete"=>"true", "handler"=>"autosuggest_countries.php", "maxresults"=>"12", "shownoresults"=>"true", "table"=>"demo_countries", "field"=>"name", "show_operator"=>"false", "default_operator"=>"like%", "case_sensitive"=>"false", "comparison_type"=>"string|numeric|binary", "width"=>"", "on_js_event"=>""),
  148.   );
  149.   $dgrid->SetFieldsFiltering($filtering_fields);
  150.  
  151. ## +---------------------------------------------------------------------------+
  152. ## | 6. View Mode Settings:                                                    |
  153. ## +---------------------------------------------------------------------------+
  154. ##  *** set view mode table properties
  155.   $vm_table_properties = array("width"=>"90%");
  156.   $dgrid->SetViewModeTableProperties($vm_table_properties);  
  157. ##  *** set columns in view mode
  158. ##  *** Ex.: "on_js_event"=>"onclick='alert(\"Yes!!!\");'"
  159. ##  ***      "barchart" : number format in SELECT SQL must be equal with number format in max_value
  160. /// $fill_from_array = array("0"=>"Banned", "1"=>"Active", "2"=>"Closed", "3"=>"Removed"); /* as "value"=>"option" */
  161.   $vm_colimns = array(
  162.     "name" =>array("header"=>"Name", "type"=>"label", "align"=>"left",  "wrap"=>"wrap",   "text_length"=>"20"),
  163.     "birth_date" =>array("header"=>"Birth Date", "type"=>"label", "align"=>"center",  "wrap"=>"nowrap", "text_length"=>"-1"),
  164.     "status" =>array("header"=>"Status", "type"=>"label", "align"=>"center",  "wrap"=>"nowrap", "text_length"=>"30"),
  165.     "country_name" =>array("header"=>"Country", "type"=>"label", "align"=>"center",  "wrap"=>"nowrap", "text_length"=>"30")
  166.   );
  167.   $dgrid->SetColumnsInViewMode($vm_colimns);
  168. ##  *** set auto-generated columns in view mode
  169. //  $auto_column_in_view_mode = false;
  170. //  $dgrid->SetAutoColumnsInViewMode($auto_column_in_view_mode);
  171.  
  172. ## +---------------------------------------------------------------------------+
  173. ## | 7. Add/Edit/Details Mode Settings:                                        |
  174. ## +---------------------------------------------------------------------------+
  175. ##  *** set add/edit mode table properties
  176.   $em_table_properties = array("width"=>"75%");
  177.   $dgrid->SetEditModeTableProperties($em_table_properties);
  178. ##  *** set details mode table properties
  179.   $dm_table_properties = array("width"=>"60%");
  180.   $dgrid->SetDetailsModeTableProperties($dm_table_properties);
  181. ##  ***  set settings for add/edit/details modes
  182.   $table_name  = "demo_presidents";
  183.   $primary_key = "id";
  184.   $condition   = "";
  185.   $dgrid->SetTableEdit($table_name, $primary_key, $condition);
  186. ##  *** set columns in edit mode  
  187.   $em_columns = array(
  188.      "name"       =>array("header"=>"Name",       "type"=>"textbox",  "width"=>"140px", "req_type"=>"rt", "title"=>"Name"),
  189.      "birth_date"  =>array("header"=>"Birth Date", "type"=>"date",     "req_type"=>"rt", "width"=>"80px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "calendar_type"=>"floating"),
  190.      "status"     =>array("header"=>"Status",     "type"=>"enum",     "req_type"=>"st", "width"=>"210px", "title"=>"Status", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "on_js_event"=>"", "source"=>"self", "view_type"=>"dropdownlist"),
  191.      "country_id"  =>array("header"=>"Country",    "type"=>"textbox",  "width"=>"160px", "req_type"=>"ri", "title"=>"Country", "readonly"=>true),      
  192.   );
  193.   $dgrid->SetColumnsInEditMode($em_columns);
  194. ##  *** set auto-generated columns in edit mode
  195. //  $auto_column_in_edit_mode = false;
  196. //  $dgrid->SetAutoColumnsInEditMode($auto_column_in_edit_mode);
  197. ##  *** set foreign keys for add/edit/details modes (if there are linked tables)
  198.   $foreign_keys = array(
  199.     "country_id"=>array("table"=>"demo_countries", "field_key"=>"id", "field_name"=>"name", "view_type"=>"dropdownlist", "radiobuttons_alignment"=>"horizontal|vertical", "condition"=>"", "order_by_field"=>"", "order_type"=>"ASC", "on_js_event"=>""),
  200.   );
  201.   $dgrid->SetForeignKeysEdit($foreign_keys);
  202.  
  203. ## +---------------------------------------------------------------------------+
  204. ## | 8. Bind the DataGrid:                                                     |
  205. ## +---------------------------------------------------------------------------+
  206. ##  *** bind the DataGrid and draw it on the screen
  207. ## call of this method between HTML &lg;HEAD&gt; tags
  208.   $dgrid->WriteCssClass();
  209.   $dgrid->Bind();        
  210. //  ob_end_flush();
  211.  
  212. ################################################################################  
  213.  
  214. ?>