Welcome to the Cumulus Support forum.

Latest Cumulus MX V3 release 3.28.6 (build 3283) - 21 March 2024

Cumulus MX V4 beta test release 4.0.0 (build 4017) - 17 March 2024

Legacy Cumulus 1 release v1.9.4 (build 1099) - 28 November 2014 (a patch is available for 1.9.4 build 1099 that extends the date range of drop-down menus to 2030)

Download the Software (Cumulus MX / Cumulus 1 and other related items) from the Wiki

Need a little PHP help

Other discussion about creating web sites for Cumulus that doesn't have a specific subforum

Moderator: daj

Post Reply
Mapantz
Posts: 1774
Joined: Sat 17 Dec 2011 11:55 am
Weather Station: Davis Vantage Pro2
Operating System: Windows 11 x64
Location: Dorset - UK
Contact:

Need a little PHP help

Post 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.. :(
Image
User avatar
beteljuice
Posts: 3292
Joined: Tue 09 Dec 2008 1:37 pm
Weather Station: None !
Operating System: W10 - Threadripper 16core, etc
Location: Dudley, West Midlands, UK

Re: Need a little PHP help

Post by beteljuice »

Code: Select all

    $valTot = $row[3] - $row[1];
You haven't floated the $row values and - (minus) won't force them to numeric.
Image
......................Imagine, what you will KNOW tomorrow !
Mapantz
Posts: 1774
Joined: Sat 17 Dec 2011 11:55 am
Weather Station: Davis Vantage Pro2
Operating System: Windows 11 x64
Location: Dorset - UK
Contact:

Re: Need a little PHP help

Post by Mapantz »

I didn't think they were needed as it still produces the same result :o
Image
User avatar
beteljuice
Posts: 3292
Joined: Tue 09 Dec 2008 1:37 pm
Weather Station: None !
Operating System: W10 - Threadripper 16core, etc
Location: Dudley, West Midlands, UK

Re: Need a little PHP help

Post 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 ?
Image
......................Imagine, what you will KNOW tomorrow !
Mapantz
Posts: 1774
Joined: Sat 17 Dec 2011 11:55 am
Weather Station: Davis Vantage Pro2
Operating System: Windows 11 x64
Location: Dorset - UK
Contact:

Re: Need a little PHP help

Post 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
Image
User avatar
beteljuice
Posts: 3292
Joined: Tue 09 Dec 2008 1:37 pm
Weather Station: None !
Operating System: W10 - Threadripper 16core, etc
Location: Dudley, West Midlands, UK

Re: Need a little PHP help

Post 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 ?
Image
......................Imagine, what you will KNOW tomorrow !
Mapantz
Posts: 1774
Joined: Sat 17 Dec 2011 11:55 am
Weather Station: Davis Vantage Pro2
Operating System: Windows 11 x64
Location: Dorset - UK
Contact:

Re: Need a little PHP help

Post 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?
Image
Mapantz
Posts: 1774
Joined: Sat 17 Dec 2011 11:55 am
Weather Station: Davis Vantage Pro2
Operating System: Windows 11 x64
Location: Dorset - UK
Contact:

Re: Need a little PHP help

Post 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. :bash: :lol:
Image
Mapantz
Posts: 1774
Joined: Sat 17 Dec 2011 11:55 am
Weather Station: Davis Vantage Pro2
Operating System: Windows 11 x64
Location: Dorset - UK
Contact:

Re: Need a little PHP help *Solved*

Post 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.. :(
Image
Post Reply