PHP Generator Feature Demo
  • Themes
    • Default
    • Default compact
    • Cerulean
    • Cerulean compact
    • Cosmo
    • Cyborg
    • Darkly
    • Darkly compact
    • Facebook
    • Flatly
    • Journal
    • Lumen
    • Paper
    • Readable
    • Sandstone
    • Simplex
    • Slate
    • Slate compact
    • Spacelab
    • Superhero
    • Superhero compact
    • United
    • United compact
    • Yeti
    • Yeti compact
  • English
    • German
    • Brazilian
    • Czech
    • Danish
    • Spanish
    • Finnish
    • French
    • Hungarian
    • Italian
    • Dutch
    • Polish
    • Russian
    • Swedish
    • Slovak
    • Slovenian
    • Serbian
    • Turkish
    • Arabic
  • Learn more
    • PHP Generator Feature Demo
    • About this demo
    • Download project file for this demo
    • Check out other PHP Generator demos
    • NBA Database Demo
    • Security Demo
    • MySQL Schema Browser Demo
    • Learn more about our products
    • Try PHP Generator for free
    • SQL Maestro Group website
    • Follow us
    • Facebook
    • Twitter
    • YouTube
  • Recently added/updated
    • Grid Toolbar
    • Tabbed Forms
    • Stepped Area Chart
    • Candlestick Chart
    • Histogram Chart
    • Bubble Chart
    • Timeline Chart
    • Gantt Chart
    • Scatter Chart
    • Tree Map Chart
    • Export options
    • Invoice generation
    • Inline Button
    • HTML Wysiwyg
    • Watermarks
  • Data Grid
    • Table View
    • Card View
    • Autohiding Columns
    • Totals
    • Grid Header
    • Column Grouping
    • Record Comparison
    • Unicode Support
    • RSS
    • Custom Drawing
    • Custom Table Template
    • Custom Card Template
    • Custom Toolbar Template
    • Custom View Form
  • Grid Columns
    • Column Types
    • Text Truncating
    • Column Fixed Width
    • Formatting
    • Null Label
    • Hyperlinks
    • Html Display
    • Lookup Data View
    • Custom Rendering
  • Grid Options
    • Fixed Grid Width
    • Fixed Column Header
    • Bordered Table
    • Condensed Table
    • Line Numbers
    • Control Buttons Position
  • Data Sources
    • Table
    • View
    • Non-Updatable Query
    • Updatable Query
    • Updatable View
  • Master-Detail Views
    • Master/Detail Basics
    • Multiple Details
    • Nested Details
    • Card Mode Details
  • Data Input Forms
    • Separate Page
    • Modal Window
    • Inline Editing
    • Form Layouts
    • Tabbed Forms
    • Custom Separate Page
    • Custom Modal Dialog
    • Custom Inline Form
    • Wizard Form
    • Adding multiple records
    • Editors Overview
    • Validation
    • Client-side API
    • Dependent Lookups
    • On-the-Fly Adding
    • Quick Edit
    • Multi Edit
    • Custom Default Values
  • Editors
    • Text
    • Autocomplete
    • Text Area
    • HTML Wysiwyg
    • Spin & Range
    • Upload To Folder
    • Radio Group
    • Combobox
    • Dynamic Combobox
    • Cascading Combobox
    • Dynamic Cascading Combobox
    • Multiple Select
    • Checkbox Group
    • Common Properties
  • Data Filtering
    • Quick Filter
    • Filter Builder
    • Column Filter
    • Preliminary Filter
    • Selection Filters
    • Custom Filter - 1
    • Custom Filter - 2
  • Sorting
    • Sort By Click
    • Sort By Dialog
    • Default Sort Order
  • Partitioning
    • Range
    • List
    • Custom
  • Exporting & Printing
    • Grid
    • Single Record
    • Export Options
    • Custom Grid
    • Custom Single Record
    • User Defined Styles
  • Image Management
    • Image Galleries
    • Thumbnails
    • Linked Images
    • Multi Upload - 1
    • Multi Upload - 2
    • Watermarks
  • Charts
    • Pie Chart
    • Column Chart
    • Bar Chart
    • Line Chart
    • Area Chart
    • Geo Charts
    • Stepped Area
    • Candlestick
    • Histogram
    • Bubble
    • Timeline
    • Gantt
    • Scatter
    • Tree Map
    • Multiple Charts
    • Chart Placement
    • Customizing Charts
    • Dashboard
  • Many-to-Many Relations
    • Classic Junction Table
    • Handling Extra Columns
  • Fine-tuning & Tweaking
    • Custom Icons
    • Custom Theme
    • Custom Editor
    • Page Embedding
    • Ajax-based Validation
    • Color Themes
    • Barcodes Generation
    • Inline Button
    • Conditional Details
    • Using Ajax in Forms
  • Emailing
    • Basic Usage
    • Advanced Usage
  • Calculated Columns
    • Example - 1
    • Example - 2
  • Custom Templates
    • Grid View
    • Card View
    • Grid Toolbar
    • Single Record View
    • Separate Page Editing
    • Modal Dialog Editing
    • Inline Form Editing
    • Wizard Form
    • Custom Filter - 1
    • Custom Filter - 2
    • Export & Print: Data Grid
    • Invoice generation
  1. Calculated Columns
  2. Calculated Columns.Example - 2
    • Example - 1

Calculated Columns.Example - 2

Page settings

Appearance

Here you can select the number of cards to be placed in a row for each of supported screen resolutions. Your current resolution is highlighted in this way.

Cancel Save changes
×

This is a more complex example of using calculated columns. To compute the value of the Amount (in USD) column, an SQL query to a table that stores daily currency exchange rates is performed.

Technical details are as follows (click a link below to see the appropriate code):

  • OnBeforePageExecute event handler code. It allows you to define a PHP function to be used in the events below
  • OnCalculateFields event handler code
  • OnCalculateControlValues event handler code. As this is a client side event, the value of a PHP function can be obtained with an Ajax request that is handled within the OnPreparePage event.
Add new
Refresh
Export
  • Export to Pdf
  • Export to Excel
  • Export to Word
  • Export to Xml
  • Export to Csv
Print
  • Print current page
  • Print all pages
    function getAmountInUsd($amount, $currency, $paymentDate, $connection) {
        if ($currency === 'USD') {
            return '$' . number_format($amount, 2);
        }
        // calculate the currency field name in table usd_exchange_rate
        // example of such field names: eur_rate, aud_rate
        $currencyFieldName = strtolower($currency) . '_rate';
        // Retrieve the usd exchange rate on payment date
        $sql = sprintf("SELECT %s FROM usd_exchange_rate WHERE exchange_date = '%s'", 
            $currencyFieldName, $paymentDate);
        $currencyRate = $connection->ExecScalarSQL($sql);
        if ($currencyRate) {
            return '$' . number_format(($amount / $currencyRate), 2);
        } else {
            return 'There is no USD exchange rate data on a payment date';
        }
    }
        protected function doCalculateFields($rowData, $fieldName, &$value)
        {
            if ($fieldName == 'amount_in_usd') {
                $value = getAmountInUsd($rowData['amount'], $rowData['currency'], $rowData['payment_date'], $this->GetConnection());
            }
        }

// OnCalculateControlValues event body
 if ((editors['amount'].getValue() >= 0) && editors['currency'].getValue() && editors['payment_date'].getValue()) {
                $.ajax({
                    url: window.location.href,
                    data: {
                        amount: editors['amount'].getValue(),
                        currency: editors['currency'].getValue(), 
                        paymentDate: editors['payment_date'].getValue()
                    },
                    success: function (result) {
                        editors['amount_in_usd'].setValue(result);                
                    },
                    error: function () {
                        editors['amount_in_usd'].setValue('');        
                    }
                });                   
            } else {
                editors['amount_in_usd'].setValue('');
            }

        protected function DoPrepare() {
            // handling Ajax request
            if (GetApplication()->isGetValueSet('amount') && 
                    GetApplication()->isGetValueSet('currency') && 
                        GetApplication()->isGetValueSet('paymentDate')) {
                $value = 
                    getAmountInUsd(
                        GetApplication()->GetGETValue('amount'),        
                        GetApplication()->GetGETValue('currency'),
                        GetApplication()->GetGETValue('paymentDate'),
                        $this->GetConnection()
                    );                    
                echo $value;
                exit;
            }
            
            // auxiliary code
            $this->setDescription(file_get_contents("external_data/doc/calculated_columns2.html"));
            
            $this->setDetailedDescription(
                '<div id="on-before-page-execute" class="event-code">' .
                    extractFunctionCode('getAmountInUsd') . '</div>' .
                '<div id="on-get-calculated-field-value" class="event-code">' .
                    extractMethodCode($this, 'doCalculateFields') . '</div>' .
                '<div id="on-get-form-editor-calculated-value" class="event-code">' .
                    extractClientEventCode($this, 'OnCalculateControlValues') . '</div>' .
                '<div id="on-prepare-page" class="event-code">' .
                    extractMethodCode($this, 'doPrepare') . '</div>'
            );
        }

 
Actions Customer Amount Currency Payment Date Amount (in USD)
Brown 1,250.00 EUR 2015-10-12 $1,421.42
Madson 2,325.00 CAD 2015-10-20 $1,787.91
Goodridge 780.00 GBP 2015-11-02 $1,204.45
Goodridge 450.00 GBP 2015-11-16 $684.41
Kerber 450.00 CHF 2015-11-26 $439.88
Jones 850.00 AUD 2015-11-30 $612.52
Jones 490.00 AUD 2015-12-07 $358.00
Goodridge 300.00 USD 2015-12-10 $300.00
Smith 700.00 CHF 2015-12-25 $709.72
Brown 450.00 USD 2015-12-30 $450.00
Davis 485.00 EUR 2016-01-10 $530.17
Madson 530.00 CAD 2016-01-12 $372.30
Smith 450.00 USD 2016-01-15 $450.00
Jones 1,120.00 AUD 2016-01-26 $780.76
Moore 350.00 GBP 2016-01-29 $501.00
Jones 350.00 AUD 2016-01-29 $248.23
Goodridge 120.00 EUR 2016-02-02 $130.90
Smith 150.00 USD 2016-02-04 $150.00
Goodridge 120.00 AUD 2016-02-08 $85.07
Smith 800.00 CAD 2016-02-10 $575.71
Davis 650.00 CHF 2016-02-12 $666.87
Jones 1,200.00 AUD 2016-02-16 $857.08
Taylor 450.00 USD 2016-02-17 $450.00
Moore 600.00 CHF 2016-02-19 $604.96
There are no records to display

Multiple Sort

Column Order

Filter builder

About this demo

This demo application contains 100+ pages and illustrates most of features provided by PHP Generator. Hope it will help you to create even more powerful websites for your users.

You can download the demo project from our website and run the demo on your webserver as described in readme.txt.

Download PHP Generator Free Trial

Legend
New Recently added or significantly updated pages.
Pro Pages illustrating features available only in the Professional edition of PHP Generator.

© 2002- SQL Maestro Group. Follow us:

Created with PHP Generator for MySQL. Want to learn more? Download the demo project!

Change your password

Change password for user ''

Passwords do not match. Please try again.

Close Change password