我有一个PHP代码,可以从ms访问中获取数据并将其存储到$ jsonStr,例如:
$pivot_dataset = array();
while(odbc_fetch_row($rs)) {
// Push this data onto the end of the array
$pivot_dataset[] = array(
'id' => odbc_result($rs,"id"),
'year' => odbc_result($rs,"year"),
'month' => odbc_result($rs,"month"),
'empName' => odbc_result($rs,"empName"),
'empPose' => odbc_result($rs,"empPos"),
'numMc' => odbc_result($rs,"numMc"),
'numLeave' => odbc_result($rs,"numLeave")
);
}
odbc_close($conn);
$jsonStr = json_encode($pivot_dataset); //[{"id": 1, "year": 2014 , "month": "JAN" ... }, { ... }];
但现在我想将$ jsonStr存储在javascript数据集中。所以我做这样的事情:
var pivot_dataset = <?php
$conn = odbc_connect('pivot_test','','') or die ("Error in connection");
$sql = "select * from empDetails";
$rs = odbc_exec($conn,$sql);
if (!$rs)
{
exit ("Error in Sql");
}
$pivot_dataset = array();
while(odbc_fetch_row($rs)) {
// Push this data onto the end of the array
$pivot_dataset[] = array(
'id' => odbc_result($rs,"id"),
'year' => odbc_result($rs,"year"),
'month' => odbc_result($rs,"month"),
'empName' => odbc_result($rs,"empName"),
'empPose' => odbc_result($rs,"empPos"),
'numMc' => odbc_result($rs,"numMc"),
'numLeave' => odbc_result($rs,"numLeave")
);
}
odbc_close($conn);
$jsonStr = json_encode($pivot_dataset);
echo $jsonStr;
?>;
但它没有用,我希望javascript存储数据集如下:
var pivot_dataset = [{"id": 1, "year": 2014 , "month": "JAN", "empName": "David", "empPos": "engineer","numMc": 1, "numLeave": 2},];
有人可以请更正吗?我是新手。 TQ
答案 0 :(得分:1)
就像
一样var pivot_dataset = <?php echo $jsonStr; ?>
不像你已经完成了整个逻辑:
var pivot_dataset = <?php
$conn = odbc_connect('pivot_test','','') or die ("Error in connection");
$sql = "select * from empDetails";
$rs = odbc_exec($conn,$sql);
if (!$rs)
...........
首先执行PHP Logic然后将数据存储到您在$jsonStr;
中完成的变量中,然后下一步是
<?php
$conn = odbc_connect('pivot_test','','') or die ("Error in connection");
$sql = "select * from empDetails";
$rs = odbc_exec($conn,$sql);
if (!$rs)
{
exit ("Error in Sql");
}
$pivot_dataset = array();
while(odbc_fetch_row($rs)) {
// Push this data onto the end of the array
$pivot_dataset[] = array(
'id' => odbc_result($rs,"id"),
'year' => odbc_result($rs,"year"),
'month' => odbc_result($rs,"month"),
'empName' => odbc_result($rs,"empName"),
'empPose' => odbc_result($rs,"empPos"),
'numMc' => odbc_result($rs,"numMc"),
'numLeave' => odbc_result($rs,"numLeave")
);
}
odbc_close($conn);
$jsonStr = json_encode($pivot_dataset);
?>
<script>
var pivot_dataset = <?php echo $jsonStr; ?>
</script>
它。