我有这个函数和代码的PHP文件:
<?php
$valuesNight = select_sensor_data7($db, $idSensor, $date22y, $date6t);
chart_line($valuesNight);
$valuesEvening = select_sensor_data7($db, $idSensor, $date6t, $date14t);
chart_line($valuesEvening);
?>
<?php function chart_line($values7){ ?>
<div>
<script type="text/javascript">
var values7 = <?php echo $values7 ?>;
var value7 = new Array;
var time7 = new Array;
for(var o in values7) {
value7.push(values7[o]['dValue']);
time7.push(values7[o]['dTimeDate']);
}
//alert(time7);
// store values
sessionStorage.setItem('myValues', JSON.stringify(value7));
sessionStorage.setItem('myTime', JSON.stringify(time7));
</script>
<object type="text/html" data="chart.htm" width="100%" height="420"></object>
</div>
<?php } ?>
select_sensor_data7()
function select_sensor_data7($db, $idSensor, $dateFrom, $dateTo){
$stmt = $db->prepare("SELECT * FROM sensorData WHERE _idSensor = :idSensor
AND dTimeDate BETWEEN :dateFrom AND :dateTo ORDER BY dTimeDate");
$stmt->execute(array(':idSensor' => $idSensor, ':dateFrom' => $dateFrom, ':dateTo' => $dateTo));
$row2 =$stmt->fetchAll(PDO::FETCH_ASSOC);
$json=json_encode($row2);
return $json;
}
在chart.htm
:
<script type="text/javascript">
var myTime = JSON.parse(sessionStorage.getItem('myTime'));
var myValues = JSON.parse(sessionStorage.getItem('myValues'));
alert(myTime);
问题:
select_sensor_data7
返回带有值和日期之间时间的对象,chart_line($valuesNight)
和chart_line($valuesEvening)
运行函数。在函数chart_line($values7)
alert(time7)
中显示正确的值,但在chart.htm
中,我只获得最后的值valuesEvening
。
我在哪里做错了?
答案 0 :(得分:2)
在继续之前,请考虑使用良好的命名惯例,select_sensor_data7
等函数和values7
之类的变量选项不佳。
说完了;它看起来像select_sensor_data7
函数返回一个json_encoded对象数组,即这样的东西(虚拟数据):
[
{"dValue": "123", "dTimeDate": "00/00/0000"},
{"dValue": "123", "dTimeDate": "00/00/0000"},
{"dValue": "123", "dTimeDate": "00/00/0000"}
]
那么,你在PHP函数中可以做的是:
<?php
$valuesNight = select_sensor_data7($db, $idSensor, $date22y, $date6t);
chart_line('night', $valuesNight);
$valuesEvening = select_sensor_data7($db, $idSensor, $date6t, $date14t);
chart_line('evening', $valuesEvening);
?>
<?php function chart_line($chartId, $jsonDataSource) { ?>
<div>
<script type="text/javascript">
var dataSource = JSON.parse('<?php echo $jsonDataSource ?>');
var dValues = new Array;
var dTimeDate = new Array;
for (var i = 0; i < dataSource.length; i++) {
dValues.push(dataSource[i].dValue);
dTimeDate.push(dataSource[i].dTimeDate);
}
sessionStorage.setItem($chartId .'_myValues', JSON.stringify(dValues));
sessionStorage.setItem($chartId .'_myTime', JSON.stringify(dTimeDate));
</script>
<object type="text/html" data="chart.htm?chartId=<?php echo $chartId ?>" width="100%" height="420"></object>
</div>
<?php } ?>
然后你像这样更新你的chart.html
:
<script type="text/javascript">
// http://stackoverflow.com/a/901144/2332336
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
// Parse chartId
var chartId = getParameterByName('chartId', location.href);
// Proceed if you have a chart id
if (typeof chartId !== 'undefined' && chartId && chartId.length > 0)
{
var myTime = JSON.parse(sessionStorage.getItem(chartId +'_myTime'));
var myValues = JSON.parse(sessionStorage.getItem(chartId +'_myValues'));
// TEST
console.log('myTime', myTime);
console.log('myValues', myValues);
}
else
{
alert('error - invalid chart id');
}
</script>
P.S。未经测试,请告诉我您是否遇到此问题。