Sample 2-8. Advanced PHP DG code.

    1. Modes: Add/Edit/Details/View.
    2. Customized layout in View Mode.

   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
  8. ##  *** (relatively to the current file)
  9. ##  *** RELATIVE PATH ONLY ***
  10.   define ("DATAGRID_DIR", "datagrid/");                     /* Ex.: "datagrid/" */
  11.   require_once(DATAGRID_DIR."datagrid.class.php");
  12.  
  13. ##  *** creating variables that we need for database connection
  14.    
  15.   $DB_USER="user";
  16.   $DB_PASS="password";      
  17.   $DB_HOST="host";
  18.   $DB_NAME="database";
  19.  
  20.   ob_start();
  21. ##  *** write down the primary key in the first place (MUST BE AUTO-INCREMENT NUMERIC!)
  22.  $sql = "SELECT
  23.            id,
  24.            model_name,
  25.            description,
  26.            image_thumb,
  27.            image,
  28.            price_from,
  29.            price_to,
  30.            submodels,
  31.            model_name as details_link
  32.        FROM demo_cars ";
  33. ##  *** set needed options and create a new class instance
  34.   $debug_mode = false;        /* display SQL statements while processing */    
  35.   $messaging = true;          /* display system messages on a screen */
  36.   $unique_prefix = "cr_";    /* prevent overlays - must be started with a letter */
  37.   $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix);
  38.  
  39. ##  *** set data source with needed options
  40.   $default_order = array("model_name"=>"ASC");
  41.   $dgrid->DataSource("PDO", "mysql", $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS, $sql, $default_order);              
  42.  
  43. ## +---------------------------------------------------------------------------+
  44. ## | 2. General Settings:                                                      |
  45. ## +---------------------------------------------------------------------------+
  46. ## +-- Layouts, Templates & CSS -----------------------------------------------+
  47. ##  *** set layouts: "0" - tabular(horizontal) - default, "1" - columnar(vertical), "2" - customized
  48. ##  *** use "view"=>"0" and "edit"=>"0" only if you work on the same tables
  49.   $layouts = array("view"=>"2", "edit"=>"1", "details"=>"1", "filter"=>"1");
  50.   $dgrid->SetLayouts($layouts);
  51.   /// *** $mode_template = array("header"=>"", "body"=>"", "footer"=>"");
  52.   $view_template = array("header"=>"", "body"=>"", "footer"=>"");
  53.   $view_template["header"] = "
  54.    <table style="margin-bottom:3px; BORDER-COLLAPSE: collapse; BORDER: #d0d0d0 1px solid; FONT: normal 12px Tahoma;" align="center" width="80%" border="0" cellspacing="2" cellpadding="3" bgcolor="#f3f3f3">
  55.    <tr>
  56.         <td colspan="4" height="10" nowrap="nowrap" align="left">[ADD]</td>
  57.         <td colspan="4" height="10" nowrap="nowrap" align="left">Sort By: @model_name@ | @price_from@</td>
  58.     </tr>
  59.     </table>";
  60.  $view_template["body"] = "    
  61.     <table style="margin-bottom:3px; BORDER-COLLAPSE:collapse; BORDER:#d0d0d0 1px solid; FONT:normal 12px Tahoma;" align="center" width="80%" border="0" cellspacing="2" cellpadding="3" bgcolor="#f3f3f3">
  62.     <tr><td colspan="4" height="10" nowrap="nowrap" align="left"></td></tr>
  63.     <tr>
  64.         <td width="105px" valign="top" align="center">
  65.             <a href="uploads/{image}" rel="lyteshow[cars]" title="">{image_thumb}</a><br />
  66.             [MULTIROW_CHECKBOX] [EDIT]
  67.         </td>
  68.         <td width="5px" nowrap="nowrap"></td>
  69.         <td valign="top">
  70.             <table width="100%" border="0" cellspacing="0" cellpadding="0">
  71.             <tr>
  72.                 <td>
  73.                     <table width="100%" border="0" cellspacing="0" cellpadding="0">
  74.                     <tr>
  75.                         <td width="235" height="20" valign="top" style="font-size:12px"><strong>{details_link}</strong></td>
  76.                         <td width="140" valign="top"><span style="color:#960000;">{price_from} - {price_to}</span></td>
  77.                         <td width="185" valign="top"><a class="x-gray_dg_a" href='javascript:alert("Blocked in Demo version!");'>EuroAutomobile</a></td>
  78.                     </tr>
  79.                     </table>
  80.                 </td>
  81.             </tr>
  82.             <tr>
  83.                 <td width="550">{description}</td>
  84.             </tr>
  85.             <tr><td height="3" nowrap="nowrap"></td></tr>
  86.             <tr>
  87.                 <td height="20">
  88.                     <table width="100%" border="0" cellspacing="0" cellpadding="0">
  89.                     <tr>
  90.                         <td width="68" valign="top" style="color:#666666">Submodel(s):&nbsp;</td>
  91.                         <td valign="top" style="color:#008a8a"> {submodels}</td>
  92.                     </tr>
  93.                     </table>
  94.                 </td>
  95.             </tr>
  96.             <tr><td height="20" style="font-style:italic">Updated:&nbsp; 20-Aug-2009</td></tr>
  97.             </table>
  98.         </td>
  99.         <td valign="top" width="150px" align="center">
  100.             N/A <br/>                
  101.             <a class="x-gray_dg_a" href=\"javascript:alert("Blocked in Demo version!");\">Rate it!</a><br/>                
  102.            <a class="x-gray_dg_a" href=\"javascript:alert("Blocked in Demo version!");\">EXPERT REVIEW</a>
  103.        </td>
  104.    </tr>
  105.    </table>";
  106.  
  107.   $dgrid->SetTemplates($view_template,"","");
  108. ##  *** set modes for operations ("type" => "link|button|image")
  109. ##  *** "view" - view mode | "edit" - add/edit/details modes
  110. ##  *** "byFieldValue"=>"fieldName" - make the field to be a link to edit mode page
  111.   $modes = array(
  112.      "add"        =>array("view"=>true, "edit"=>false, "type"=>"link", "show_add_button"=>"inside|outside"),
  113.      "edit"       =>array("view"=>true, "edit"=>true,  "type"=>"link", "byFieldValue"=>""),
  114.      "cancel"  =>array("view"=>true, "edit"=>true,  "type"=>"link"),
  115.      "details" =>array("view"=>true, "edit"=>false, "type"=>"link"),
  116.      "delete"  =>array("view"=>true, "edit"=>false,  "type"=>"image")
  117.   );
  118.   $dgrid->SetModes($modes);
  119. ##  *** set CSS class for datagrid
  120. ##  *** "default", "blue", "x-blue", "gray", "green" or "pink" or "empty" your own css file
  121.   $css_class = "x-gray";
  122.   $dgrid->SetCssClass($css_class);
  123. ##  *** set DataGrid caption
  124.   $dg_caption = "New Cars";
  125.   $dgrid->SetCaption($dg_caption);
  126.  
  127. ## +-- Multirow Operations ----------------------------------------------------+
  128. ##  *** allow multirow operations
  129.   $multirow_option = true;
  130.   $dgrid->AllowMultirowOperations($multirow_option);
  131.   $multirow_operations = array(
  132.     "edit"    => array("view"=>true),
  133.     "details" => array("view"=>true),
  134.     "delete"  => array("view"=>true)
  135.   );
  136.   $dgrid->SetMultirowOperations($multirow_operations);  
  137.  
  138. ## +---------------------------------------------------------------------------+
  139. ## | 4. Sorting & Paging Settings:                                             |
  140. ## +---------------------------------------------------------------------------+
  141. ##  *** set paging option: true(default) or false
  142.   $paging_option = true;
  143.   $rows_numeration = false;
  144.   $numeration_sign = "N #";
  145.   $dropdown_paging = false;
  146.   $dgrid->AllowPaging($paging_option, $rows_numeration, $numeration_sign, $dropdown_paging);
  147. ##  *** set paging settings
  148.   $bottom_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", " "=>true, "page_size_align"=>"right");
  149.   $top_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", "page_size"=>true, "page_size_align"=>"right");
  150.   $pages_array = array("2"=>"2", "5"=>"5", "10"=>"10", "25"=>"25", "50"=>"50", "100"=>"100", "250"=>"250", "500"=>"500", "1000"=>"1000");
  151.   $default_page_size = 5;
  152.   $paging_arrows = array("first"=>"|<<", "previous"=>"<<", "next"=>">>", "last"=>">>|");
  153.   $dgrid->SetPagingSettings($bottom_paging, $top_paging, $pages_array, $default_page_size, $paging_arrows);
  154.  
  155. ## +---------------------------------------------------------------------------+
  156. ## | 6. View Mode Settings:                                                    |
  157. ## +---------------------------------------------------------------------------+
  158. ##  *** set view mode table properties
  159.   $vm_table_properties = array("width"=>"80%");
  160.   $dgrid->SetViewModeTableProperties($vm_table_properties);  
  161. ##  *** set columns in view mode
  162. ##  ***      "barchart" : number format in SELECT SQL must be equal with number format in max_value
  163.   $fill_from_array = array("0"=>"Banned", "1"=>"Active", "2"=>"Closed", "3"=>"Removed"); /* as "value"=>"option" */
  164.   $vm_colimns = array(        
  165.     "details_link"=>array("header"=>"Details view", "type"=>"linktoview", "align"=>"left", "width"=>"X%|Xpx", "wrap"=>"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"=>""),
  166.     "image_thumb" =>array("header"=>"Thumbnail", "type"=>"image",      "align"=>"center", "width"=>"120px", "wrap"=>"nowrap", "text_length"=>"-1", "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"=>"100px", "image_height"=>"75px", "linkto"=>"", "magnify"=>"false", "magnify_type"=>"lightbox", "magnify_power"=>"2"),
  167.     "image"       =>array("header"=>"Image", "type"=>"data"),
  168.     "submodels"   =>array("header"=>"Submodels", "type"=>"data"),
  169.     "model_name"  =>array("header"=>"Model", "type"=>"label",      "align"=>"left", "width"=>"", "wrap"=>"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"=>""),
  170.     "description" =>array("header"=>"Desciption", "type"=>"label",      "align"=>"left", "width"=>"", "wrap"=>"wrap|nowrap", "text_length"=>"255", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal|upper|lower|camel", "summarize"=>"false", "sort_type"=>"string|numeric", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
  171.     "price_from"  =>array("header"=>"Price From", "type"=>"money",     "align"=>"right", "width"=>"", "wrap"=>"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"=>"", "sign"=>"$", "sign_place"=>"before", "decimal_places"=>"0", "dec_separator"=>".", "thousands_separator"=>","),
  172.     "price_to"    =>array("header"=>"Price to", "type"=>"money",     "align"=>"right", "width"=>"", "wrap"=>"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"=>"", "sign"=>"$", "sign_place"=>"before", "decimal_places"=>"0", "dec_separator"=>".", "thousands_separator"=>","),
  173.   );
  174.   $dgrid->SetColumnsInViewMode($vm_colimns);
  175.  
  176. ## +---------------------------------------------------------------------------+
  177. ## | 7. Add/Edit/Details Mode Settings:                                        |
  178. ## +---------------------------------------------------------------------------+
  179. ##  ***  set settings for add/edit/details modes
  180.   $table_name  = "demo_cars";
  181.   $primary_key = "id";
  182.   $condition   = "";
  183.   $dgrid->SetTableEdit($table_name, $primary_key, $condition);
  184. ##  *** set columns in edit mode  
  185.   $em_columns = array(
  186.     "model_name"  =>array("header"=>"Model Name", "type"=>"textbox",    "req_type"=>"rt", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
  187.     "description" =>array("header"=>"Desciption", "type"=>"textarea",   "req_type"=>"ry", "width"=>"430px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "edit_type"=>"simple", "resizable"=>"true", "rows"=>"3", "cols"=>"50"),
  188.     "submodels"   =>array("header"=>"Submodels", "type"=>"textbox",     "req_type"=>"sy", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
  189.     "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"=>"100KB", "image_width"=>"100px", "image_height"=>"75px", "resize_image"=>"true", "resize_width"=>"100px", "resize_height"=>"75px", "magnify"=>"false", "magnify_type"=>"popup|magnifier|lightbox", "magnify_power"=>"2", "file_name"=>"car_thumb_".((isset($_GET[$unique_prefix."mode"]) && ($_GET[$unique_prefix."mode"] == "add")) ? $dgrid->GetRandomString("10") : $dgrid->GetRandomString("10")), "host"=>"local|remote"),
  190.     "image"       =>array("header"=>"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"=>"300KB", "image_width"=>"100px", "image_height"=>"75px", "resize_image"=>"false", "resize_width"=>"100px", "resize_height"=>"75px", "magnify"=>"false", "magnify_type"=>"popup|magnifier|lightbox", "magnify_power"=>"2", "file_name"=>"car_".((isset($_GET[$unique_prefix."mode"]) && ($_GET[$unique_prefix."mode"] == "add")) ? $dgrid->GetRandomString("10") : $dgrid->GetRandomString("10")), "host"=>"local|remote"),
  191.     "price_from"  =>array("header"=>"Price From", "type"=>"money",      "req_type"=>"rf", "width"=>"80px",  "title"=>"", "readonly"=>"false", "maxlength"=>"9", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "sign"=>"$", "sign_place"=>"before", "decimal_places"=>"0", "dec_separator"=>".", "thousands_separator"=>","),
  192.     "price_to"    =>array("header"=>"Price To", "type"=>"money",      "req_type"=>"rf", "width"=>"80px",  "title"=>"", "readonly"=>"false", "maxlength"=>"9", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "sign"=>"$", "sign_place"=>"before", "decimal_places"=>"0", "dec_separator"=>".", "thousands_separator"=>","),
  193.   );
  194.   $dgrid->SetColumnsInEditMode($em_columns);
  195.  
  196. ## +---------------------------------------------------------------------------+
  197. ## | 8. Bind the DataGrid:                                                     |
  198. ## +---------------------------------------------------------------------------+
  199. ##  *** bind the DataGrid and draw it on the screen
  200.   $dgrid->Bind();        
  201.   ob_end_flush();
  202.  
  203. ################################################################################  
  204.  
  205. ?>