Hi there, in my query I have 2 fields, 1 is 'Payment', the other '% Amount', id there a way to calculate this and display in table?
EG: Payment | % Amount | Total Paid
100 | 30 | £70
Calculation – Payment*%Amount/100 = 70
Thank you
Please refer demos/appearance/calc-column.php
Step1: define virtual column on grid to show calculated data.
Step2: In on_data_display event handler, set it's value in loop.
You must have all source fields as part of your select query.
Thanks Abu, I am trying to check the demo but it doesn't load, says 'Unable to resolve the servers DNS address'.
Not sure how to define the virtual column as it does not exist in table?
Thank you
Hi Abu, I tried this but it returns 0 in the virtual column?
// virtual column
$col = array();
$col["title"] = "Paid";
$col["name"] = "col2-virtual";
$col["width"] = "200";
$col["editable"] = false;
$col["hidden"] = false;
$col["default"] = "{col2}";
$col["on_data_display"] = array("do_maths","");
function do_maths($data){
return $data["PropertyValue"] / $col["Cost"] *100;}
$cols[] = $col;
If I simplify it to simply deduct one from the other it returns the PropertyValue field only?
Thank you
Perhaps, a typo.
$col should be $data inside function.
on line: return $data["PropertyValue"] / $col["Cost"] *100;
Hi Abu, sorry could you expand on that one if you have time? Removing the } doesn't work, neither does moving it to $col?
$col["on_data_display"] = array("do_maths","");
function do_maths($data){
return $data["PropertyValue"] / $col["Cost"] *100;
$cols[] = $col;
Sorry, I xan find any demo example as it doesnt load on your site?
Sorry I mean to paste this instead, I tried this with no joy;
$col["on_data_display"] = array("do_maths","");
function do_maths($data){
return $col["PropertyValue"] / $col["Cost"] *100;}
$cols[] = $col;
Hi Abu, please disregard above, now I sort of have it working but not using the % maths which is odd as this works in another site of mine [not datagrid]
//This returns correct result
return $data ["PropertyValue"] – $data["Cost"];}
100 – 40 = 60
//This still doesn't return correct result
return $data ["PropertyValue"] / $data["Cost"] *100;}
100/40*100 = 250, correct calc should be 40
Any ideas?
Try using brackets.
return ( floatval($data["PropertyValue"]) / floatval($data["Cost"]) ) *100;
This can be checked independently on some php code and then used in grid.
Hiya Abu, I think its the maths function that may be wrong, I just copied if from my other php apps where it works fine but I have it now & its working great..
If it helps anyone going forward the working function is;
return $data ["PropertyValue"] * $data["Cost"]/100;}
Thanks again for your help.
Gary