Page 1 of 1
Need a little PHP help
Posted: Fri 05 Nov 2021 9:48 pm
by Mapantz
Disclaimer; The code is probably terrible.
I'm trying to get the percentage of two values, as follows;
Code: Select all
while ($row = $result->fetch_array()) {
$title[] = (float)$row[0];
$valMissed[] = (float)$row[1];
$valError[] = (float)$row[2];
$valRecorded[] = (float)$row[3];
$valTot = $row[3] - $row[1];
$valSuccess[] = $valTot / $row[3] * 100;
}
$ret = array(($title[0] + 6000) * 1000, $valMissed, $valError, $valRecorded, $valSuccess);
$row[3] = 33329
$row[1] = 345
$valTot = 32984
On a calculator:
32984 / 33329 * 100 = 98.96 %
When I pull $valSuccess in highcharts (Javascript) it is returning 99.9%
It seems to be adding on 1% from somewhere..

Re: Need a little PHP help
Posted: Sat 06 Nov 2021 12:29 am
by beteljuice
You haven't floated the $row values and - (minus) won't force them to numeric.
Re: Need a little PHP help
Posted: Sat 06 Nov 2021 12:44 am
by Mapantz
I didn't think they were needed as it still produces the same result

Re: Need a little PHP help
Posted: Sat 06 Nov 2021 1:43 am
by beteljuice
All depends on if the array value is a string or not ..
When I pull $valSuccess in highcharts (Javascript)
Silly question ... you have checked the php value that is being used ?
Re: Need a little PHP help
Posted: Sat 06 Nov 2021 2:43 am
by Mapantz
beteljuice wrote: ↑Sat 06 Nov 2021 1:43 am
All depends on if the array value is a string or not ..
When I pull $valSuccess in highcharts (Javascript)
Silly question ... you have checked the php value that is being used ?
https://warehamwx.co.uk/utils/historicStats.php
Re: Need a little PHP help
Posted: Sat 06 Nov 2021 11:43 am
by beteljuice
Works correctly on my 'old' php ...
double-check you have:
Code: Select all
$valTot = (float)$row[3] - (float)$row[1];
$valSuccess = $valTot / (float)$row[3] * 100;
BTW - I don't know what you are checking, but are you sure total is recorded - missed and not recorded + missed ?
Re: Need a little PHP help
Posted: Sat 06 Nov 2021 12:11 pm
by Mapantz
beteljuice wrote: ↑Sat 06 Nov 2021 11:43 am
Works correctly on my 'old' php ...
double-check you have:
Code: Select all
$valTot = (float)$row[3] - (float)$row[1];
$valSuccess = $valTot / (float)$row[3] * 100;
BTW - I don't know what you are checking, but are you sure total is recorded - missed and not recorded + missed ?
$valSuccess = $valTot / (float)$row[3] * 100;
doesn't work for me
$valSuccess[] = $valTot / (float)$row[3] * 100;
Does.
Code: Select all
[1635903600000,[46,97,63],[345,425,364],[33329,33188,33188],[99.8619820576675,99.70772568398216,99.81017235145234]]
If you manually do the math;
33329 - 345 = 32984
32984 / 33329 * 100 = 98.96486543250623
33188 - 425 = 32763
32763 / 33329 * 100 = 98.30177923130007
33188 - 364 = 32824
32824 / 33188 * 100 = 98.90321803061347
It's probably something very simple, but where does the extra (almost 1%) come from?
Re: Need a little PHP help
Posted: Sat 06 Nov 2021 1:33 pm
by Mapantz
I figured it out!
It wasn't the code, It was Maspantz..
I had two columns the wrong way around in my database.

Re: Need a little PHP help *Solved*
Posted: Sat 06 Nov 2021 1:36 pm
by Mapantz
Mapantz wrote: ↑Fri 05 Nov 2021 9:48 pm
Disclaimer; The code is probably terrible.
I'm trying to get the percentage of two values, as follows;
Code: Select all
while ($row = $result->fetch_array()) {
$title[] = (float)$row[0];
$valMissed[] = (float)$row[1];
$valError[] = (float)$row[2];
$valRecorded[] = (float)$row[3];
$valTot = $row[3] - $row[1];
$valSuccess[] = $valTot / $row[3] * 100;
}
$ret = array(($title[0] + 6000) * 1000, $valMissed, $valError, $valRecorded, $valSuccess);
$row[3] = 33329
$row[1] = 345
$valTot = 32984
On a calculator:
32984 / 33329 * 100 = 98.96 %
When I pull $valSuccess in highcharts (Javascript) it is returning 99.9%
It seems to be adding on 1% from somewhere..