1) On head section where javascript is, I have the following
1)在javascript所在的头部,我有以下内容
var chartData =[];
...
// generate some random data which I need to change to php array
generateChartData();
...
chart.dataProvider = chartData;
...
function generateChartData() {
var firstDate = new Date();
firstDate.setDate(firstDate.getDate() - 100);
for (var i = 0; i < 100; i++) {
var newDate = new Date(firstDate);
newDate.setDate(newDate.getDate() + i);
var PROCNT = Math.round(Math.random() * 4) + 100;
var FAT = Math.round(Math.random() * 8) + 100;
var CHOCDF = Math.round(Math.random() * 1) + 80;
var glucose = Math.round(Math.random() * 1) + 110;
var albumine = Math.round(Math.random() * 2) + 102;
var creatinine = Math.round(Math.random() * 3) + 70;
var Na = Math.round(Math.random() * 6) + 103;
var K = Math.round(Math.random() * 4) + 110;
var Ca = Math.round(Math.random() * 2) + 83;
chartData.push({
date: newDate,
Protein: PROCNT,
Fats: FAT,
Carbo: CHOCDF,
Glucose: glucose,
Albumine: albumine,
Creatinine: creatinine,
Sodium: Na,
Potasium: K,
Calcium: Ca
});
}
}
This works Ok with this random data and it is displayed in proper way, but I need to make the graphic with certain mysql query I have after son joins, so
这个随机数据运行正常,它以适当的方式显示,但我需要在儿子加入之后使用某些mysql查询制作图形,所以
2) On my php program after certain user actions I have the following:
2)在某些用户操作后我的php程序中我有以下内容:
while ($row = mysql_fetch_row($result)) {
$date=$row[1];
$PROCNT=$row[2];
$FAT=$row[3];
$CHOCDF=$row[4];
$glucose=$row[5];
$albumine=$row[6];
$creatinine=$row[7];
$Na=$row[8];
$K=$row[9];
$Ca=$row[10];
$new=array_splice($row,1,-10);
$chartData_1 = implode(', ', $new);
$pass="date: $date, Protein: $PROCNT, Fats: $FAT, "
."Carbo: $CHOCDF, "
."Glucose: $glucose, "
."Albumine: $albumine, "
."Creatinine: $creatinine, "
."Sodium: $Na, "
."Potasium: $K, "
."Calcium: $Ca";
$json = json_encode($pass);
echo $json;
... Then I refresh screen and the datagrid I have ... to get new grid and the graphic
...然后我刷新屏幕和我拥有的数据网格......以获得新的网格和图形
3) So my $json looks OK:
3)所以我的$ json看起来不错:
"date: 2012-10-10, Protein: 10.6532, Fats: 45.5899, Carbo: 6.83007, Glucose: 120, Albumine: 3.9, Creatinine: 1.2, Sodium: 15, Potasium: 22, Calcium: 21"
4) Where do I need to assign $json to javascripot chartData array and how?
4)我在哪里需要将$ json分配给javascripot chartData数组以及如何?
I have tried everything I have found on inet, but can get it to work and I don't understand what I am doing wrong. I am not a programmer so clear explanation will be even more appreciated.
我已经尝试了我在inet上找到的所有内容,但可以让它工作,我不明白我做错了什么。我不是程序员,所以更加理解这个明确的解释。
My program makes a query, displays a grid, and after the grid the graphic. We wait some actions of user, so on certain selections, we make what is on step 2 making other queries and displaying the results on a graphic, so first time graphic does not have data, but after grid display and user actions, we try to fill chartData array on javascript and display graphic, but "how and where to send such php array to javascript to display the graphic?
我的程序进行查询,显示网格,并在网格后面显示图形。我们等待用户的某些操作,所以在某些选择上,我们在第2步进行其他查询并在图形上显示结果,因此第一次图形没有数据,但在网格显示和用户操作之后,我们尝试在javascript和display graphic上填充chartData数组,但“如何以及在何处将这样的php数组发送到javascript来显示图形?
3 个解决方案
#1
2
Why don't encode array directly to json with (exemple)
为什么不用(例如)直接将数组编码到json
$myKeys = array('date', 'Protein', 'Carbo' /*, ... */);
$myValues = array(date('m/d/Y'), 25.3, 32 /*, ... */);
$myData = array_combine($myKeys, $myValues);
$json = json_encode($myData);
echo $json; // {"date":"10\/18\/2012","Protein":25.3,"Carbo":32}
A rapid way to integrate:
快速集成方式:
<?php
// your code that generate $result
?>
<script type="text/javascript">
/* ... your js ... */
function generateChartData() {
/* ... */
<?php
// a part of php to fill chartData
while ($row = mysql_fetch_row($result)) {
// build data from $row
$json = json_encode($data);
echo "chartData.push($json)\n";
}
?>
/* ... end of generateChartData... */
}
#2
0
Parse your JSon
with php and for each element in your json add it to your javascript array
:
使用php解析你的JSon,并将json中的每个元素添加到你的javascript数组中:
var array = new Array(<?= $element1; ?>, <?= $element2; ?>);
#3
0
<script type="text/javascript">
var yourarray = <?php echo json_encode($php_array); ?>;
</script>
#1
2
Why don't encode array directly to json with (exemple)
为什么不用(例如)直接将数组编码到json
$myKeys = array('date', 'Protein', 'Carbo' /*, ... */);
$myValues = array(date('m/d/Y'), 25.3, 32 /*, ... */);
$myData = array_combine($myKeys, $myValues);
$json = json_encode($myData);
echo $json; // {"date":"10\/18\/2012","Protein":25.3,"Carbo":32}
A rapid way to integrate:
快速集成方式:
<?php
// your code that generate $result
?>
<script type="text/javascript">
/* ... your js ... */
function generateChartData() {
/* ... */
<?php
// a part of php to fill chartData
while ($row = mysql_fetch_row($result)) {
// build data from $row
$json = json_encode($data);
echo "chartData.push($json)\n";
}
?>
/* ... end of generateChartData... */
}
#2
0
Parse your JSon
with php and for each element in your json add it to your javascript array
:
使用php解析你的JSon,并将json中的每个元素添加到你的javascript数组中:
var array = new Array(<?= $element1; ?>, <?= $element2; ?>);
#3
0
<script type="text/javascript">
var yourarray = <?php echo json_encode($php_array); ?>;
</script>