dongwu4834 2014-04-04 03:46
浏览 20

将Ms Ms数据存储到javascript数据集中

I have a code that fetch all the data in MS Access database in php and its working.

$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");}
    echo "<table><tr>";
    echo "<th>id</th>";
    echo "<th>year</th>";
    echo "<th>month</th>";
    echo "<th>empName</th>";
    echo "<th>empPos</th>";
    echo "<th>numMc</th>";
    echo "<th>numLeave</th></tr>";
    while (odbc_fetch_row($rs))
    {
    $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);
    echo"</table>";
    ?>

But now I need to store all the data in the dataset something like this:

var pivot_dataset = [
{"id": 1, "year": 2014 , "month": "JAN", "empName": "David", "empPos": "engineer", "numMc": 1, "numLeave": 2},
];

How to do this?please advise.

  • 写回答

1条回答 默认 最新

  • donglin7383 2014-04-04 03:51
    关注

    Take a look at the json_encode method. It returns a JSON string if you pass it an associative array.

    <?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");
    
    echo "<table><tr>";
    echo "<th>id</th>";
    echo "<th>year</th>";
    echo "<th>month</th>";
    echo "<th>empName</th>";
    echo "<th>empPos</th>";
    echo "<th>numMc</th>";
    echo "<th>numLeave</th></tr>";
    
    $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);
    echo "</table>";
    
    $jsonStr = json_encode($pivot_dataset);
    
    // var pivot_dataset = [{"id": 1, "year": 2014 , "month": "JAN" ... }, { ... }];
    echo "var pivot_dataset = $jsonStr;";
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向