Sample 1-1. Simplest PHP DG code.

    1. All modes (Add/Edit/Details/Delete/View).
    2. Auto-Genereted columns.
Show Plain Text »
  1. <?php
  2.  
  3. ## +---------------------------------------------------------------------------+
  4. ## | 1. Creating & Calling:                                                    |
  5. ## +---------------------------------------------------------------------------+
  6. ##  *** only relative (virtual) path (to the current document)
  7. define ("DATAGRID_DIR", "datagrid/");                    
  8. require_once(DATAGRID_DIR."datagrid.class.php");
  9.  
  10. ##  *** creating variables that we need for database connection    
  11. $DB_USER = "db_user";
  12. $DB_PASS = "db_password";      
  13. $DB_HOST = "db_host";      
  14. $DB_NAME = "db_name";      
  15.  
  16. ##  *** set needed options
  17. $debug_mode = false;
  18. $messaging = true;
  19. $unique_prefix = "f_";  
  20. $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix);
  21.  
  22. ##  *** put a primary key on the first place
  23. $sql=" SELECT demo_countries.*,
  24. FORMAT(demo_countries.population, 0) as population,
  25. CASE WHEN demo_countries.is_democracy = 1 THEN 'Yes' ELSE 'No' END as is_democracy
  26. FROM demo_countries";
  27.  
  28. ##  *** set data source with needed options
  29. $default_order = array("name"=>"ASC");
  30. $dgrid->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);
  31. $dgrid->SetCaption("<b>Simplest ApPHP DataGrid</b>");
  32.  
  33. ## +---------------------------------------------------------------------------+
  34. ## | 6. View Mode Settings:                                                    |
  35. ## +---------------------------------------------------------------------------+
  36. ##  *** set columns in view mode
  37. $dgrid->SetAutoColumnsInViewMode(true);  
  38.  
  39. ## +---------------------------------------------------------------------------+
  40. ## | 7. Add/Edit/Details Mode settings:                                        |
  41. ## +---------------------------------------------------------------------------+
  42. ##  ***  set settings for edit/details mode
  43. $table_name = "demo_countries";
  44. $primary_key = "id";
  45. $condition = "";
  46. $dgrid->SetTableEdit($table_name, $primary_key, $condition);
  47. $dgrid->SetAutoColumnsInEditMode(true);
  48.  
  49. ## +---------------------------------------------------------------------------+
  50. ## | 8. Bind the DataGrid:                                                     |
  51. ## +---------------------------------------------------------------------------+
  52. ##  *** set debug mode & messaging options
  53. $dgrid->Bind();        
  54.  
  55. ?>