Live Examples

Example 15: Display Images

For data that stores a path to an image, you can use phpGrid method set_col_img() to display the actual image. The third parameter is used to set additional style to the image, such as border and width.

$dg = new C_DataGrid($hostName, $userName, $password, $dbName);
 
$dg -> set_gridpath     ("include/");
$dg -> set_sql          ("SELECT * FROM Employees");
$dg -> set_sql_table    ("Employees");
$dg -> set_sql_key      ("EmployeeId");
 
// change column titles
$dg -> set_col_title    ("EmployeeId", "Employee ID");
$dg -> set_col_title    ("LastName", "Last Name");
$dg -> set_col_title    ("FirstName", "First Name");
 
// set background and mouse over color
$dg -> set_alt_bgcolor  ("#ffcc99, #ffccdd");
$dg -> set_onmouseover  ("yellow");
 
// hide a column
$dg -> set_col_hidden   ("Notes");
 
// display URL as hyperlink
$dg -> set_col_link    ("ReportsTo", 
                        "/query.php?EmployeeId=", 
                        "EmployeeId", 
                        "target='_new'"); 
 
// set page size
$dg -> set_page_size   (20);
 
// make the datagrid editable
$dg -> set_allow_actions(true);
 
// set data administrative level
// "V" means View, "E" means Edit, and "D" means Delete.
$dg -> set_action_type ("VE");   // view and edit only
 
// make BirthDate and HomePhone read only
$dg -> set_fields_readonly("LastName, HomePhone");
 
// sum up columns
$dg -> set_col_sum_enabled(true);
$dg -> set_col_sum      ("Salary");
$dg -> set_col_sum      ("Age");
 
// add Checkbox HTML control to status
$dg -> add_control("Status",                    
		CHECKBOX, 
		array("fulltime"=>"Full Time", 
		      "parttime"=>"Part Time",  
		      "contract"=>"Contract"), 
		      ",");
 
// turn on inline-editing with Ajax enabled
// $dg -> set_inlineedit_enabled(true, true);
 
// condition-based output
$dg -> set_col_txt ("IsSeniorEmployee", 
                    " < 1", "YES", "NO", 
                    false);
 
// display image
$dg -> set_col_img ("PhotoPath", "", "width:50px;");
  $dg -> display();