Sample 1-2. Simplest DataGrid code.

    1. All modes (Add/Edit/Details/Delete/View).
    2. All features.
    3. Two DataGrids on one page.
Show Plain Text »
  1. <?php
  2.  
  3. ## +---------------------------------------------------------------------------+
  4. ## | 1. Creating & Calling:                                                    |
  5. ## +---------------------------------------------------------------------------+
  6. ##  *** define a relative (virtual) path to datagrid.class.php file
  7. ##  *** directory (relatively to the current file)
  8. ##  *** RELATIVE PATH ONLY ***
  9. define ("DATAGRID_DIR", "datagrid/");                    
  10. require_once(DATAGRID_DIR."datagrid.class.php");
  11.  
  12. ##  *** creating variables that we need for database connection    
  13. $DB_USER = "db_user";
  14. $DB_PASS = "db_password";      
  15. $DB_HOST = "db_host";      
  16. $DB_NAME = "db_name";      
  17.  
  18. ##  *** set needed options and create a new class instance
  19. $debug_mode = false;        /* display SQL statements while processing */    
  20. $messaging = true;          /* display system messages on a screen */
  21. $unique_prefix = "f_";      /* prevent overlays - must be started with a letter */
  22. $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix);
  23.  
  24. ##  *** put a primary key on the first place
  25. $sql=" SELECT "
  26.  ."demo_countries.id, "
  27.  ."demo_countries.region_id, "
  28.  ."demo_regions.name as region_name, "
  29.  ."demo_countries.name as country_name, "
  30.  ."demo_countries.description, "
  31.  ."demo_countries.picture_url, "
  32.  ."demo_countries.independent_date, "
  33.  ."FORMAT(demo_countries.population, 0) as population, "  
  34.  ."(SELECT COUNT(demo_presidents.id) FROM demo_presidents WHERE demo_presidents.country_id = demo_countries.id) as presidents, "
  35.  ." CASE WHEN demo_countries.is_democracy = 1 THEN 'Yes' ELSE 'No' END as is_democracy "
  36.  ."FROM demo_countries INNER JOIN demo_regions ON demo_countries.region_id=demo_regions.id ";
  37.  
  38. ##  *** set data source with needed options
  39. $default_order = array("id"=>"DESC");
  40. $dgrid->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);            
  41. $dgrid->isDemo = true;
  42.  
  43. ## +---------------------------------------------------------------------------+
  44. ## | 2. General Settings:                                                      |
  45. ## +---------------------------------------------------------------------------+
  46. ##  *** set interface language (default - English)
  47. $dgrid->SetInterfaceLang("en");
  48. ##  *** set layouts: 0 - tabular(horizontal) - default, 1 - columnar(vertical)
  49. $dgrid->SetLayouts(array("view"=>0, "edit"=>1, "filter"=>1));
  50. ##  *** set modes for operations ("type" => "link|button|image")
  51. ##  *** "byFieldValue"=>"fieldName" - make the field to be a link to edit mode page
  52. $modes = array(
  53.   "add" =>array("view"=>true, "edit"=>false, "type"=>"link"),
  54.   "edit" =>array("view"=>true, "edit"=>true,  "type"=>"link", "byFieldValue"=>""),
  55.   "details" =>array("view"=>true, "edit"=>false, "type"=>"link"),
  56.   "delete" =>array("view"=>true, "edit"=>true,  "type"=>"image")
  57. );
  58. $dgrid->SetModes($modes);
  59. ##  *** allow mulirow operations
  60. $multirow_option = true;
  61. $dgrid->AllowMultirowOperations($multirow_option);
  62. $multirow_operations = array(
  63.   "edit"  => array("view"=>false),
  64.   "delete"  => array("view"=>true),
  65.   "details" => array("view"=>true)
  66. );
  67. $dgrid->SetMultirowOperations($multirow_operations);  
  68. ##  *** set CSS class for datagrid
  69. $dgrid->SetCssClass("default");
  70. ##  *** set other datagrid/s unique prefixes (if you use few datagrids on one page)
  71. $anotherDatagrids = array("fp_"=>array("view"=>false, "edit"=>true, "details"=>true));
  72. $dgrid->SetAnotherDatagrids($anotherDatagrids);  
  73. ##  *** set DataGrid caption
  74. $dg_caption = "<b>Simple ApPHP DataGrid</b>";
  75. $dgrid->SetCaption($dg_caption);
  76.  
  77. ## +---------------------------------------------------------------------------+
  78. ## | 3. Printing & Exporting Settings:                                         |
  79. ## +---------------------------------------------------------------------------+
  80. ##  *** set printing option: true(default) or false
  81. $dgrid->AllowPrinting(true);
  82.  
  83. ## +---------------------------------------------------------------------------+
  84. ## | 4. Sorting & Paging Settings:                                             |
  85. ## +---------------------------------------------------------------------------+
  86. ##  *** set sorting option: true(default) or false
  87. $dgrid->AllowSorting(true);              
  88. ##  *** set paging option: true(default) or false
  89. $paging_option = true;
  90. $rows_numeration = false;
  91. $numeration_sign = "N #";      
  92. $dgrid->AllowPaging($paging_option, $rows_numeration, $numeration_sign);
  93. ##  *** set paging settings
  94. $bottom_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", "page_size"=>true, "page_size_align"=>"right");
  95. $top_paging = array();
  96. $pages_array = array("10"=>"10", "25"=>"25", "50"=>"50", "100"=>"100", "250"=>"250", "500"=>"500", "1000"=>"1000");
  97. $default_page_size = 10;
  98. $dgrid->SetPagingSettings($bottom_paging, $top_paging, $pages_array, $default_page_size);
  99.  
  100. ## +---------------------------------------------------------------------------+
  101. ## | 5. Filter Settings:                                                       |
  102. ## +---------------------------------------------------------------------------+
  103. ##  *** set filtering option: true or false(default)
  104. $filtering_option = true;
  105. $dgrid->AllowFiltering($filtering_option);
  106. ##  *** set aditional filtering settings
  107. $fill_from_array = array("10000"=>"10000", "250000"=>"250000", "5000000"=>"5000000", "25000000"=>"25000000", "100000000"=>"100000000");
  108. $sel_region = isset($_POST['sel_region']) ? $_POST['sel_region'] : "";
  109. $country_condition = (!empty($sel_region)) ? "region_id=".(int)$sel_region : "";      
  110. $filtering_fields = array(
  111.   "Region" => array("type"=>"enum", "view_type"=>"dropdownlist", "table"=>"demo_regions",   "field"=>"id", "field_view"=>"name", "source"=>"self", "order"=>"DESC", "default_operator"=>"=", "default"=>$sel_region, "on_js_event"=>"onchange='".$unique_prefix."_doPostBack(\"reset\", 0, \"&sel_region=\"+this.value)'"),
  112.   "Country" => array("type"=>"enum", "view_type"=>"dropdownlist", "table"=>"demo_countries", "field"=>"id", "field_view"=>"name", "source"=>"self", "order"=>"DESC", "default_operator"=>"=", "condition"=>$country_condition),
  113.   "Date" => array("type"=>"textbox", "table"=>"demo_countries", "field"=>"independent_date", "source"=>"self", "operator"=>true, "case_sensitive"=>false,  "comparison_type"=>"string"),      
  114.   "Population" => array("type"=>"enum", "view_type"=>"dropdownlist", "table"=>"demo_countries", "field"=>"population", "source"=>$fill_from_array, "order"=>"DESC", "operator"=>true, "case_sensitive"=>false, "comparison_type"=>"numeric")
  115. );
  116. $dgrid->SetFieldsFiltering($filtering_fields);
  117.  
  118. ## +---------------------------------------------------------------------------+
  119. ## | 6. View Mode Settings:                                                    |
  120. ## +---------------------------------------------------------------------------+
  121. ##  *** set columns in view mode
  122. $vm_columns = array(
  123.   "region_name" => array("header"=>"Region Name", "type"=>"label", "width"=>"130px", "align"=>"left", "wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal"),
  124.   "country_name" => array("header"=>"Country Name", "type"=>"linktoedit", "align"=>"left", "width"=>"130px", "wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal", "summarize"=>false, "on_js_event"=>""),
  125.   "population" => array("header"=>"Population", "type"=>"label", "summarize"=>true, "align"=>"right", "wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal"),
  126.   "presidents" => array("header"=>"Presidents", "type"=>"label", "summarize"=>true, "align"=>"right", "wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal"),
  127.   "description" => array("header"=>"Short Description","type"=>"label", "align"=>"left", "wrap"=>"nowrap", "text_length"=>"25", "case"=>"lower"),
  128.   "picture_url" => array("header"=>"Picture", "type"=>"image", "align"=>"center", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal", "summarize"=>false, "on_js_event"=>"", "target_path"=>"uploads/", "default"=>"", "image_width"=>"17px", "image_height"=>"17px"),
  129. );
  130. $dgrid->SetColumnsInViewMode($vm_columns);
  131.  
  132. ## +---------------------------------------------------------------------------+
  133. ## | 7. Add/Edit/Details Mode Settings:                                        |
  134. ## +---------------------------------------------------------------------------+
  135. ##  *** set add/edit mode table properties
  136. $em_table_properties = array("width"=>"70%");
  137. $dgrid->SetEditModeTableProperties($em_table_properties);
  138. ##  *** set details mode table properties
  139. $dm_table_properties = array("width"=>"70%");
  140. $dgrid->SetDetailsModeTableProperties($dm_table_properties);
  141. ##  ***  set settings for add/edit/details modes
  142. $table_name  = "demo_countries";
  143. $primary_key = "id";
  144. $condition   = "";
  145. $dgrid->SetTableEdit($table_name, $primary_key, $condition);
  146. ##  *** set columns in edit mode
  147. $fill_from_array = array("10000"=>"10000", "250000"=>"250000", "5000000"=>"5000000", "25000000"=>"25000000", "100000000"=>"100000000");
  148. $em_columns = array(
  149.   "region_id" => array("header"=>"Region", "type"=>"textbox", "width"=>"210px", "req_type"=>"rt", "title"=>"Region Name"),
  150.   "name" => array("header"=>"Country", "type"=>"textbox", "width"=>"210px", "req_type"=>"ry", "title"=>"Country Name", "unique"=>true),
  151.   "description" => array("header"=>"Short Descr.", "type"=>"textarea", "width"=>"210px", "req_type"=>"rt", "title"=>"Short Description", "edit_type"=>"wysiwyg", "rows"=>"7", "cols"=>"50"),
  152.   "population" => array("header"=>"Peoples", "type"=>"enum", "source"=>$fill_from_array, "view_type"=>"dropdownlist", "width"=>"", "req_type"=>"ri", "title"=>"Population (Peoples)"),
  153.   "picture_url" => array("header"=>"Image URL", "type"=>"image", "req_type"=>"st", "width"=>"210px", "title"=>"Picture", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "on_js_event"=>"", "target_path"=>"uploads/", "max_file_size"=>"100K", "image_width"=>"100px", "image_height"=>"100px", "file_name"=>"", "host"=>"local"),
  154.   "is_democracy" => array("header"=>"Is Democracy", "type"=>"checkbox", "true_value"=>1, "false_value"=>0, "width"=>"210px", "req_type"=>"sy", "title"=>"Is Democraty"),
  155.   "independent_date" => array("header"=>"Independence Day", "type"=>"date", "req_type"=>"rt", "width"=>"140px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "calendar_type"=>"floating"),
  156. );
  157. $dgrid->SetColumnsInEditMode($em_columns);
  158. ##  *** set foreign keys for add/edit/details modes (if there are linked tables)
  159. $foreign_keys = array(
  160.   "region_id" => array("table"=>"demo_regions", "field_key"=>"id", "field_name"=>"name", "view_type"=>"dropdownlist", "order_by_field"=>"name", "order_type"=>"ASC")
  161. );
  162. $dgrid->SetForeignKeysEdit($foreign_keys);
  163.  
  164. ## +---------------------------------------------------------------------------+
  165. ## | 8. Bind the DataGrid:                                                     |
  166. ## +---------------------------------------------------------------------------+
  167. ##  *** bind the DataGrid and draw it on the screen
  168. $dgrid->Bind();        
  169.  
  170. // if we in EDIT mode of the first datagrid
  171. if(isset($_REQUEST['f_mode']) && (($_REQUEST['f_mode'] == "edit") || ($_REQUEST['f_mode'] == "details"))){
  172.    
  173.     ## +---------------------------------------------------------------------------+
  174.    ## | 1. Creating & Calling:                                                    |
  175.    ## +---------------------------------------------------------------------------+
  176.    ##  *** put a primary key on the first place
  177.    $sql=" SELECT "
  178.      ."demo_presidents.id, "
  179.      ."demo_presidents.country_id, "
  180.      ."demo_presidents.name, "
  181.      ."demo_presidents.birth_date, "
  182.      ."demo_presidents.status "
  183.      ."FROM demo_presidents INNER JOIN demo_countries ON demo_presidents.country_id=demo_countries.id "      
  184.      ."WHERE demo_presidents.country_id = ".$dgrid->rid." ";
  185.     ##  *** set needed options and create a new class instance
  186.    $debug_mode = false;        /* display SQL statements while processing */    
  187.     $messaging = true;          /* display system messages on a screen */
  188.     $unique_prefix = "fp_";    /* prevent overlays - must be started with a letter */
  189.     $dgrid1 = new DataGrid($debug_mode, $messaging, $unique_prefix);
  190.     ##  *** set data source with needed options
  191.    $default_order = array("id"=>"DESC");
  192.     $dgrid1->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);            
  193.     $dgrid1->isDemo = true;
  194.    
  195.     ## +---------------------------------------------------------------------------+
  196.    ## | 2. General Settings:                                                      |
  197.    ## +---------------------------------------------------------------------------+
  198.    ##  *** set interface language (default - English)
  199.    $dgrid1->SetInterfaceLang($language);
  200.     ##  *** set direction: "ltr" or "rtr" (default - "ltr")
  201.    $direction = "ltr";
  202.     $dgrid1->SetDirection($direction);
  203.     ##  *** set layouts: 0 - tabular(horizontal) - default, 1 - columnar(vertical)
  204.    $layouts = array("view"=>0, "edit"=>0, "filter"=>1);
  205.     $dgrid1->SetLayouts($layouts);
  206.     ##  *** set modes for operations ("type" => "link|button|image")
  207.    ##  *** "byFieldValue"=>"fieldName" - make the field to be a link to edit mode page
  208.    if($_REQUEST["f_mode"] == "edit"){    
  209.       $modes = array(
  210.           "add" => array("view"=>true, "edit"=>false, "type"=>"link"),
  211.           "edit" => array("view"=>true, "edit"=>true, "type"=>"link", "byFieldValue"=>""),
  212.           "details" => array("view"=>false, "edit"=>false, "type"=>"link"),
  213.           "delete" => array("view"=>true, "edit"=>false, "type"=>"image")
  214.       );
  215.     }else{
  216.       $modes = array(
  217.           "add" => array("view"=>false, "edit"=>false, "type"=>"link"),
  218.           "edit" => array("view"=>false, "edit"=>false, "type"=>"link", "byFieldValue"=>""),
  219.           "details" => array("view"=>false, "edit"=>false, "type"=>"link"),
  220.           "delete" => array("view"=>false, "edit"=>false, "type"=>"image")
  221.       );
  222.     }
  223.     $dgrid1->SetModes($modes);
  224.     ##  *** set CSS class for datagrid
  225.    $dgrid1->SetCssClass("default");
  226.     ##  *** set other datagrid/s unique prefixes (if you use few datagrids on one page)
  227.    $anotherDatagrids = array("f_"=>array("view"=>true, "edit"=>true, "details"=>true));
  228.     $dgrid1->SetAnotherDatagrids($anotherDatagrids);  
  229.     ##  *** set DataGrid caption
  230.    $dgrid1->SetCaption("Presidents");
  231.    
  232.     ## +---------------------------------------------------------------------------+
  233.    ## | 3. Printing & Exporting Settings:                                         |
  234.    ## +---------------------------------------------------------------------------+
  235.    ##  *** set printing option: true(default) or false
  236.    $printing_option = false;
  237.     $dgrid1->AllowPrinting($printing_option);
  238.     ##  *** set exporting option: true(default) or false
  239.    
  240.     ## +---------------------------------------------------------------------------+
  241.    ## | 4. Sorting & Paging Settings:                                             |
  242.    ## +---------------------------------------------------------------------------+
  243.    ##  *** set sorting option: true(default) or false
  244.    $dgrid1->AllowSorting(true);              
  245.     ##  *** set paging option: true(default) or false
  246.    $paging_option = true;
  247.     $rows_numeration = false;
  248.     $numeration_sign = "N #";      
  249.     $dgrid1->AllowPaging($paging_option, $rows_numeration, $numeration_sign);
  250.     ##  *** set paging settings
  251.    $bottom_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", "page_size"=>true, "page_size_align"=>"right");
  252.     $top_paging = array();
  253.     $pages_array = array("10"=>"10", "25"=>"25", "50"=>"50", "100"=>"100", "250"=>"250", "500"=>"500", "1000"=>"1000");
  254.     $default_page_size = 10;
  255.     $dgrid1->SetPagingSettings($bottom_paging, $top_paging, $pages_array, $default_page_size);
  256.    
  257.     ## +---------------------------------------------------------------------------+
  258.    ## | 5. Filter Settings:                                                       |
  259.    ## +---------------------------------------------------------------------------+
  260.    ##  *** set filtering option: true or false(default)
  261.    $filtering_option = false;
  262.     $dgrid1->AllowFiltering($filtering_option);
  263.    
  264.     ## +---------------------------------------------------------------------------+
  265.    ## | 6. View Mode Settings:                                                    |
  266.    ## +---------------------------------------------------------------------------+
  267.    ##  *** set view mode table properties
  268.    $vm_table_properties = array("width"=>"70%");
  269.     $dgrid1->SetViewModeTableProperties($vm_table_properties);  
  270.     ##  *** set columns in view mode
  271.    $vm_columns = array(
  272.       "name"       =>array("header"=>"Name",        "type"=>"label", "align"=>"left",  "wrap"=>"wrap",   "text_length"=>"20", "case"=>"normal"),
  273.       "birth_date" =>array("header"=>"Birth Date",  "type"=>"label", "align"=>"center",  "wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal"),
  274.       "status"     =>array("header"=>"Status",      "type"=>"label", "align"=>"center",  "wrap"=>"nowrap", "text_length"=>"30", "case"=>"normal")
  275.     );
  276.     $dgrid1->SetColumnsInViewMode($vm_columns);
  277.    
  278.     ## +---------------------------------------------------------------------------+
  279.    ## | 7. Add/Edit/Details Mode Settings:                                        |
  280.    ## +---------------------------------------------------------------------------+
  281.    ##  *** set add/edit mode table properties
  282.    $em_table_properties = array("width"=>"70%");
  283.     $dgrid1->SetEditModeTableProperties($em_table_properties);
  284.     ##  *** set details mode table properties
  285.    $dm_table_properties = array("width"=>"70%");
  286.     $dgrid1->SetDetailsModeTableProperties($dm_table_properties);
  287.     ##  ***  set settings for add/edit/details modes
  288.    $table_name  = "demo_presidents";
  289.     $primary_key = "id";
  290.     $condition   = "demo_presidents.country_id = ".$dgrid->rid." ";
  291.     $dgrid1->SetTableEdit($table_name, $primary_key, $condition);
  292.     ##  *** set columns in edit mode
  293.    $em_columns = array(
  294.       "country_id" => array("header"=>"Country", "type"=>"textbox", "width"=>"160px", "req_type"=>"ri", "title"=>"Country", "readonly"=>true),      
  295.       "name" => array("header"=>"Name", "type"=>"textbox", "width"=>"140px", "req_type"=>"rt", "title"=>"Name"),
  296.       "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"),
  297.       "status" => array("header"=>"Status", "type"=>"enum", "req_type"=>"st", "width"=>"", "title"=>"Status", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "on_js_event"=>"", "source"=>"self", "view_type"=>"dropdownlist")
  298.     );
  299.     $dgrid1->SetColumnsInEditMode($em_columns);
  300.     ##  *** set foreign keys for add/edit/details modes (if there are linked tables)
  301.    $foreign_keys = array(
  302.       "country_id" => array("table"=>"demo_countries", "field_key"=>"id", "field_name"=>"name", "view_type"=>"dropdownlist", "condition"=>"")
  303.     );
  304.     $dgrid1->SetForeignKeysEdit($foreign_keys);
  305.    
  306.     ## +---------------------------------------------------------------------------+
  307.    ## | 8. Bind the DataGrid:                                                     |
  308.    ## +---------------------------------------------------------------------------+
  309.    ##  *** bind the DataGrid and draw it on the screen
  310.    $dgrid1->Bind();        
  311. }  
  312.  
  313. ?>