Original Source
https://pdocrud.com/demo/pages/table-col-string-formatting
https://pdocrud.com/demo/pages/table-col-formula-formatting
https://pdocrud.com/demo/pages/table-col-more-formatting
https://pdocrud.com/demo/pages/replace-col-value
https://pdocrud.com/demo/pages/hide-column
https://pdocrud.com/demo/pages/specific-column
https://pdocrud.com/demo/pages/col-tooltip
https://pdocrud.com/demo/pages/col-data-type
https://pdocrud.com/demo/pages/portfolio
https://pdocrud.com/demo/pages/field-formula
https://pdocrud.com/demo/pages/sum-per-page
https://pdocrud.com/demo/pages/table-formatting
https://pdocrud.com/demo/pages/calculate-field-based-on-another-field-dynamically
String Formatting
You can format table column using various string functions. For example you can make any table column content to uppercase, first letter uppercase, lowercase or you can add prefix, suffix etc.
$pdocrud = new PDOCrud();
//Add prefix (e.g. Mr.) in first name
$pdocrud->tableColFormatting("first_name", "string",array("type" =>"prefix","str"=>"Mr. "));
//make firstname upper case
$pdocrud->tableColFormatting("first_name", "string",array("type" =>"uppercase"));
//make lastname lower case
$pdocrud->tableColFormatting("last_name", "string",array("type" =>"lowercase"));
//Add suffix
$pdocrud->tableColFormatting("Address", "string",array("type" =>"suffix","str"=>" "));
echo $pdocrud->dbTable("employee")->render();
Formula Based Formatting
You can format table column using various formula functions. For example you can make any table column content to uppercase, first letter uppercase, lowercase or you can add prefix, suffix etc.
$pdocrud = new PDOCrud();
// format field to 2 decimal point
$pdocrud->tableColFormatting("tax", "formula",array("type" =>"round","decimalpoint"=>2));
// format field to 2 decimal point
$pdocrud->tableColFormatting("product_discount", "formula",array("type" =>"number_format","decimalpoint"=>2));
// get ceil value of field
$pdocrud->tableColFormatting("product_price", "formula",array("type" =>"ceil"));
//get floor value of field
$pdocrud->tableColFormatting("product_sell_price", "formula",array("type" =>"floor"));
$pdocrud->crudTableCol(array("product_name", "product_price","product_sell_price","tax","product_discount"));
echo $pdocrud->dbTable("products")->render();
Table Formatting
$pdocrud->tableColFormatting("product_name", "string",array("type" =>"uppercase"));
$pdocrud->tableColFormatting("product_description", "string",array("type" =>"lowercase"));
$pdocrud->tableColFormatting("product_image", "image");
$pdocrud->tableColFormatting("product_description", "dialog", array("length"=>4,"showreadmore"=>true));
$pdocrud->tableColFormatting("product_rating", "html",array("type" =>"html","str"=>"{col-name}"));
$pdocrud->tableColFormatting("discount", "formula",array("type" =>"percentage"));
$pdocrud->tableColFormatting("qyt_available", "formula",array("type" =>"number_format","decimalpoint"=>2));
$pdocrud->tableColAddition("Total", "sum",array("product_price","product_sell_price"));
$pdocrud->tableColFormatting("qyt_available", "formula",array("type" =>"round","decimalpoint"=>2));
$pdocrud->tableColFormatting("product_price", "formula",array("type" =>"ceil"));
$pdocrud->tableColFormatting("product_sell_price", "formula",array("type" =>"floor"));
$pdocrud->tableDataFormatting("col", "style", array("product_price",">", "200"),array("background:#000","color:#ff0"));
echo $pdocrud->dbTable("producttable")->render();
More Formatting Options
You can format table column for date, image, html,read more, description and conditional logic formatting and many more other options. date format allows you to pass any standard php date formatting string and table will be formatted according to that. image format makes column value as image tag. long description can be made short with some character limit. conditional logic like if product price is greater than 200 than make that background highlight. Similar way view form formatting can be done. Click here to check example of view form formatting options.
$pdocrud = new PDOCrud();
$pdocrud->crudTableCol(array("product_line","product_image","product_name","product_description","product_price"));
// date formatting (pass date format)
$pdocrud->tableColFormatting("added_date", "date",array("format" =>"m-d-Y"));
// convert to any html like url or any html with actual value passed as {col-name}
$pdocrud->tableColFormatting("product_line", "html",array("type" =>"html", "str"=>"{col-name}"));
$pdocrud->tableColFormatting("product_name", "html",array("type" =>"html", "str"=>"{col-name}"));
//convert to image column
$pdocrud->tableColFormatting("product_image", "image");
//convert long column text to small text with read more
$pdocrud->tableColFormatting("product_description", "readmore", array("length"=>4,"showreadmore"=>true));//not showing read more
//conditional logic for formatting table colums - e.g apply css style for cells with product price>200
$pdocrud->tableDataFormatting("col", "style", array("product_price",">", "55"),array("background:#f00","color:#ff0"));
$pdocrud->tableDataFormatting("row", "style", array("product_price",">", "55"),array("background:#000","color:#ff0"));
echo $pdocrud->dbTable("products")->render();
Replace Column Value
Many times, we need to display different value of column than the value saved for example we save status as 0,1 and display as "Approved" "Not Approved". This can be easily achieved using the PDOCrud using tableColFormatting function and replacing the value of column.
$pdocrud = new PDOCrud();
//column_name, replace, array of value and replace value array (you can also write in one statement, rather than two)
$pdocrud->tableColFormatting("order_status", "replace",array("Completed" =>""));
$pdocrud->tableColFormatting("order_status", "replace",array("Pending" =>""));
echo $pdocrud->dbTable("orders")->render();
Hide Particular Column
$pdocrud = new PDOCrud();
$pdocrud->crudRemoveCol(array("user_id"));
echo $pdocrud->dbTable("users")->render();
Show Specific Columns and Rename Column
$pdocrud = new PDOCrud();
$pdocrud->colRename("first_name", "client name");
$pdocrud->crudTableCol(array("first_name","last_name"));
echo $pdocrud->dbTable("users")->render();
Column Tooltip
$pdocrud = new PDOCrud();
$pdocrud->crudColTooltip("first_name", "First name of user!");
echo $pdocrud->dbTable("users")->render();
Change Column Data Type
$pdocrud = new PDOCrud();
$pdocrud->crudTableCol(array("product_name","product_line","product_image","product_url"));
//set product_image column to type image and set width and height (you can also set any attribute like style, class)
$pdocrud->tableColFormatting("product_image", "image",array("width" =>"100px","height"=>'100px'));
//set product_url column to type url and set it's text (you can also set any attribute like style, class)
$pdocrud->tableColFormatting("product_url", "url",array("text" =>"View product","target"=>'_blank'));
echo $pdocrud->dbTable("products")->render();
Portfolio
$pdocrud = new PDOCrud();
$pdocrud->crudTableCol(array("product_image","product_name","product_price"));
$pdocrud->setPortfolioColumn(4);// set no. of columns in one row. Possible values are 1,2,3,4,6
$pdocrud->tableColFormatting("product_image", "image");
echo $pdocrud->dbTable("products")->render();
Field Formula
You can apply basic formula to change value of the field before insert/update operation dynamically. It can be done using the callback function also but this function is added to provide more convenient way to do same. It also helps to set the value to NULL for empty values to help to enter data in case of database restriction for e.g. saving NULL for datetime for empty values.
$pdocrud = new PDOCrud();
// format field to 2 decimal point
$pdocrud->fieldFormula("tax", "formula",array("type" =>"round","decimalpoint"=>2));
// format field to 2 decimal point
$pdocrud->fieldFormula("product_discount", "formula",array("type" =>"number_format","decimalpoint"=>2));
// get ceil value of field
$pdocrud->fieldFormula("product_price", "formula",array("type" =>"ceil"));
//get floor value of field
$pdocrud->fieldFormula("product_sell_price", "formula",array("type" =>"floor"));
//string type - adding prefix of sku always and checking for duplicate i.e. string already have sku or not
$pdocrud->fieldFormula("product_id","string",array("type" =>"prefix","str"=>"SKU_","duplicate"=>true));
//make product name first letter upper case
$pdocrud->fieldFormula("product_name", "string",array("type" =>"uppercase"));
//setting value to NULL if empty
$pdocrud->fieldFormula("added_Date", "string",array("type" =>"null"));
echo $pdocrud->dbTable("products")->render();
Sum Per Page
You can use sum per page function to display sum of any columns values of that page and sum total function to display grand total. Please note that these function display values dynamically on crud table only.
$pdocrud = new PDOCrud();
$pdocrud->crudTableCol(array("product_id","product_name","product_price","product_sell_price"));
$pdocrud->colSumPerPage("product_price");
$pdocrud->colSumPerPage("product_sell_price");
$pdocrud->colSumTotal("product_sell_price");
echo $pdocrud->dbTable("products")->render();
Calculate Field Based on Other Field
If you want to calculate a field value based on the other field's value then you can easily do using the formFieldValue() function. You can apply basic math function e.g addition, subtraction, multiplication or division to generate the field value based on other field's value. For example, if you want to save the total field as multiplicate of qty and rate field and don't want to show this total field to user then you can easily acheive this using this function.
Please note that you need to hide that field from form that is being calculated. Also, math function can be applied to two fields only. It doesn't work with complex functions currently. We will further improve this function in next versions.
In the below example, we are dynamically calculating sale_price field based on the price field. We are adding 24 to price field to generate sale_price field.
$pdocrud = new PDOCrud();
$pdocrud->formRemoveFields(array("sale_price"));
/**
* Set a value of field
* @param string $fieldName field name to renamed
* @param string $value value of the field
* @param bool $fieldHidden Wheter that field is hidden or not
* @param string $tablename Tablename = Required only when using join table field name
* return object Object of class
*/
$pdocrud->formFieldValue("sale_price", "{price}+24", true);
//apply formula
$pdocrud->fieldFormula("sale_price", "expression",array("type" =>"math_expression"));
echo $pdocrud->dbTable("book")->render("insertform"); // call render function