gpt4 book ai didi

javascript - Mysql PHP 到 JavaScript 数组

转载 作者:行者123 更新时间:2023-11-29 23:22:29 25 4
gpt4 key购买 nike

我有一个从 mysql 返回此数据的 PHP 代码 [8,430]、[8,430]、[8,380]、[8,380]、[8,430]

想要将其插入到这个 JavaScript 数据数组中,不知道如何实现。

<?php
$link = mysql_connect('tankStatus.db.numbers.hostedresource.com', 'username', 'password')
or die('Could not connect: ' . mysql_error());

mysql_select_db('tankStatus') or die('Could not select database');

$dataArray=array();
$dateArray=array();
$in=0;
//get data from database
$sql="SELECT * FROM Additives ";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
$dataArray[$in]=$row["Calcium"];
$dateArray[$in]=$row["pH"];
$in++;
}
$arrayLenght = count($dataArray);
for($x = 0; $x < $arrayLenght; $x++) {
echo "{$dateArray[$x]},";
echo "{$dataArray[$x]}";
if($x < $arrayLenght-1){
echo ", ";
}
}
}
?>will return 8,400, 8,420, 8,430, 8,430, 8,380, 8,380, 8,430

// Graph Data ##############################################
var graphData = [{
// Visits
data: [php data inserted here],
color: '#71c73e'
}, {
// Returning Visits
data: [ [6, 500], [7, 600], [8, 550], [9, 600], [10, 800], [11, 900], [12, 800], [13, 850], [14, 830], [15, 1000] ],
color: '#77b7c5',
points: { radius: 4, fillColor: '#77b7c5' }
}
];

最佳答案

要么是经验的运气,要么只是不明白,但我无法使用你的答案。将 mysql php 代码添加到我的主页并将变量格式化为预期的图形。

//part of php mysql
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
$dataArray[$in]=$row["Calcium"];
$dateArray[$in]=$row["pH"];
$in++;
}
//end

//part og graph javascript
graphData = <?php echo "[ "; $arrayLenght = count($dataArray);
for($x = 0; $x < $arrayLenght; $x++) {
echo "[{$dateArray[$x]},";
echo "{$dataArray[$x]}]";
if($x < $arrayLenght-1){
echo ", ";
}
}
echo " ]";
?>;
//end

//result
graphData = [ [8,400], [7,420], [7,430], [8,430], [8,380], [8,380], [8,430] ];
//end

关于javascript - Mysql PHP 到 JavaScript 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27206170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com