Sample 2-6. Advanced PHP DG code - Master/Detail DataGrid Structure.

    1. Master DataGrid in View Mode.
    2. Second Datagrid in all Modes.

   View LIVE DEMO on this sample   BACK to Examples Page

Show Plain Text »
  1. <?php
  2.  
  3.   $act = isset($_GET["act"]) ? $_GET["act"] : "";
  4.   $region_id = isset($_GET["region_id"]) ? $_GET["region_id"] : "";
  5.   $regn_p = isset($_GET["regn_p"]) ? $_GET["regn_p"] : "";
  6.   $regn_page_size = isset($_GET["regn_page_size"]) ? $_GET["regn_page_size"] : "";
  7.  
  8.   ################################################################################
  9.   ## +---------------------------------------------------------------------------
  10.   ## | 1. Creating & Calling:                                                    
  11.   ## +---------------------------------------------------------------------------
  12.   ##  *** define a relative (virtual) path to datagrid.class.php file (relatively to the current file)
  13.   ##  *** RELATIVE PATH ONLY ***
  14.   ##  Ex.: "datagrid/datagrid.class.php" or "datagrid.class.php" etc.
  15.     define ("DATAGRID_DIR", "datagrid/");                     /* Ex.: "datagrid/" */
  16.     require_once(DATAGRID_DIR."datagrid.class.php");
  17.  
  18.   ##  *** creating variables that we need for database connection      
  19.     $DB_USER="demo_user";
  20.     $DB_PASS="demo_password";      
  21.     $DB_HOST="demo_host";
  22.     $DB_NAME="demo_database";
  23.      
  24.   ##  *** put a primary key on the first place
  25.     $sql = "SELECT demo_regions.id, demo_regions.name, 'View Countries' as link_to_countries FROM demo_regions";
  26.          
  27.   ##  *** set needed options and create a new class instance
  28.     $debug_mode = false;        /* display SQL statements while processing */    
  29.     $messaging = true;          /* display system messages on a screen */
  30.     $unique_prefix = "regn_";    /* prevent overlays - must be started with a letter */
  31.     $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix);
  32.    
  33.   ##  *** set data source with needed options
  34.     $default_order = array("id"=>"ASC");
  35.     $dgrid->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);            
  36.  
  37.   ## +---------------------------------------------------------------------------
  38.   ## | 2. General Settings:                                                      
  39.   ## +---------------------------------------------------------------------------
  40.     $dg_language = "en";  
  41.     $dgrid->SetInterfaceLang($dg_language);
  42.  
  43.   ##  *** set layouts: "0" - tabular(horizontal) - default, "1" - columnar(vertical), "2" - customized
  44.     $layouts = array("view"=>"0", "edit"=>"0", "details"=>"1", "filter"=>"1");
  45.     $dgrid->setLayouts($layouts);
  46.  
  47.   ##  *** set modes for operations ("type" => "link|button|image")
  48.   ##  *** "byFieldValue"=>"fieldName" - make the field to be a link to edit mode page
  49.     $modes = array(
  50.        "add"      =>array("view"=>false, "edit"=>false, "type"=>"link"),
  51.        "edit"     =>array("view"=>false, "edit"=>false,  "type"=>"link", "byFieldValue"=>""),
  52.        "cancel"  =>array("view"=>false, "edit"=>false,  "type"=>"link"),
  53.        "details" =>array("view"=>false, "edit"=>false, "type"=>"link"),
  54.        "delete"  =>array("view"=>false, "edit"=>false,  "type"=>"image")
  55.     );
  56.     $dgrid->setModes($modes);
  57.  
  58.     $css_class = "x-blue";
  59.     $dgrid->SetCssClass($css_class);
  60.   ##  *** set DataGrid caption
  61.     $dg_caption = "Regions (Master DataGrid)";
  62.     $dgrid->setCaption($dg_caption);
  63.  
  64.   ## +---------------------------------------------------------------------------
  65.   ## | 3. Printing & Exporting Settings:                                          
  66.   ## +---------------------------------------------------------------------------
  67.   ##  *** set printing option: true(default) or false
  68.     $printing_option = false;
  69.     $dgrid->AllowPrinting($printing_option);
  70.  
  71.   ## +---------------------------------------------------------------------------
  72.   ## | 4. Sorting & Paging Settings:                                              
  73.   ## +---------------------------------------------------------------------------
  74.   ##  *** set sorting option: true(default) or false
  75.     $sorting_option = true;
  76.     $dgrid->AllowSorting($sorting_option);              
  77.   ##  *** set paging option: true(default) or false
  78.     $paging_option = true;
  79.     $rows_numeration = false;
  80.     $numeration_sign = "N #";
  81.     $dropdown_paging = true;
  82.     $dgrid->AllowPaging($paging_option, $rows_numeration, $numeration_sign, $dropdown_paging);
  83.   ##  *** set paging settings
  84.     $bottom_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", "page_size"=>true, "page_size_align"=>"right");
  85.     $top_paging = array();
  86.     $pages_array = array("5"=>"5", "10"=>"10", "15"=>"15", "25"=>"25", "50"=>"50", "100"=>"100");
  87.     $default_page_size = 5;
  88.     $paging_arrows = array("first"=>"|&lt;&lt;", "previous"=>"&lt;&lt;", "next"=>"&gt;&gt;", "last"=>"&gt;&gt;|");
  89.     $dgrid->SetPagingSettings($bottom_paging, $top_paging, $pages_array, $default_page_size, $paging_arrows);
  90.  
  91.   ## +---------------------------------------------------------------------------
  92.   ## | 6. View Mode Settings:                                                    
  93.   ## +---------------------------------------------------------------------------
  94.   ##  *** set view mode table properties
  95.     $vm_table_properties = array("width"=>"60%");
  96.     $dgrid->setViewModeTableProperties($vm_table_properties);  
  97.   ##  *** set columns in view mode
  98.   ##  *** "barchart" : number format in SELECT SQL must be equal with number format in max_value
  99.     $vm_colimns = array(  
  100.       "id"                =>array("header"=>"ID",     "type"=>"label",   "align"=>"center", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>true|false, "tooltip_type"=>"floating|simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  101.       "name"              =>array("header"=>"Name",   "type"=>"label", "align"=>"left", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>true|false, "tooltip_type"=>"floating|simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  102.       "link_to_countries" =>array("header"=>"Action", "type"=>"link", "align"=>"center", "width"=>"150px", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>"false", "tooltip_type"=>"floating|simple", "case"=>"normal|upper|lower|camel", "summarize"=>"false", "sort_type"=>"string|numeric", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "field_key"=>"id", "field_data"=>"link_to_countries", "rel"=>"", "title"=>"", "target"=>"_self", "href"=>"sample_2_6_demo.php?act=details&region_id={0}&regn_p=".$regn_p."&regn_page_size=".$regn_page_size),        
  103.     );
  104.     $dgrid->setColumnsInViewMode($vm_colimns);
  105.  
  106.   if($act == "details"){
  107.      
  108.       ##  *** put a primary key on the first place
  109.       $sql=" SELECT  
  110.          demo_countries.id,
  111.          demo_countries.name,
  112.          demo_countries.description,
  113.          demo_countries.picture_url,
  114.          FORMAT(demo_countries.population, 0) as population,
  115.          CASE WHEN demo_countries.is_democracy = 1 THEN 'Yes' ELSE 'No' END as is_democracy
  116.      FROM demo_countries
  117.      WHERE region_id = ".(int)$region_id;        
  118.          
  119.       ##  *** set needed options and create a new class instance
  120.       $debug_mode = false;        /* display SQL statements while processing */    
  121.       $messaging = true;          /* display system messages on a screen */
  122.       $unique_prefix = "cnt_";    /* prevent overlays - must be started with a letter */
  123.       $dgrid1 = new DataGrid($debug_mode, $messaging, $unique_prefix);
  124.      
  125.       ##  *** set data source with needed options
  126.       $default_order = array("id"=>"ASC");
  127.       $dgrid1->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);            
  128.        
  129.       $css_class = "x-gray";
  130.       $dgrid1->SetCssClass($css_class);
  131.        
  132.       ##  *** set DataGrid caption
  133.       $dg_caption = "Countries (Detail DataGrid)";
  134.       $dgrid1->setCaption($dg_caption);
  135.      
  136.       ##  *** set variables that used to get access to the page (like: my_page.php?act=34&id=56 etc.)
  137.       $http_get_vars = array("act", "region_id");
  138.       $dgrid1->SetHttpGetVars($http_get_vars);
  139.  
  140.       ## +---------------------------------------------------------------------------
  141.       ## | 3. Printing & Exporting Settings:                                          
  142.       ## +---------------------------------------------------------------------------
  143.       ##  *** set printing option: true(default) or false
  144.       $printing_option = false;
  145.       $dgrid1->AllowPrinting($printing_option);
  146.      
  147.       ## +---------------------------------------------------------------------------
  148.       ## | 6. View Mode Settings:                                                    
  149.       ## +---------------------------------------------------------------------------
  150.       ##  *** set view mode table properties
  151.       $vm_table_properties = array("width"=>"60%");
  152.       $dgrid1->setViewModeTableProperties($vm_table_properties);  
  153.       ##  *** set columns in view mode
  154.       ##  *** "barchart" : number format in SELECT SQL must be equal with number format in max_value
  155.       $vm_colimns = array(  
  156.          "name"       =>array("header"=>"Name",   "type"=>"label", "align"=>"left", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>true|false, "tooltip_type"=>"floating|simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  157.          "population" =>array("header"=>"Population", "type"=>"label", "align"=>"left", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>true|false, "tooltip_type"=>"floating|simple", "case"=>"normal", "summarize"=>"false", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  158.       );
  159.       $dgrid1->setColumnsInViewMode($vm_colimns);
  160.      
  161.       ## +---------------------------------------------------------------------------
  162.       ## | 7. Add/Edit/Details Mode settings:                                        
  163.       ## +---------------------------------------------------------------------------
  164.       ##  ***  set settings for edit/details mode
  165.       $em_table_properties = array("width"=>"60%");
  166.       $dgrid1->SetEditModeTableProperties($em_table_properties);
  167.       $dm_table_properties = array("width"=>"60%");
  168.       $dgrid1->SetDetailsModeTableProperties($dm_table_properties);
  169.       ##  ***  set settings for edit/details mode
  170.       $table_name = "demo_countries";
  171.       $primary_key = "id";
  172.       $condition = "";
  173.       $dgrid1->setTableEdit($table_name, $primary_key, $condition);
  174.  
  175.       $fill_from_array = array("10000"=>"10000", "250000"=>"250000", "5000000"=>"5000000", "25000000"=>"25000000", "100000000"=>"100000000");
  176.       $em_columns = array(
  177.          "region_id"        =>array("header"=>"Region",           "type"=>"textbox",  "width"=>"210px", "req_type"=>"rt", "title"=>"Region Name"),
  178.          "name"             =>array("header"=>"Country",          "type"=>"textbox",  "width"=>"210px", "req_type"=>"ry", "title"=>"Country Name", "unique"=>true),
  179.          "description"      =>array("header"=>"Short Descr.",     "type"=>"textarea", "width"=>"210px", "req_type"=>"rt", "title"=>"Short Description", "edit_type"=>"wysiwyg", "rows"=>"3", "cols"=>"50"),
  180.          "population"       =>array("header"=>"Peoples",          "type"=>"enum",     "source"=>$fill_from_array, "view_type"=>"dropdownlist",  "width"=>"139px", "req_type"=>"ri", "title"=>"Population (Peoples)"),
  181.          "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"),
  182.          "is_democracy"     =>array("header"=>"Is Democracy",     "type"=>"checkbox", "true_value"=>1, "false_value"=>0,  "width"=>"210px", "req_type"=>"sy", "title"=>"Is Democraty"),
  183.          "independent_date" =>array("header"=>"Independence Day", "type"=>"date",     "req_type"=>"rt", "width"=>"187px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "calendar_type"=>"floating"),
  184.       );
  185.       $dgrid1->SetColumnsInEditMode($em_columns);
  186.       ##  *** set foreign keys for add/edit/details modes (if there are linked tables)
  187.       $foreign_keys = array(
  188.          "region_id"=>array("table"=>"demo_regions", "field_key"=>"id", "field_name"=>"name", "view_type"=>"dropdownlist", "order_by_field"=>"name", "order_type"=>"ASC"),
  189.       );
  190.       $dgrid1->SetForeignKeysEdit($foreign_keys);
  191.   }
  192.  
  193.    ## +---------------------------------------------------------------------------
  194.   ## | 8. Bind the DataGrid:                                                      
  195.   ## +---------------------------------------------------------------------------
  196.   ##  *** bind the DataGrid and draw it on the screen
  197.  
  198.   ob_start();    
  199.     $dgrid->Bind();            
  200.      
  201.     if($act == "details"){
  202.         echo "<br />";
  203.         $dgrid1->Bind();                
  204.     }
  205.   ob_end_flush();    
  206.  
  207.   ################################################################################
  208.  
  209. ?>