使用PHP从db表获取的JSON消息的格式变得扭曲

时间:2018-05-15 00:28:26

标签: php mysql json

我有一个MySQL数据库表,STUDENT格式如下:

ID    NAME     PLACE    MARKS
1     John      NYC     62/115
2     Rose      SF      43/45
3     Alex      DC      54/75
.      .        .       .
.      .        .       .
.      .        .       .
.      .        .       .
.      .        .       .
.      .        .       .
.      .        .       .
.      .        .       .
50    Adam      NYC     88/96

我试图通过使用php从此表中获取数据来创建JSON消息。所需的JSON消息应为:

[{"NAME":"John","ID":"1","PLACE":"NYC","MARKS":"62/115","PERCENTAGE":"53.91%"},{"NAME":"Rose","ID":"2","PLACE":"SF","MARKS":"43/45","PERCENTAGE":"95.56%"},{"NAME":"Alex","ID":"3","PLACE":"DC","MARKS":"54/75","PERCENTAGE":"72%"},.........,{"NAME":"Adam","ID":"50","PLACE":"NYC","MARKS":"88/96","PERCENTAGE":"91.67%"}]

我写了以下代码:

<!DOCTYPE html>
<meta charset="UTF-8">
<head>
<style>
</style>
</head>

<?php
                   $servername = "test.server.com";
                   $username = "test_user";
                   $password = "test_pass";
                   $dbname = "test_db";
                   $conn = new mysqli($servername, $username, $password, $dbname);

                   if ($conn->connect_error) {
                          die("Connection failed: " . $conn->connect_error);
                         }

                       $sql = "select NAME, ID, PLACE, MARKS from STUDENT;";

                       $temp = array();

                       $result = $conn->query($sql);

                       while($row=$result->fetch_assoc())
                          {
                             $temp[] = $row;
                             $number = $row["MARKS"];

                            $pieces = explode("/", $number);

                            if ($pieces[1] <> 0) {
                                      $per = round((($pieces[0]/$pieces[1])*100),2);
                                    }
                            else { $per = 'Undefined';}

                           $temp1= json_encode($temp).($temp[]['percentage']=$per);
                         }

                     $testjson= json_encode($temp1);
                     echo $testjson;

                     mysqli_close($conn);
?>

使用上面的代码,我得到以下输出:

"[{\"NAME\":\"John\",\"ID\":\"1\",\"PLACE\":\"NYC\",\"MARKS\":\"62\\\/115\"},{\"percentage\":53.91},{\"NAME\":\"Rose\",\"ID\":\"2\",\"PLACE\":\"SF\",\"MARKS\":\"43\\\/45\"},{\"percentage\":95.56},{\"NAME\":\"Alex\",\"ID\":\"3\",\"PLACE\":\"DC\",\"MARKS\":\"54\\\/75\"},{\"percentage\":72},..........,{\"NAME\":\"Adam\",\"ID\":\"50\",\"PLACE\":\"NYC\",\"MARKS\":\"88\\\/96\"}]91.67"

上述格式失真。

如何动态计算标记的百分比并将其包含在我所需格式的JSON消息中?请帮忙!

1 个答案:

答案 0 :(得分:0)

<!DOCTYPE html>
<meta charset="UTF-8">
<head>
<style>
</style>
</head>

<?php
    $servername = "test.server.com";
    $username = "test_user";
    $password = "test_pass";
    $dbname = "test_db";
    $conn = new mysqli($servername, $username, $password,$dbname);
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }

    $sql = "select NAME, ID, PLACE, MARKS from STUDENT;";

    $temp = array();

    $result = $conn->query($sql);

    while($row=$result->fetch_assoc())
    {

       $number = $row["MARKS"];
       $pieces = explode("/", $number);
       if ($pieces[1] <> 0) {
         $per = round((($pieces[0]/$pieces[1])*100),2);
       }else { $per = 'Undefined';}
       $row['percentage'] = $per; // add percentage to row
       $temp[] =$row; // add row with percentage to temp array
    }
    $testjson= json_encode($temp1); // note that we only run json_encode once
    echo $testjson;
    mysqli_close($conn);
?>

请尝试上面的代码。