Sample 2-3. Advanced PHP DG code.

    1. All modes (Add/Edit/Details/Delete/View).
    2. All features.
    3. One DataGrid splited on 2 parts with different styles.

   View LIVE DEMO on this sample   BACK to Examples Page

Show Plain Text »
  1. <?php
  2.    
  3.   $pr_rid = isset($_GET["pr_rid"]) ? $_GET["pr_rid"] : "";
  4.   $pr_mode = isset($_GET["pr_mode"]) ? $_GET["pr_mode"] : "";
  5.  
  6.   if(($pr_mode == "edit") || ($pr_mode == "details") || ($pr_mode == "add") || ($pr_mode == "cancel")){
  7.     $_REQUEST['pr_mode'] = "view";
  8.     $_REQUEST['pr_rid'] = "";
  9.     if(($pr_mode == "cancel") && ($pr_rid == "-1")){
  10.       $_REQUEST['pr_page_size'] = "";      
  11.     }
  12.   }
  13.  
  14. ################################################################################
  15. ## +---------------------------------------------------------------------------+
  16. ## | 1. Creating & Calling:                                                    |
  17. ## +---------------------------------------------------------------------------+
  18. ##  *** define a relative (virtual) path to datagrid.class.php file and "pear"
  19. ##  *** directory (relatively to the current file)
  20. ##  *** RELATIVE PATH ONLY ***
  21.  define ("DATAGRID_DIR", "datagrid/");                     /* Ex.: "datagrid/" */
  22.  require_once(DATAGRID_DIR."datagrid.class.php");
  23.  
  24. ##  *** creating variables that we need for database connection
  25.  $DB_USER="username";            // usually like this: prefix_name
  26.  $DB_PASS="password";            // must be already enscrypted
  27.  $DB_HOST="localhost";           // often localhost
  28.  $DB_NAME="database_name";       // usually like this: prefix_name  
  29.  
  30.  ob_start();
  31.  
  32. ##  *** put a primary key on the first place
  33.  $sql = "
  34.   SELECT
  35.      demo_presidents.id,
  36.      demo_presidents.country_id,
  37.      demo_presidents.name,
  38.      demo_presidents.birth_date,
  39.      demo_presidents.status,
  40.      demo_countries.name as country_name,
  41.      'Edit' as lnk_edit,
  42.      'Details' as lnk_details      
  43.   FROM demo_presidents
  44.   INNER JOIN demo_countries ON demo_presidents.country_id=demo_countries.id";
  45.  
  46. ##  *** set needed options and create a new class instance
  47.  $debug_mode = false;        /* display SQL statements while processing */    
  48.  $messaging = true;          /* display system messages on a screen */
  49.  $unique_prefix = "pr_";    /* prevent overlays - must be started with a letter */
  50.  $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix);
  51. ##  *** set data source with needed options
  52.  $default_order = array("id"=>"ASC");
  53.  $dgrid->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);            
  54.  
  55. ## +---------------------------------------------------------------------------+
  56. ## | 2. General Settings:                                                      |
  57. ## +---------------------------------------------------------------------------+
  58.  $modes = array(
  59.     "add"        =>array("view"=>false, "edit"=>false, "type"=>"link"),
  60.     "edit"       =>array("view"=>false, "edit"=>true,  "type"=>"link", "byFieldValue"=>""),
  61.     "cancel"  =>array("view"=>true, "edit"=>true,  "type"=>"link"),
  62.     "details" =>array("view"=>false, "edit"=>false, "type"=>"link"),
  63.     "delete"  =>array("view"=>true, "edit"=>true,  "type"=>"image")
  64.  );
  65.  $dgrid->SetModes($modes);
  66. ##  *** allow mulirow operations
  67.  $multirow_option = false;
  68.  $dgrid->AllowMultirowOperations($multirow_option);
  69. ##  *** set DataGrid caption
  70.  $dg_caption = "Presidents";
  71.  $dgrid->SetCaption($dg_caption);
  72.  
  73. ## +---------------------------------------------------------------------------+
  74. ## | 3. Printing & Exporting Settings:                                         |
  75. ## +---------------------------------------------------------------------------+
  76. ##  *** set printing option: true(default) or false
  77.  $printing_option = false;
  78.  $dgrid->AllowPrinting($printing_option);
  79.  
  80. ## +---------------------------------------------------------------------------+
  81. ## | 4. Sorting & Paging Settings:                                             |
  82. ## +---------------------------------------------------------------------------+
  83. ##  *** set paging option: true(default) or false
  84.  $paging_option = true;
  85.  $rows_numeration = false;
  86.  $numeration_sign = "N #";      
  87.  $dgrid->AllowPaging($paging_option, $rows_numeration, $numeration_sign);
  88. ##  *** set paging settings
  89.  $bottom_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", "page_size"=>true, "page_size_align"=>"right");
  90.  $top_paging = array();
  91.  $pages_array = array("10"=>"10", "25"=>"25", "50"=>"50", "100"=>"100", "250"=>"250", "500"=>"500", "1000"=>"1000");
  92.  $default_page_size = 10;
  93.  $dgrid->SetPagingSettings($bottom_paging, $top_paging, $pages_array, $default_page_size);
  94.  
  95. ## +---------------------------------------------------------------------------+
  96. ## | 5. Filter Settings:                                                       |
  97. ## +---------------------------------------------------------------------------+
  98. ##  *** set filtering option: true or false(default)
  99.  $filtering_option = true;
  100.  $show_search_type = false;
  101.  $dgrid->AllowFiltering($filtering_option, $show_search_type);
  102. ##  *** set aditional filtering settings
  103.  $filtering_fields = array(
  104.     "Name"=>array("table"=>"demo_presidents", "field"=>"name", "source"=>"self", "show_operator"=>false, "default_operator"=>"like%", "order"=>"ASC", "type"=>"textbox", "case_sensitive"=>false, "comparison_type"=>"string"),
  105.  );
  106.  $dgrid->SetFieldsFiltering($filtering_fields);
  107.  
  108. ## +---------------------------------------------------------------------------+
  109. ## | 6. View Mode Settings:                                                    |
  110. ## +---------------------------------------------------------------------------+
  111. ##  *** set view mode table properties
  112.  $vm_table_properties = array("width"=>"95%");
  113.  $dgrid->SetViewModeTableProperties($vm_table_properties);  
  114. ##  *** set columns in view mode
  115. ##  *** Ex.: "on_js_event"=>"onclick='alert(\"Yes!!!\");'"
  116. ##  ***      "barchart" : number format in SELECT SQL must be equal with number format in max_value
  117.  $vm_colimns = array(
  118.      "name"=>array("header"=>"Name", "type"=>"label",      "align"=>"left", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>false, "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  119.      "m_birth_date"=>array("header"=>"Date of Birth", "type"=>"label",      "align"=>"left", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>false, "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  120.      "status"=>array("header"=>"Status", "type"=>"label",      "align"=>"left", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>false, "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  121.      "country_name"=>array("header"=>"Country", "type"=>"label",      "align"=>"left", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>false, "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  122.      "lnk_edit"=>array("header"=>" ", "type"=>"link",       "align"=>"center", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>false, "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "field_key"=>"id", "field_data"=>"lnk_edit", "rel"=>"", "title"=>"", "target"=>"", "href"=>"javascript:pr__doPostBack('edit','{0}','');"),
  123.      "lnk_details"=>array("header"=>" ", "type"=>"link",       "align"=>"center", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>false, "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "field_key"=>"id", "field_data"=>"lnk_details", "rel"=>"", "title"=>"", "target"=>"", "href"=>"javascript:pr__doPostBack('details','{0}','');"),
  124.  );
  125.  $dgrid->SetColumnsInViewMode($vm_colimns);
  126.  
  127.  
  128. ## +---------------------------------------------------------------------------+
  129. ## | 7. Add/Edit/Details Mode Settings:                                        |
  130. ## +---------------------------------------------------------------------------+
  131. ##  *** set add/edit mode table properties
  132.  $em_table_properties = array("width"=>"95%");
  133.  $dgrid->SetEditModeTableProperties($em_table_properties);
  134. ##  *** set details mode table properties
  135.  $dm_table_properties = array("width"=>"95%");
  136.  $dgrid->SetDetailsModeTableProperties($dm_table_properties);
  137. ##  ***  set settings for add/edit/details modes
  138.   $table_name  = "demo_presidents";
  139.   $primary_key = "id";
  140.   $condition   = "";    //table_name.field = ".$_REQUEST['abc_rid'];
  141.   $dgrid->SetTableEdit($table_name, $primary_key, $condition);
  142. ##  *** set columns in edit mode
  143.  $em_columns = array(
  144.     "name"        =>array("header"=>"Name", "type"=>"textbox",  "align"=>"left", "req_type"=>"rt", "width"=>"120px", "title"=>"Name", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
  145.     "country_id"  =>array("header"=>"Country", "type"=>"textbox", "align"=>"left",  "req_type"=>"rt", "width"=>"120px", "title"=>"Country", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
  146.     "birth_date"  =>array("header"=>"Birth Date", "type"=>"date",  "align"=>"left",    "req_type"=>"rt", "width"=>"120px", "title"=>"Date of Birth", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "calendar_type"=>"floating"),
  147.     "status"      =>array("header"=>"Status", "type"=>"enum",  "align"=>"left",    "req_type"=>"st", "width"=>"120px", "title"=>"Status", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "source"=>"self", "view_type"=>"dropdownlist", "multiple"=>false, "multiple_size"=>"1"),
  148.  );
  149.  $dgrid->SetColumnsInEditMode($em_columns);
  150.  
  151.  $foreign_keys = array(
  152.      "country_id"=>array("table"=>"demo_countries", "field_key"=>"id", "field_name"=>"name", "view_type"=>"dropdownlist", "condition"=>"", "order_by_field"=>"name", "order_type"=>"ASC", "on_js_event"=>""),
  153.  );
  154.  $dgrid->SetForeignKeysEdit($foreign_keys);
  155.  
  156. ## +---------------------------------------------------------------------------+
  157. ## | 8. Bind the DataGrid:                                                     |
  158. ## +---------------------------------------------------------------------------+
  159. ##  *** bind the DataGrid and draw it on the screen
  160.  $dgrid->Bind();        
  161.   ob_end_flush();
  162. ##
  163. ################################################################################  
  164.  
  165. if(($pr_mode == "edit") || ($pr_mode == "details") || ($pr_mode == "add")){
  166.  
  167.   $_GET["pr_mode"] = $pr_mode;
  168.   $_GET["pr_rid"] = $pr_rid;
  169.   $_REQUEST["pr_mode"] = $pr_mode;
  170.   $_REQUEST["pr_rid"] = $pr_rid;
  171.  
  172. ################################################################################
  173. ##
  174.  ob_start();
  175.   $sql = "
  176.    SELECT
  177.       demo_presidents.id,
  178.       demo_presidents.country_id,
  179.       demo_presidents.name,
  180.       demo_presidents.birth_date,
  181.       demo_presidents.status,
  182.       demo_countries.name as country_name,
  183.       'Edit' as lnk_edit,
  184.       'Details' as lnk_details      
  185.    FROM demo_presidents
  186.    INNER JOIN demo_countries ON demo_presidents.country_id=demo_countries.id
  187.  ";
  188.  
  189. ##  *** set needed options and create a new class instance
  190.  $debug_mode = false;        /* display SQL statements while processing */    
  191.  $messaging = true;          /* display system messages on a screen */
  192.  $unique_prefix = "pr_";    /* prevent overlays - must be started with a letter */
  193.  $dgrid1 = new DataGrid($debug_mode, $messaging, $unique_prefix);
  194. ##  *** set data source with needed options
  195.  $default_order = array("id"=>"ASC");
  196.  $dgrid1->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);            
  197.  
  198. ## +---------------------------------------------------------------------------+
  199. ## | 2. General Settings:                                                      |
  200. ## +---------------------------------------------------------------------------+
  201. $modes = array(
  202.     "add"     =>array("view"=>false, "edit"=>false, "type"=>"link"),
  203.     "edit"    =>array("view"=>false, "edit"=>true,  "type"=>"link", "byFieldValue"=>""),
  204.     "cancel"  =>array("view"=>false, "edit"=>false,  "type"=>"link"),
  205.     "details" =>array("view"=>false, "edit"=>false, "type"=>"link"),
  206.     "delete"  =>array("view"=>false, "edit"=>false,  "type"=>"image")
  207.  );
  208.  $dgrid1->setModes($modes);
  209. ##  *** set CSS class for datagrid
  210. ##  *** "default" or "blue" or "gray" or "green" or your own css file
  211.  $css_class = "gray";
  212.  $dgrid1->setCssClass($css_class);
  213. ##  *** set DataGrid caption
  214.  $dg_caption = "President Info";
  215.  $dgrid1->setCaption($dg_caption);
  216.  
  217. ## +---------------------------------------------------------------------------+
  218. ## | 3. Printing & Exporting Settings:                                         |
  219. ## +---------------------------------------------------------------------------+
  220. ##  *** set printing option: true(default) or false
  221.  $printing_option = false;
  222.  $dgrid1->allowPrinting($printing_option);
  223.  
  224. ## +---------------------------------------------------------------------------+
  225. ## | 7. Add/Edit/Details Mode Settings:                                        |
  226. ## +---------------------------------------------------------------------------+
  227. ##  *** set add/edit mode table properties
  228.  $em_table_properties = array("width"=>"96%");
  229.  $dgrid1->setEditModeTableProperties($em_table_properties);
  230. ##  *** set details mode table properties
  231.  $dm_table_properties = array("width"=>"96%");
  232.  $dgrid1->setDetailsModeTableProperties($dm_table_properties);
  233. ##  ***  set settings for add/edit/details modes
  234.  $table_name  = "demo_presidents";
  235.  $primary_key = "id";
  236.  $condition   = "";
  237.  $dgrid1->setTableEdit($table_name, $primary_key, $condition);
  238. ##  *** set columns in edit mode
  239. $em_columns = array(
  240.     "name"        =>array("header"=>"Name",    "type"=>"textbox",  "align"=>"left",  "req_type"=>"rt", "width"=>"120px", "title"=>"Name", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
  241.     "country_id"  =>array("header"=>"Country", "type"=>"textbox",  "align"=>"left",  "req_type"=>"rt", "width"=>"120px", "title"=>"Country", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
  242.     "birth_date"  =>array("header"=>"Birth Date", "type"=>"date",  "align"=>"left",  "req_type"=>"rt", "width"=>"120px", "title"=>"Date of Birth", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "calendar_type"=>"floating"),
  243.     "status"      =>array("header"=>"Status",  "type"=>"enum",     "align"=>"left",  "req_type"=>"st", "width"=>"120px", "title"=>"Status", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "source"=>"self", "view_type"=>"dropdownlist", "multiple"=>false, "multiple_size"=>"1"),
  244.  );
  245.  $dgrid1->setColumnsInEditMode($em_columns);
  246.  
  247.  $foreign_keys = array(
  248.      "country_id"=>array("table"=>"demo_countries", "field_key"=>"id", "field_name"=>"name", "view_type"=>"dropdownlist", "condition"=>"", "order_by_field"=>"name", "order_type"=>"ASC", "on_js_event"=>""),
  249.  );
  250.  $dgrid1->setForeignKeysEdit($foreign_keys);
  251.  
  252. ## +---------------------------------------------------------------------------+
  253. ## | 8. Bind the DataGrid:                                                     |
  254. ## +---------------------------------------------------------------------------+
  255. ##  *** bind the DataGrid and draw it on the screen
  256.  $dgrid1->bind();        
  257.   ob_end_flush();
  258. ##
  259. ################################################################################  
  260. }
  261.  
  262. ?>