Sample 2-5. Advanced DataGrid code.

    1. View Mode.
    2. Using DataGrids for displaying some statistical data.
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. ##  *** put a primary key on the first place
  19. $sql = "SELECT
  20.    (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-01-01' AND demo_downloads.download_date < '".$year."-02-01' ".$where_clause.") as month1,
  21.    (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-02-01' AND demo_downloads.download_date < '".$year."-03-01' ".$where_clause.") as month2,
  22.    (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-03-01' AND demo_downloads.download_date < '".$year."-04-01' ".$where_clause.") as month3,
  23.    (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-04-01' AND demo_downloads.download_date < '".$year."-05-01' ".$where_clause.") as month4,
  24.    (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-05-01' AND demo_downloads.download_date < '".$year."-06-01' ".$where_clause.") as month5,
  25.    (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-06-01' AND demo_downloads.download_date < '".$year."-07-01' ".$where_clause.") as month6,
  26.    (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-07-01' AND demo_downloads.download_date < '".$year."-08-01' ".$where_clause.") as month7,
  27.    (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-08-01' AND demo_downloads.download_date < '".$year."-09-01' ".$where_clause.") as month8,
  28.    (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-09-01' AND demo_downloads.download_date < '".$year."-10-01' ".$where_clause.") as month9,
  29.    (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-10-01' AND demo_downloads.download_date < '".$year."-11-01' ".$where_clause.") as month10,
  30.    (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-11-01' AND demo_downloads.download_date < '".$year."-12-01' ".$where_clause.") as month11,
  31.    (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-12-01' AND demo_downloads.download_date < '".($year+1)."-01-01' ".$where_clause.") as month12
  32.  FROM demo_downloads
  33.  GROUP BY month1, month2, month3, month4 ";        
  34.      
  35. ##  *** set needed options and create a new class instance
  36. $debug_mode = false;        /* display SQL statements while processing */    
  37. $messaging = true;          /* display system messages on a screen */
  38. $unique_prefix = "prd_";    /* prevent overlays - must be started with a letter */
  39. $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix);
  40. $dgrid->SetCssClass($css_class);
  41.  
  42. ##  *** set variables that used to get access to the page (like: my_page.php?act=34&id=56 etc.)
  43. $dgrid->SetHttpGetVars(array("selected_year"));      
  44. $dgrid->DisplayLoadingImage("true");
  45. $dgrid->AllowPaging(false, "", "");
  46.      
  47. ##  *** set data source with needed options
  48. $default_order = array("download_date"=>"DESC");
  49. $dgrid->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);      
  50.  
  51. ##  *** "byFieldValue"=>"fieldName" - make the field to be a link to edit mode page
  52. $modes = array(
  53.    "add"     =>array("view"=>false, "edit"=>false, "type"=>"link"),
  54.    "edit"    =>array("view"=>false, "edit"=>false,  "type"=>"link", "byFieldValue"=>""),
  55.    "details" =>array("view"=>false, "edit"=>false, "type"=>"link"),
  56.    "delete"  =>array("view"=>false, "edit"=>false,  "type"=>"image")
  57. );
  58. $dgrid->SetModes($modes);
  59. ##  *** set DataGrid caption
  60. $dg_caption = "Current Year";
  61. $dgrid->SetCaption($dg_caption);
  62. $dgrid->AllowPrinting(false);
  63. $dgrid->AllowSorting(false);
  64. $dgrid->AllowHighlighting(false);
  65.  
  66. ## +---------------------------------------------------------------------------+
  67. ## | 6. View Mode Settings:                                                    |
  68. ## +---------------------------------------------------------------------------+
  69. ##  *** set view mode table properties
  70. $vm_table_properties = array("width"=>"444px");
  71. $dgrid->SetViewModeTableProperties($vm_table_properties);  
  72. $max_value = "3000";
  73. $vm_columns = array(  
  74.   "month1"  =>array("header"=>"Jan", "type"=>"barchart",  "align"=>"center", "wrap"=>"nowrap", "visible"=>"true", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  75.   "month2"  =>array("header"=>"Feb", "type"=>"barchart",  "align"=>"center", "wrap"=>"nowrap", "visible"=>"true", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  76.   "month3"  =>array("header"=>"Mar", "type"=>"barchart",  "align"=>"center", "wrap"=>"nowrap", "visible"=>"true", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  77.   "month4"  =>array("header"=>"Apr", "type"=>"barchart",  "align"=>"center", "wrap"=>"nowrap", "visible"=>"true", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  78.   "month5"  =>array("header"=>"May", "type"=>"barchart",  "align"=>"center", "wrap"=>"nowrap", "visible"=>"true", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  79.   "month6"  =>array("header"=>"Jun", "type"=>"barchart",  "align"=>"center", "wrap"=>"nowrap", "visible"=>"true", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  80.   "month7"  =>array("header"=>"Jul", "type"=>"barchart",  "align"=>"center", "wrap"=>"nowrap", "visible"=>"true", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  81.   "month8"  =>array("header"=>"Aug", "type"=>"barchart",  "align"=>"center", "wrap"=>"nowrap", "visible"=>"true", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  82.   "month9"  =>array("header"=>"Sep", "type"=>"barchart",  "align"=>"center", "wrap"=>"nowrap", "visible"=>"true", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  83.   "month10"  =>array("header"=>"Oct", "type"=>"barchart",  "align"=>"center", "wrap"=>"nowrap", "visible"=>"true", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  84.   "month11"  =>array("header"=>"Nov", "type"=>"barchart",  "align"=>"center", "wrap"=>"nowrap", "visible"=>"true", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  85.   "month12"  =>array("header"=>"Dec", "type"=>"barchart",  "align"=>"center", "wrap"=>"nowrap", "visible"=>"true", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  86. );
  87. $dgrid->SetColumnsInViewMode($vm_columns);
  88.  
  89. ## +---------------------------------------------------------------------------+
  90. ## | 8. Bind the DataGrid:                                                     |
  91. ## +---------------------------------------------------------------------------+
  92. ##  *** bind the DataGrid and draw it on the screen
  93. $dgrid->Bind();        
  94.  
  95. ?>