I am successfully executing a query on an SQL database.
$sql = "SELECT id, scdTempC, scdRH, ..., gt1pt0um, reading_time FROM Sensor order by reading_time desc limit 96";
Then, there is this and I don't know, at all, what it is doing.
$scdTempC = json_encode(array_reverse(array_column($sensor_data, 'scdTempC')), JSON_NUMERIC_CHECK);
$scdRH = json_encode(array_reverse(array_column($sensor_data, 'scdRH')), JSON_NUMERIC_CHECK);
It returns a set of 96 values for each column selected. Those go into a HighCharts chart for presentation in a browser.
Then, there is this and I have no idea, at all, what it is called or what it is doing:
var scdTempC = <?php echo $scdTempC; ?>;
var scdRH = <?php echo $scdRH; ?>;
var bmeBP = <?php echo $bmeBP; ?>;
And those vars feed into the HighCharts code:
series:
[{
marker: { symbol: 'dot' }, showInLegend: false,
name: 'SCD41 Temp', data: scdTempC
}],
Is there any way to get to the individual values and do arithmetic on them? Specifically, adjust centigrade temperature to Fahrenheit or Barometric pressure in hPa to inches of Mercury. Yes, I could add another column and feed in °F or inHG, but that seems wasteful if I can adjust the numbers on the fly. The result would need to look like what came from SQL and, as far as I know, that is a CSV string of numeric values. This is being done in a .PHP file. I don't know PHP yet. If this is too crazy or complicated, then just say so and I will go the other way with adding another column of data. Maybe it is SQL that I need, not PHP. Sorry, a bit lost and it shows!
It seems like I would use "foreach
" and loop through the original list making up a new list with the same format (CSV?) and adjusted values. Is that even close?
I am a long-time programmer having worked with at least 12 languages but very new to PHP and SQL. Just started working with it inside an existing project a week ago and needing some pointers for modifying it. I have done a lot, already, but got stuck, here. Since I am jumping into the middle of this, it is difficult to even know what to search for. Search hints would be gladly accepted. THANKS for any help!!!
Mike
CodePudding user response:
These lines appear to be the most important for you
$scdTempC = json_encode(array_reverse(array_column($sensor_data, 'scdTempC')), JSON_NUMERIC_CHECK);
$scdRH = json_encode(array_reverse(array_column($sensor_data, 'scdRH')), JSON_NUMERIC_CHECK);
Since you say that the numbers are feeding through to the HighCharts configuration, the above must be working fine.
What the above is doing, is that each line is converting one column of the database query results into a JSON array that can be used in the JavaScript.
Since you say you have little experience in PHP, but you do have experience in SQL, I recommend you stick to what you know and use the SQL for the arithmetic. That way you have one less problem to work on, and you can concentrate on just getting that data to the chart.
What you're asking to do is quite basic, and beyond the scope of a single answer here - the short answer is that you really need to read or watch a PHP tutorial, sorry! But since you say you've used 12 languages, you should find it easy to pick up - PHP is very much like most other C-like languages, so you should not have a problem catching on.
A clue: when you want to know what a PHP function does, just put it after "https://php.net/" in a browser. example: https://php.net/array_column
CodePudding user response:
It’s important to recognize that there are 4 languages in play here: SQL, PHP, HTML, and Javascript.
In a script using best practices, you will see PHP on the top, doing all the logic and data collection/manipulation (SQL calls). Under that, you’ll see a PHP close tag (?>
) followed by HTML with embedded Javascript. PHP looping, conditionals, and variable substitution are accomplished as seen here:
<?php
// work with user input
// get sql stuff
// work with sql results
$scdTempC = json_encode(array_reverse(array_column($sensor_data, 'scdTempC')), JSON_NUMERIC_CHECK);
$scdRH = json_encode(array_reverse(array_column($sensor_data, 'scdRH')), JSON_NUMERIC_CHECK);
// send output to browser
?>
<html>
… snip …
<!-- simple looping example; there are other ways to do it -->
<ul>
<?php foreach($rows as $row) { ?>
<li><?php echo $row; ?></li>
<?php } ?>
</ul>
… snip …
<!-- now starts javascript -->
<script>
// note the javascript is not running yet; we are filling in values from PHP
// The javascript will run in the browser after PHP serves it
var scdTempC = <?php echo $scdTempC; ?>;
var scdRH = <?php echo $scdRH; ?>;
var bmeBP = <?php echo $bmeBP; ?>
// more javascript
// your solution is down here somewhere. While iterating through the array,
// make a call to the appropriate javascript converter function to translate
function toFahrenheit(celsius)
{
return celsius * 9 / 5 32;
}
function toCelsius(fahrenheit)
{
return (fahrenheit - 32) * 5 / 9;
}
</script>
Here’s the important part for you: the javascript is not executed on the server, it’s executed in the browser after the php has finished and sent its output.
It appears that you are passing an array to javascript, which is then creating the desired output.
The solution then would be to create a function in javascript to convert to the desired unit of measure, as javascript is creating its output.
My advice is to concentrate on the javascript and solve the problem there.