Sample 2-5. Advanced PHP DG code.

    1. View Mode.
    2. Using DataGrids for displaying some statistical data..

   View LIVE DEMO on this sample   BACK to Examples Page

Show Plain Text »
  1. <?php
  2.    
  3.    $year = date("Y");
  4.  
  5.    ################################################################################
  6.    ## +---------------------------------------------------------------------------+
  7.    ## | 1. Creating & Calling:                                                    |
  8.    ## +---------------------------------------------------------------------------+
  9.    ##  *** define a relative (virtual) path to datagrid.class.php file and "pear"
  10.    ##  *** directory (relatively to the current file)
  11.    ##  *** RELATIVE PATH ONLY ***
  12.    //
  13.       define ("DATAGRID_DIR", "datagrid/");                     /* Ex.: "datagrid/" */
  14.       require_once(DATAGRID_DIR."datagrid.class.php");
  15.  
  16.       ob_start();
  17.    ##
  18.    ##  *** creating variables that we need for database connection    
  19.       $DB_USER="******";
  20.       $DB_PASS="******";
  21.       $DB_HOST="localhost";
  22.       $DB_NAME="******";
  23.      
  24.    ################################################################################  
  25.    ##  *** put a primary key on the first place
  26.      $sql = "SELECT
  27.            (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-01-01' AND demo_downloads.download_date < '".$year."-02-01') as month1,
  28.            (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-02-01' AND demo_downloads.download_date < '".$year."-03-01') as month2,
  29.            (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-03-01' AND demo_downloads.download_date < '".$year."-04-01') as month3,
  30.            (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-04-01' AND demo_downloads.download_date < '".$year."-05-01') as month4,
  31.            (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-05-01' AND demo_downloads.download_date < '".$year."-06-01') as month5,
  32.            (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-06-01' AND demo_downloads.download_date < '".$year."-07-01') as month6,
  33.            (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-07-01' AND demo_downloads.download_date < '".$year."-08-01') as month7,
  34.            (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-08-01' AND demo_downloads.download_date < '".$year."-09-01') as month8,
  35.            (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-09-01' AND demo_downloads.download_date < '".$year."-10-01') as month9,
  36.            (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-10-01' AND demo_downloads.download_date < '".$year."-11-01') as month10,
  37.            (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-11-01' AND demo_downloads.download_date < '".$year."-12-01') as month11,
  38.            (SELECT COUNT(*) FROM demo_downloads WHERE demo_downloads.download_date >= '".$year."-12-01' AND demo_downloads.download_date < '".($year+1)."-01-01') as month12
  39.        FROM demo_downloads
  40.        GROUP BY month1, month2, month3, month4 ";
  41.        
  42.    ##  *** set needed options and create a new class instance
  43.      $debug_mode = false;        /* display SQL statements while processing */    
  44.      $messaging = true;          /* display system messages on a screen */
  45.      $unique_prefix = "prd_";    /* prevent overlays - must be started with a letter */
  46.      $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix);
  47.      $dgrid->SetCssClass("pink");
  48.      $dgrid->DisplayLoadingImage("true");
  49.      $dgrid->AllowPaging(false, "", "");
  50.            
  51.    ##  *** set data source with needed options
  52.      $default_order = array("download_date"=>"DESC");
  53.      $dgrid->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);      
  54.    
  55.    ##  *** "byFieldValue"=>"fieldName" - make the field to be a link to edit mode page
  56.      $modes = array(
  57.          "add"     =>array("view"=>false, "edit"=>false, "type"=>"link"),
  58.          "edit"    =>array("view"=>false, "edit"=>false,  "type"=>"link", "byFieldValue"=>""),
  59.          "cancel"  =>array("view"=>false, "edit"=>false,  "type"=>"link"),
  60.          "details" =>array("view"=>false, "edit"=>false, "type"=>"link"),
  61.          "delete"  =>array("view"=>false, "edit"=>false,  "type"=>"image")
  62.      );
  63.      $dgrid->SetModes($modes);
  64.    ##  *** set DataGrid caption
  65.      $dg_caption = "Current Year";
  66.      $dgrid->SetCaption($dg_caption);
  67.      $dgrid->AllowPrinting(false);
  68.      $dgrid->AllowSorting(false);
  69.      $dgrid->AllowHighlighting(false);
  70.      
  71.    ## +---------------------------------------------------------------------------+
  72.    ## | 6. View Mode Settings:                                                    |
  73.    ## +---------------------------------------------------------------------------+
  74.    ##  *** set view mode table properties
  75.      $vm_table_properties = array("width"=>"444px");
  76.      $dgrid->SetViewModeTableProperties($vm_table_properties);  
  77.      $max_value = "3000";
  78.      $vm_colimns = array(  
  79.         "month1"  =>array("header"=>"Jan", "type"=>"barchart",  "align"=>"center", "width"=>"", "wrap"=>"nowrap", "visible"=>"true", "on_js_event"=>"", "field"=>"", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  80.         "month2"  =>array("header"=>"Feb", "type"=>"barchart",  "align"=>"center", "width"=>"", "wrap"=>"nowrap", "visible"=>"true", "on_js_event"=>"", "field"=>"", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  81.         "month3"  =>array("header"=>"Mar", "type"=>"barchart",  "align"=>"center", "width"=>"", "wrap"=>"nowrap", "visible"=>"true", "on_js_event"=>"", "field"=>"", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  82.         "month4"  =>array("header"=>"Apr", "type"=>"barchart",  "align"=>"center", "width"=>"", "wrap"=>"nowrap", "visible"=>"true", "on_js_event"=>"", "field"=>"", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  83.         "month5"  =>array("header"=>"May", "type"=>"barchart",  "align"=>"center", "width"=>"", "wrap"=>"nowrap", "visible"=>"true", "on_js_event"=>"", "field"=>"", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  84.         "month6"  =>array("header"=>"Jun", "type"=>"barchart",  "align"=>"center", "width"=>"", "wrap"=>"nowrap", "visible"=>"true", "on_js_event"=>"", "field"=>"", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  85.         "month7"  =>array("header"=>"Jul", "type"=>"barchart",  "align"=>"center", "width"=>"", "wrap"=>"nowrap", "visible"=>"true", "on_js_event"=>"", "field"=>"", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  86.         "month8"  =>array("header"=>"Aug", "type"=>"barchart",  "align"=>"center", "width"=>"", "wrap"=>"nowrap", "visible"=>"true", "on_js_event"=>"", "field"=>"", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  87.         "month9"  =>array("header"=>"Sep", "type"=>"barchart",  "align"=>"center", "width"=>"", "wrap"=>"nowrap", "visible"=>"true", "on_js_event"=>"", "field"=>"", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  88.         "month10"  =>array("header"=>"Oct", "type"=>"barchart",  "align"=>"center", "width"=>"", "wrap"=>"nowrap", "visible"=>"true", "on_js_event"=>"", "field"=>"", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  89.         "month11"  =>array("header"=>"Nov", "type"=>"barchart",  "align"=>"center", "width"=>"", "wrap"=>"nowrap", "visible"=>"true", "on_js_event"=>"", "field"=>"", "maximum_value"=>$max_value, "display_type"=>"vertical"),
  90.      );
  91.      $dgrid->SetColumnsInViewMode($vm_colimns);
  92.    
  93.    ## +---------------------------------------------------------------------------+
  94.    ## | 8. Bind the DataGrid:                                                     |
  95.    ## +---------------------------------------------------------------------------+
  96.    ##  *** bind the DataGrid and draw it on the screen
  97.       $dgrid->Bind();        
  98.       ob_end_flush();
  99.    ##
  100.    ################################################################################  
  101. }
  102.  
  103. ?>