Sample 2-7. Advanced PHP DG code. AJAX sorting and paging.

    1. All modes (Add/Edit/Details/Delete/View).
    2. All features.
    3. Dependent dropdown lists (regions and countries).
    4. AJAX PostBack submission type.

   View LIVE DEMO on this sample   BACK to Examples Page

Show Plain Text »
  1. <?php
  2.  
  3. ## +---------------------------------------------------------------------------+
  4. ## | 1. Creating & Calling:                                                    |
  5. ## +---------------------------------------------------------------------------+
  6. ##  *** define a relative (virtual) path to datagrid.class.php file and "pear"
  7. ##  *** directory (relatively to the current file)
  8. ##  *** RELATIVE PATH ONLY ***
  9.   define ("DATAGRID_DIR", "datagrid/");                     /* Ex.: "datagrid/" */
  10.   require_once(DATAGRID_DIR."datagrid.class.php");
  11.    
  12. ##  *** creating variables that we need for database connection
  13.   $DB_USER="username";            // usually like this: prefix_name
  14.   $DB_PASS="password";            // must be already enscrypted
  15.   $DB_HOST="localhost";           // often localhost
  16.   $DB_NAME="database_name";       // usually like this: prefix_name  
  17.  
  18.   ob_start();
  19. ##  *** put a primary key on the first place
  20.   $sql=" SELECT
  21.        demo_presidents.id,
  22.        demo_presidents.name,
  23.        demo_presidents.birth_date,
  24.        demo_presidents.status,
  25.        demo_presidents.country_id,
  26.        demo_presidents.is_active,
  27.        demo_presidents.favorite_color,
  28.        demo_countries.name as country_name            
  29.    FROM demo_presidents
  30.        INNER JOIN demo_countries ON demo_presidents.country_id=demo_countries.id ";
  31.  
  32. ##  *** set needed options and create a new class instance
  33.   $debug_mode = false;        /* display SQL statements while processing */    
  34.   $messaging = true;          /* display system messages on a screen */
  35.   $unique_prefix = "prs_";    /* prevent overlays - must be started with a letter */
  36.   $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix);
  37. ##  *** set data source with needed options
  38.   $default_order = array("rating"=>"ASC");
  39.   $dgrid->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);              
  40.  
  41. ## +---------------------------------------------------------------------------+
  42. ## | 2. General Settings:                                                      |
  43. ## +---------------------------------------------------------------------------+
  44. ## +-- PostBack Submission Method ---------------------------------------------+
  45. ##  *** defines postback submission method for DataGrid: AJAX, POST or GET(default)
  46. $postback_method = "ajax";
  47.  $dgrid->SetPostBackMethod($postback_method);
  48.  
  49.   $dg_language = "en";  
  50.   $dgrid->SetInterfaceLang($dg_language);
  51. ##  *** set layouts: "0" - tabular(horizontal) - default, "1" - columnar(vertical), "2" - customized
  52.   $layouts = array("view"=>"0", "edit"=>"1", "details"=>"1", "filter"=>"2");
  53.   $dgrid->SetLayouts($layouts);
  54. ##  *** allow multirow operations
  55.   $multirow_option = true;
  56.   $dgrid->AllowMultirowOperations($multirow_option);
  57.   $multirow_operations = array(
  58.       "edit"  => array("view"=>true),
  59.       "delete"  => array("view"=>true),
  60.       "details" => array("view"=>true),
  61.   );
  62.   $dgrid->SetMultirowOperations($multirow_operations);  
  63. ##  *** set DataGrid caption
  64.   $dg_caption = "Presidents";
  65.   $dgrid->SetCaption($dg_caption);
  66.  
  67. ## +---------------------------------------------------------------------------+
  68. ## | 3. Printing & Exporting Settings:                                         |
  69. ## +---------------------------------------------------------------------------+
  70. ##  *** set printing option: true(default) or false
  71.   $printing_option = true;
  72.   $dgrid->AllowPrinting($printing_option);
  73. ##  *** set exporting option: true(default) or false and relative (virtual) path
  74. ##  *** to export directory (relatively to datagrid.class.php file).
  75. ##  *** Ex.: "" - if we use current datagrid folder
  76.   $exporting_option = false;
  77.   $exporting_directory = "";              
  78.   $dgrid->AllowExporting($exporting_option, $exporting_directory);
  79.   $exporting_types = array("excel"=>"true", "pdf"=>"true", "xml"=>"true");
  80.   $dgrid->AllowExportingTypes($exporting_types);
  81.  
  82. ## +---------------------------------------------------------------------------+
  83. ## | 4. Sorting & Paging Settings:                                             |
  84. ## +---------------------------------------------------------------------------+
  85. ##  *** set sorting option: true(default) or false
  86.   $sorting_option = true;
  87.   $dgrid->AllowSorting($sorting_option);              
  88. ##  *** set paging option: true(default) or false
  89.  $paging_option = true;
  90.   $rows_numeration = false;
  91.   $numeration_sign = "N #";
  92.   $dropdown_paging = true;
  93.   $dgrid->AllowPaging($paging_option, $rows_numeration, $numeration_sign, $dropdown_paging);
  94. ##  *** set paging settings
  95.   $bottom_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", "page_size"=>true, "page_size_align"=>"right");
  96.   $top_paging = array();
  97.   $pages_array = array("2"=>"2", "5"=>"5", "7"=>"7", "10"=>"10", "15"=>"15", "25"=>"25");
  98.   $default_page_size = 7;
  99.   $paging_arrows = array("first"=>"|&lt;&lt;", "previous"=>"&lt;&lt;", "next"=>"&gt;&gt;", "last"=>"&gt;&gt;|");
  100.   $dgrid->SetPagingSettings($bottom_paging, $top_paging, $pages_array, $default_page_size, $paging_arrows);
  101.  
  102. ## +---------------------------------------------------------------------------+
  103. ## | 5. Filter Settings:                                                       |
  104. ## +---------------------------------------------------------------------------+
  105. ##  *** set filtering option: true or false(default)
  106.   $filtering_option = true;
  107.   $show_search_type = true;
  108.   $dgrid->AllowFiltering($filtering_option, $show_search_type);
  109. ##  *** set additional filtering settings
  110. ##  *** tips: use "," (comma) if you want to make search by some words, for ex.: hello, bye, hi
  111.  $filtering_fields = array(
  112.     "Name"       =>array("type"=>"textbox", "table"=>"demo_presidents", "field"=>"name", "show_operator"=>"false", "default_operator"=>"like%", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"80px", "on_js_event"=>""),
  113.     "Birth From" =>array("type"=>"calendar", "field_type"=>"from", "calendar_type"=>"floating", "date_format"=>"date", "table"=>"demo_presidents", "field"=>"birth_date", "show_operator"=>"false", "default_operator"=>">=", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"70px", "on_js_event"=>""),
  114.     "To"         =>array("type"=>"calendar", "field_type"=>"to", "calendar_type"=>"floating", "date_format"=>"date", "table"=>"demo_presidents", "field"=>"birth_date", "show_operator"=>"false", "default_operator"=>"<=", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"70px", "on_js_event"=>""),
  115.     "Status"     =>array("type"=>"enum", "table"=>"demo_presidents", "field"=>"status", "field_view"=>"", "filter_condition"=>"", "order"=>"ASC", "source"=>"self", "view_type"=>"radiobutton", "condition"=>"", "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"", "multiple"=>"false", "multiple_size"=>"4", "on_js_event"=>""),
  116.   );
  117.   $dgrid->SetFieldsFiltering($filtering_fields);
  118.  
  119. ## +---------------------------------------------------------------------------+
  120. ## | 6. View Mode Settings:                                                    |
  121. ## +---------------------------------------------------------------------------+
  122. ##  *** set view mode table properties
  123.   $vm_table_properties = array("width"=>"90%");
  124.   $dgrid->SetViewModeTableProperties($vm_table_properties);  
  125. ##  *** set columns in view mode
  126. ##  ***      "barchart" : number format in SELECT SQL must be equal with number format in max_value
  127.   $vm_colimns = array(
  128.     "name"       =>array("header"=>"Name",        "type"=>"label", "align"=>"left",  "wrap"=>"wrap",   "text_length"=>"20", "case"=>"normal"),
  129.     "country_name" =>array("header"=>"Country",   "type"=>"label", "align"=>"center",  "wrap"=>"nowrap", "text_length"=>"30", "case"=>"normal"),
  130.     "birth_date" =>array("header"=>"Birth Date",  "type"=>"label", "align"=>"center",  "header_tooltip"=>"Shows the birth date", "wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal"),
  131.     "status"     =>array("header"=>"Status",      "type"=>"label", "align"=>"center",  "header_tooltip"=>"Shows the current status", "wrap"=>"nowrap", "text_length"=>"30", "case"=>"normal"),
  132.     "favorite_color" =>array("header"=>"Favorite Color",       "type"=>"color",     "align"=>"center", "width"=>"", "wrap"=>"wrap|nowrap", "text_length"=>"-1", "tooltip"=>"false", "tooltip_type"=>"floating|simple", "case"=>"normal", "sort_type"=>"string|numeric", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "view_type"=>"image"),
  133.     "is_active"  =>array("header"=>"Active",      "type"=>"checkbox",  "align"=>"center", "width"=>"", "wrap"=>"nowrap", "sort_type"=>"numeric", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "true_value"=>1, "false_value"=>0),
  134.     "rating"  =>array("header"=>"Rating",      "type"=>"label",  "align"=>"center", "width"=>"", "wrap"=>"nowrap", "sort_type"=>"numeric", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "movable"=>true, ),
  135.   );
  136.   $dgrid->SetColumnsInViewMode($vm_colimns);
  137.  
  138. ## +---------------------------------------------------------------------------+
  139. ## | 7. Add/Edit/Details Mode Settings:                                        |
  140. ## +---------------------------------------------------------------------------+
  141. ##  *** set add/edit mode table properties
  142.   $em_table_properties = array("width"=>"75%");
  143.   $dgrid->SetEditModeTableProperties($em_table_properties);
  144. ##  *** set details mode table properties
  145.   $dm_table_properties = array("width"=>"60%");
  146.   $dgrid->SetDetailsModeTableProperties($dm_table_properties);
  147. ##  ***  set settings for add/edit/details modes
  148.   $table_name  = "demo_presidents";
  149.   $primary_key = "id";
  150.   $condition   = "";
  151.   $dgrid->SetTableEdit($table_name, $primary_key, $condition);
  152. ##  *** set columns in edit mode  
  153.   $em_columns = array(
  154.     "name"        =>array("header"=>"Name",       "type"=>"textbox",  "width"=>"140px", "req_type"=>"rt", "title"=>"Name"),
  155.     "birth_date"  =>array("header"=>"Birth Date", "type"=>"date",     "req_type"=>"rt", "width"=>"80px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "calendar_type"=>"floating"),
  156.     "status"      =>array("header"=>"Status",     "type"=>"enum",     "req_type"=>"st", "width"=>"210px", "title"=>"Status", "readonly"=>false, "maxlength"=>"-1", "default"=>"", "unique"=>false, "unique_condition"=>"", "on_js_event"=>"", "source"=>"self", "view_type"=>"dropdownlist"),
  157.     "country_id"  =>array("header"=>"Country",    "type"=>"textbox",  "width"=>"160px", "req_type"=>"ri", "title"=>"Country", "readonly"=>true),      
  158.     "favorite_color" =>array("header"=>"Favorite Color",      "type"=>"color",    "req_type"=>"rt", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "view_type"=>"dropdownlist", "save_format"=>"hexcodes"),
  159.     "is_active"   =>array("header"=>"Active",     "type"=>"checkbox", "req_type"=>"si", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "true_value"=>1, "false_value"=>0),
  160.   );
  161.   $dgrid->SetColumnsInEditMode($em_columns);
  162. ##  *** set foreign keys for add/edit/details modes (if there are linked tables)
  163.   $foreign_keys = array(
  164.     "country_id"=>array("table"=>"demo_countries", "field_key"=>"id", "field_name"=>"name", "view_type"=>"dropdownlist", "radiobuttons_alignment"=>"horizontal", "condition"=>"", "order_by_field"=>"", "order_type"=>"ASC", "on_js_event"=>""),
  165.   );
  166.   $dgrid->SetForeignKeysEdit($foreign_keys);
  167.  
  168. ## +---------------------------------------------------------------------------+
  169. ## | 8. Bind the DataGrid:                                                     |
  170. ## +---------------------------------------------------------------------------+
  171. ##  *** bind the DataGrid and draw it on the screen
  172. ## call of this method between HTML &lg;HEAD&gt; tags
  173.   $dgrid->WriteCssClass();
  174.   $dgrid->Bind();        
  175.   ob_end_flush();
  176.  
  177. ################################################################################  
  178.  
  179. ?>