我使用LabVIEW发送8个变量,这些变量在网页上每秒都在变化。我正在用下面的代码读取变量数据,并将其发送到WebPage并从中绘制图形。一切工作正常,我有1行表每秒更改数据,并且我有实时图形绘制。现在我想将变量发送到MS sql DB。
我使用json数据数组和ajax从LabVIEW中读取变量。(下面的代码)
我想在已经创建数据库和表的地方填充Microsoft SQL数据库。
我制作了php(下面的代码)以将数据发送到sql,但我不知道将其连接到jsonarray和.js文档的任何人可以帮忙吗?
这是我的.js文件:
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
var numbersFromLabView1 = [];
var numbersFromLabView2 = [];
var numbersFromLabView3 = [];
var numbersFromLabView4 = [];
var numbersFromLabView5 = [];
var numbersFromLabView6 = [];
var numbersFromLabView7 = [];
var numbersFromLabView8 = [];
var arrayFromLabView = [];
var time = [];
// na click bottuna
$('.viewbutton').click(function() {
$('#portfolio').addClass('hidden');
$('#element-di-zivi-aplikacija').removeClass('hidden');
setInterval(getDataFromLabView, 1000);
});
// THIS IS THE PART THAT BOTHER ME, WHAT TO ADD SO I CAN PASS DATA TO PHP FILE
function getDataFromLabView() {
var jsonData = $.ajax({
url: 'projectXHTTP',
dataType: 'json',
success: function (jsonArray) {
var zadnjiBroj = jsonArray[0];
var zadnjiBrojy = jsonArray[1];
var zadnjiBrojz = jsonArray[2];
var zadnjiBrojxa = jsonArray[3];
var zadnjiBrojya = jsonArray[4];
var zadnjiBrojza = jsonArray[5];
var zadnjiBrojxb = jsonArray[6];
var zadnjiBrojyb = jsonArray[7];
drawChart(zadnjiBroj, zadnjiBrojy ,zadnjiBrojz, zadnjiBrojxa, zadnjiBrojya, zadnjiBrojza, zadnjiBrojxb, zadnjiBrojyb);
$(document.querySelector('.current-number')) .text(zadnjiBroj);
$(document.querySelector('.current-numbery')) .text(zadnjiBrojy);
$(document.querySelector('.current-numberz')) .text(zadnjiBrojz);
$(document.querySelector('.current-numberxa')).text(zadnjiBrojxa);
$(document.querySelector('.current-numberya')).text(zadnjiBrojya);
$(document.querySelector('.current-numberza')).text(zadnjiBrojza);
$(document.querySelector('.current-numberxb')).text(zadnjiBrojxb);
$(document.querySelector('.current-numberyb')).text(zadnjiBrojyb);
}
});
}
function drawChart(zadnjiBroj, zadnjiBrojy ,zadnjiBrojz, zadnjiBrojxa, zadnjiBrojya, zadnjiBrojza, zadnjiBrojxb, zadnjiBrojyb) {
var data1 = new google.visualization.DataTable();
moment.locale('hr');
time.push(moment());
numbersFromLabView1.push(zadnjiBroj);
data1.addColumn('string', 'Time of Day');
data1.addColumn('number', 'struja 1');
numbersFromLabView1.forEach((zadnjiBroj, key) => {
data1.addRow([time[key].format('LTS'), zadnjiBroj]);
});
var data2 = new google.visualization.DataTable();
moment.locale('hr');
time.push(moment());
numbersFromLabView2.push(zadnjiBrojy );
data2.addColumn('string', 'Time of Day');
data2.addColumn('number', 'struja 2');
numbersFromLabView2.forEach((zadnjiBrojy, key) => {
data2.addRow([time[key].format('LTS'), zadnjiBrojy]);
});
var data3 = new google.visualization.DataTable();
moment.locale('hr');
time.push(moment());
numbersFromLabView3.push(zadnjiBrojz );
data3.addColumn('string', 'Time of Day');
data3.addColumn('number', 'struja 3');
numbersFromLabView3.forEach((zadnjiBrojz, key) => {
data3.addRow([time[key].format('LTS'), zadnjiBrojz]);
});
var data4 = new google.visualization.DataTable();
moment.locale('hr');
time.push(moment());
numbersFromLabView4.push(zadnjiBrojxa );
data4.addColumn('string', 'Time of Day');
data4.addColumn('number', 'napon 1');
numbersFromLabView4.forEach((zadnjiBrojxa, key) => {
data4.addRow([time[key].format('LTS'), zadnjiBrojxa]);
});
var data5 = new google.visualization.DataTable();
moment.locale('hr');
time.push(moment());
numbersFromLabView5.push(zadnjiBrojya );
data5.addColumn('string', 'Time of Day');
data5.addColumn('number', 'napon 2');
numbersFromLabView5.forEach((zadnjiBrojya, key) => {
data5.addRow([time[key].format('LTS'), zadnjiBrojya]);
});
var data6 = new google.visualization.DataTable();
moment.locale('hr');
time.push(moment());
numbersFromLabView6.push(zadnjiBrojza );
data6.addColumn('string', 'Time of Day');
data6.addColumn('number', 'napon 3');
numbersFromLabView6.forEach((zadnjiBrojza, key) => {
data6.addRow([time[key].format('LTS'), zadnjiBrojza]);
});
var data7 = new google.visualization.DataTable();
moment.locale('hr');
time.push(moment());
numbersFromLabView7.push(zadnjiBrojxb );
data7.addColumn('string', 'Time of Day');
data7.addColumn('number', 'snaga');
numbersFromLabView7.forEach((zadnjiBrojxb, key) => {
data7.addRow([time[key].format('LTS'), zadnjiBrojxb]);
});
var data8 = new google.visualization.DataTable();
moment.locale('hr');
time.push(moment());
numbersFromLabView8.push(zadnjiBrojyb );
data8.addColumn('string', 'Time of Day');
data8.addColumn('number', 'frekvencija');
numbersFromLabView8.forEach((zadnjiBrojyb, key) => {
data8.addRow([time[key].format('LTS'), zadnjiBrojyb]);
});
// chart1
var joinedData = google.visualization.data.join(data1, data2, 'full', [[0, 0]], [1], [1]);
var joinedData2 = google.visualization.data.join(joinedData, data3, 'full', [[0, 0]], [1, 2], [1]);
// chart2
var joinedData3 = google.visualization.data.join(data4, data5, 'full', [[0, 0]], [1], [1]);
var joinedData4 = google.visualization.data.join(joinedData3, data6, 'full', [[0, 0]], [1, 2], [1]);
var options = {
background: 'transparent',
title: 'Vrijednosti varijabli očitanih iz LabView-a',
height: 350,
width: '48%',
interpolateNulls: true,
hAxis: {
title: 'Vrijeme',
},
vAxis: {
title: 'Amplituda',
}
}
var chart = new google.visualization.LineChart(document.querySelector('#chart_div'));
var chart2 = new google.visualization.LineChart(document.querySelector('#chart_div2'));
var chart3 = new google.visualization.LineChart(document.querySelector('#chart_div3'));
var chart4 = new google.visualization.LineChart(document.querySelector('#chart_div4'));
chart.draw(joinedData2, options);
chart2.draw(joinedData4, options);
chart3.draw(data7, options);
chart4.draw(data8, options);
}
这是我的php文件
<?php
$conn = new PDO("sqlsrv:Server=127.0.0.1,50632;Database=variables_DB","","");
if($conn->connect_error) {
exit('Could not connect');
}
$data = $_POST['my_data']; //there comes name my_data that gota be created in js
$sql = json_decode($data,true);
$sql= "INSERT INTO jsondatax (var_x, var_y, var_z, var_xa, var_ya, var_za, var_xb, var_yb)
VALUES (jsonArray[0],jsonArray[1],jsonArray[2],jsonArray[3], jsonArray[4], jsonArray[5], jsonArray[6], jsonArray[7]);";
$results = $conn->query($sql);
?>