简体   繁体   English

我想对返回的一组 SQL 值进行算术运算

[英]I want to do arithmetic on a returned set of SQL values

I am successfully executing a query on an SQL database.我成功地对 SQL 数据库执行了查询。

$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.它为每个选定的列返回一组 96 个值。 Those go into a HighCharts chart for presentation in a browser.将那些 go 转化为 HighCharts 图表,以便在浏览器中呈现。

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:这些变量输入 HighCharts 代码:

  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.是的,我可以添加另一列并输入 °F 或 inHG,但如果我可以即时调整数字,那似乎很浪费。 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.结果需要看起来像来自 SQL 的结果,据我所知,这是一个 CSV 的数值字符串。 This is being done in a.PHP file.这是在 .PHP 文件中完成的。 I don't know PHP yet.我还不知道 PHP。 If this is too crazy or complicated, then just say so and I will go the other way with adding another column of data.如果这太疯狂或太复杂,那就直说吧,我将以另一种方式 go 添加另一列数据。 Maybe it is SQL that I need, not PHP. Sorry, a bit lost and it shows!也许我需要的是 SQL,而不是 PHP。抱歉,有点迷路,显示出来了!

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.似乎我会使用“ foreach ”并循环遍历原始列表,构成具有相同格式(CSV?)和调整值的新列表。 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.我是一名资深程序员,至少使用过 12 种语言,但对 PHP 和 SQL 还是个新手。一周前才开始在现有项目中使用它,需要一些指导来修改它。 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麦克风

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.既然你说这些数字正在通过 HighCharts 配置,那么上面的内容一定工作正常。

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.上面所做的是,每一行都将数据库查询结果的一列转换为可在 JavaScript 中使用的 JSON 数组。

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.既然你说你对PHP没有什么经验,但你对SQL有经验,我建议你坚持你所知道的并使用SQL进行运算。 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.你要求做的是非常基本的,除了 scope 的单个答案之外 - 简短的答案是你真的需要阅读或观看 PHP 教程,抱歉,但是既然你说你已经使用了 12 种语言,您应该会发现它很容易上手——PHP 与大多数其他类 C 语言非常相似。 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.一条线索:当您想知道 PHP function 的作用时,只需将其放在浏览器中的“https://php.net/”之后即可。 example: https://php.net/array_column示例: https://php.net/array_column

It's important to recognize that there are 4 languages in play here: SQL, PHP, HTML, and Javascript.重要的是要认识到这里有 4 种语言在起作用:SQL、PHP、HTML 和 Javascript。

In a script using best practices, you will see PHP on the top, doing all the logic and data collection/manipulation (SQL calls).在使用最佳实践的脚本中,您将在顶部看到 PHP,执行所有逻辑和数据收集/操作(SQL 调用)。 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 关闭标记 ( ?> ),后跟带有嵌入式 Javascript 的 HTML。PHP 循环、条件和变量替换已完成,如下所示:

<?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.这是对您来说重要的部分:javascript 不在服务器上执行,它在 php 完成并发送其 output在浏览器中执行。

It appears that you are passing an array to javascript, which is then creating the desired output.看来您正在将数组传递给 javascript,然后创建所需的 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.然后解决方案是在 javascript 中创建一个 function 以转换为所需的度量单位,因为 javascript 正在创建其 output。

My advice is to concentrate on the javascript and solve the problem there.我的建议是专注于 javascript 并在那里解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM