Sample 2-1. Simple PHP DG code.

    1. All modes (Add/Edit/Details/Delete/View).
    2. All features.
    3. Many types of fields.
    4. Image magnifying feature.
    5. WYSIWYG editor.
    etc.

   View LIVE DEMO on this sample   BACK to Examples Page

Show Plain Text »
  1. <?php
  2.    
  3. ################################################################################
  4. ## +---------------------------------------------------------------------------+
  5. ## | 1. Creating & Calling:                                                    |
  6. ## +---------------------------------------------------------------------------+
  7. ##  *** define a relative (virtual) path to datagrid.class.php file and "pear"
  8. ##  *** directory (relatively to the current file)
  9. ##  *** RELATIVE PATH ONLY ***
  10.   define ("DATAGRID_DIR", "datagrid/");                    
  11.   require_once(DATAGRID_DIR."datagrid.class.php");
  12.    
  13. ##  *** creating variables that we need for database connection
  14.   $DB_USER="username";            
  15.   $DB_PASS="password";            
  16.   $DB_HOST="localhost";          
  17.   $DB_NAME="database_name";      
  18.  
  19.   ob_start();
  20. ##  *** put a primary key on the first place
  21.   $sql = "SELECT
  22.            demo_products.id,
  23.            demo_products.name,
  24.            demo_products.short_description,
  25.            demo_products.long_description,
  26.            demo_products.image_thumb,
  27.            demo_products.image_big,
  28.            demo_products.price,
  29.            demo_products.available_from,
  30.            demo_products.statistics,
  31.            demo_suppliers.name as supplier_name
  32.        FROM demo_products
  33.            LEFT OUTER JOIN demo_suppliers ON demo_products.supplier_id = demo_suppliers.id ";
  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. ##  *** set encoding and collation (default: utf8/utf8_unicode_ci)
  41.   $dg_encoding = "utf8";
  42.   $dg_collation = "utf8_unicode_ci";
  43.   $dgrid->SetEncoding($dg_encoding, $dg_collation);
  44. ##  *** set data source with needed options
  45.   $default_order = array("demo_products.name"=>"ASC");
  46.   $dgrid->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);            
  47.  
  48. ## +---------------------------------------------------------------------------+
  49. ## | 2. General Settings:                                                      |
  50. ## +---------------------------------------------------------------------------+
  51. ##  *** allow multirow operations
  52.   $multirow_option = true;
  53.   $dgrid->AllowMultirowOperations($multirow_option);
  54.   $multirow_operations = array(
  55.         "delete"  => array("view"=>true),
  56.         "details" => array("view"=>true),
  57.   );
  58.   $dgrid->SetMultirowOperations($multirow_operations);  
  59. ##  *** set CSS class for datagrid
  60. ##  *** "default" or "blue" or "gray" or "green" or "pink" or your own css file
  61. ##  *** set DataGrid caption
  62.   $dg_caption = "List of Mobile Phones";
  63.   $dgrid->SetCaption($dg_caption);
  64.  
  65. ## +---------------------------------------------------------------------------+
  66. ## | 3. Printing & Exporting Settings:                                         |
  67. ## +---------------------------------------------------------------------------+
  68. ##  *** set printing option: true(default) or false
  69.   $printing_option = false;
  70.   $dgrid->AllowPrinting($printing_option);
  71.  
  72. ## +---------------------------------------------------------------------------+
  73. ## | 4. Sorting & Paging Settings:                                             |
  74. ## +---------------------------------------------------------------------------+
  75. ##  *** set paging option: true(default) or false
  76.   $paging_option = true;
  77.   $rows_numeration = false;
  78.   $numeration_sign = "N #";      
  79.   $dgrid->AllowPaging($paging_option, $rows_numeration, $numeration_sign);
  80. ##  *** set paging settings
  81.   $bottom_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", "page_size"=>true, "page_size_align"=>"right");
  82.   $top_paging = array();
  83.   $pages_array = array("10"=>"10", "25"=>"25", "50"=>"50", "100"=>"100", "250"=>"250", "500"=>"500", "1000"=>"1000");
  84.   $default_page_size = 10;
  85.   $dgrid->SetPagingSettings($bottom_paging, $top_paging, $pages_array, $default_page_size);
  86.  
  87. ## +---------------------------------------------------------------------------+
  88. ## | 5. Filter Settings:                                                       |
  89. ## +---------------------------------------------------------------------------+
  90. ##  *** set filtering option: true or false(default)
  91.   $filtering_option = true;
  92.   $show_search_type = true;
  93.   $dgrid->AllowFiltering($filtering_option, $show_search_type);
  94. ##  *** set additional filtering settings
  95. ##  *** tips: use "," (comma) if you want to make search by some words, for ex.: hello, bye, hi
  96.   $filtering_fields = array(
  97.     "Supplier"  => array("type"=>"enum", "order"=>"ASC", "table"=>"demo_suppliers", "field"=>"name", "source"=>"self", "show"=>"", "condition"=>"", "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"", "multiple"=>"false", "multiple_size"=>"4", "on_js_event"=>""),
  98.     "From"      =>array("type"=>"calendar", "table"=>"demo_products", "field"=>"available_from", "field_type"=>"from", "show_operator"=>"false", "default_operator"=>">=", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"", "on_js_event"=>""),
  99.     "To"        =>array("type"=>"calendar", "table"=>"demo_products", "field"=>"available_from", "field_type"=>"to", "show_operator"=>"false", "default_operator"=>"<=", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"", "on_js_event"=>""),
  100.   );
  101.   $dgrid->SetFieldsFiltering($filtering_fields);
  102.  
  103. ## +---------------------------------------------------------------------------+
  104. ## | 6. View Mode Settings:                                                    |
  105. ## +---------------------------------------------------------------------------+
  106. ##  *** set view mode table properties
  107.   $vm_table_properties = array("width"=>"95%");
  108.   $dgrid->SetViewModeTableProperties($vm_table_properties);  
  109. ##  *** set columns in view mode
  110. ##  *** Ex.: "on_js_event"=>"onclick="alert(\"Yes!!!\");""
  111. ##  ***      "barchart" : number format in SELECT SQL must be equal with number format in max_value
  112.   $vm_colimns = array(
  113.     "name"              => array("header"=>"Name", "type"=>"linktoedit",      "align"=>"left", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>"false", "tooltip_type"=>"floating|simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  114.     "image_thumb"       => array("header"=>"Image", "type"=>"image",  "align"=>"center", "width"=>"58x", "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"=>"", "target_path"=>"uploads/", "default"=>"default_image.ext", "image_width"=>"50px", "image_height"=>"30px", "magnify"=>"true", "magnify_type"=>"magnifier", "magnify_power"=>"3"),
  115.     "supplier_name"     => array("header"=>"Supplier", "type"=>"linktoview",      "align"=>"center", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>"false", "tooltip_type"=>"floating|simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  116.     "short_description" => array("header"=>"Short Description", "type"=>"label",      "align"=>"left", "width"=>"120px", "wrap"=>"nowrap", "text_length"=>"12", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  117.     "price"             => array("header"=>"Price", "type"=>"money",      "align"=>"right", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>"false", "tooltip_type"=>"floating|simple", "case"=>"normal|upper|lower|camel", "summarize"=>"true", "sort_type"=>"string|numeric", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "sign"=>"$", "decimal_places"=>"2", "dec_separator"=>".", "thousands_separator"=>","),
  118.     "available_from"    => array("header"=>"Available From", "type"=>"label",      "align"=>"center", "width"=>"", "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"=>"", "on_item_created"=>"my_format_date"),
  119.     "statistics"        => array("header"=>"Sales %", "type"=>"barchart",   "align"=>"left", "width"=>"", "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"=>"statistics", "maximum_value"=>"100"),
  120.   );
  121.   $dgrid->SetColumnsInViewMode($vm_colimns);
  122.  
  123. ## +---------------------------------------------------------------------------+
  124. ## | 7. Add/Edit/Details Mode Settings:                                        |
  125. ## +---------------------------------------------------------------------------+
  126. ##  *** set add/edit mode table properties
  127.   $em_table_properties = array("width"=>"70%");
  128.   $dgrid->SetEditModeTableProperties($em_table_properties);
  129. ##  *** set details mode table properties
  130.   $dm_table_properties = array("width"=>"70%");
  131.   $dgrid->SetDetailsModeTableProperties($dm_table_properties);
  132. ##  ***  set settings for add/edit/details modes
  133.   $table_name  = "demo_products";
  134.   $primary_key = "id";
  135.   $condition   = "";
  136.   $dgrid->SetTableEdit($table_name, $primary_key, $condition);
  137.  
  138.   $fill_from_array_sales = array();
  139.   for($i=1; $i<100; $i++){
  140.     $fill_from_array_sales[$i] = $i;
  141.   }
  142.  
  143.   $fill_from_array_chargers = array("linear_regulator"=>"Linear Regulator", "switch_mode"=>"Switch Mode", "shunt_regulators"=>"Shunt Regulators", "pulsed_charging"=>"Pulsed Charging");
  144.   $em_columns = array(
  145.     "delimiter_0"   =>array("inner_html"=>"&lt;div style='padding:5px;'&gt;Textbox and dropdown list fields: They are basic fields for each DataGrid. Supplier is an example of Foreign Key with possibility to select value from dropdown list.&lt;/div&gt;"),
  146.     "name"          =>array("header"=>"Product Name", "type"=>"textbox",    "req_type"=>"rt", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
  147.     "supplier_id"   =>array("header"=>"Supplier", "type"=>"foreign_key","req_type"=>"ri", "width"=>"210px", "title"=>"", "readonly"=>"false", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true"),
  148.     "charger_types" =>array("header"=>"Charger Types", "type"=>"enum",  "req_type"=>"st", "width"=>"210px", "title"=>"", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "source"=>$fill_from_array_chargers, "view_type"=>"checkbox", "radiobuttons_alignment"=>"horizontal|vertical", "multiple"=>true, "multiple_size"=>"5"),
  149.    
  150.     "delimiter_1"   =>array("inner_html"=>"&lt;div style='padding:5px;'&gt;Image fields: These are image fields. You can click on each picture to enlarge. Uploading or deleting of images is blocked in Demo version.&lt;/div&gt;"),
  151.     "image_thumb"   =>array("header"=>"Thumbnail", "type"=>"image",      "req_type"=>"st", "width"=>"220px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "target_path"=>"uploads/", "max_file_size"=>"200K", "image_width"=>"120px", "image_height"=>"90px", "magnify"=>"true", "magnify_type"=>"lightbox", "file_name"=>"img_".((isset($_GET["prd_mode"]) && ($_GET["prd_mode"] == "add")) ? $dgrid->GetRandomString("10") : $dgrid->GetCurrentId()), "host"=>"local", "pre_addition"=>"Click on image to enlarge...&lt;br&gt;"),
  152.     "image_big"     =>array("header"=>"Big Image", "type"=>"image",      "req_type"=>"st", "width"=>"220px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "target_path"=>"uploads/", "max_file_size"=>"500K", "image_width"=>"240px", "image_height"=>"180px", "magnify"=>"true", "magnify_type"=>"lightbox", "file_name"=>"img_".((isset($_GET["prd_mode"]) && ($_GET["prd_mode"] == "add")) ? $dgrid->GetRandomString("10") : $dgrid->GetCurrentId()), "host"=>"local", "pre_addition"=>"Click on image to enlarge...&lt;br&gt;"),
  153.    
  154.     "delimiter_2"   =>array("inner_html"=>"&lt;div style='padding:5px;'&gt;Teaxtarea fields: There are 2 types of textarea fields. First is a "simple", with draggable bar at the bottom to resizing. Second - is a WYSIWYG Editor. In WYSIWYG Editor you can easy edit and format the text. WYSIWYG implies a user interface that allows the user to view something very similar to the end result while the document is being created.&lt;/div&gt;"),
  155.     "short_description"  =>array("header"=>"Short Description", "type"=>"textarea",   "req_type"=>"rt", "width"=>"500px", "title"=>"", "readonly"=>"false", "maxlength"=>"255", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "edit_type"=>"simple", "resizable"=>"true", "rows"=>"5", "cols"=>"50"),
  156.     "long_description"   =>array("header"=>"Long Description", "type"=>"textarea",   "req_type"=>"rt", "width"=>"500px", "title"=>"", "readonly"=>"false", "maxlength"=>"1024", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "edit_type"=>"wysiwyg", "resizable"=>"false", "rows"=>"7", "cols"=>"50"),
  157.  
  158.     "delimiter_3"   =>array("inner_html"=>"&lt;div style='padding:5px;'&gt;Date/Time fields: There are 2 types of date/time fields: floating calendar and dropdown list calendar.&lt;/div&gt;"),
  159.     "available_from"  =>array("header"=>"Available From", "type"=>"datetime",   "req_type"=>"st", "width"=>"187px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "calendar_type"=>"dropdownlist"),
  160.     "date_added"    =>array("header"=>"Date Added", "type"=>"label",      "req_type"=>"rt", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
  161.     "last_updated"  =>array("header"=>"Last Updated", "type"=>"datetime",   "req_type"=>"st", "width"=>"187px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "calendar_type"=>"floating"),
  162.    
  163.     "delimiter_4"   =>array("inner_html"=>"&lt;div style='padding:5px;'&gt;Other fields: Money, checkbox, predefined dropdown list (enum) and radio buttons.&lt;/div&gt;"),
  164.     "price"         =>array("header"=>"Price", "type"=>"textbox",    "req_type"=>"rf", "width"=>"90px", "title"=>"", "readonly"=>"false", "maxlength"=>"12", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "pre_addition"=>"$"),
  165.     "statistics"    =>array("header"=>"Sales in %", "type"=>"enum",       "req_type"=>"rf", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "source"=>$fill_from_array_sales, "view_type"=>"dropdownlist", "radiobuttons_alignment"=>"horizontal|vertical", "multiple"=>"false", "multiple_size"=>"4"),
  166.     "subscribe"     =>array("header"=>"Subscribe for news?", "type"=>"checkbox",   "req_type"=>"st", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "true_value"=>1, "false_value"=>0),
  167.     "is_featured"   =>array("header"=>"Is Featured", "type"=>"enum",       "req_type"=>"st", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "source"=>"self", "view_type"=>"radiobutton", "radiobuttons_alignment"=>"horizontal", "multiple"=>"false", "multiple_size"=>"4"),
  168.   );
  169.   $dgrid->SetColumnsInEditMode($em_columns);
  170. ##  *** set auto-generated columns in edit mode
  171.   $auto_column_in_edit_mode = false;
  172.   $dgrid->SetAutoColumnsInEditMode($auto_column_in_edit_mode);
  173. ##  *** set foreign keys for add/edit/details modes (if there are linked tables)
  174.   $foreign_keys = array(
  175.     "supplier_id"=>array("table"=>"demo_suppliers", "field_key"=>"id", "field_name"=>"name", "view_type"=>"dropdownlist", "radiobuttons_alignment"=>"horizontal|vertical", "condition"=>"", "order_by_field"=>"name", "order_type"=>"ASC", "on_js_event"=>""),
  176.   );
  177.   $dgrid->SetForeignKeysEdit($foreign_keys);
  178.  
  179. ## +---------------------------------------------------------------------------+
  180. ## | 8. Bind the DataGrid:                                                     |
  181. ## +---------------------------------------------------------------------------+
  182. ##  *** bind the DataGrid and draw it on the screen
  183.   $dgrid->Bind();        
  184.   ob_end_flush();
  185.  
  186. ################################################################################  
  187.  
  188. function my_format_date($last_login_time){
  189.     $last_login = @mktime(substr($last_login_time, 11, 2), substr($last_login_time, 14, 2),
  190.                         substr($last_login_time, 17, 2), substr($last_login_time, 5, 2),
  191.                         substr($last_login_time, 8, 2), substr($last_login_time, 0, 4));
  192.     if($last_login_time != ""){
  193.         return @date("M d, Y g:i A", $last_login);
  194.     }else return "";
  195. }
  196.  
  197. ?>