【问题标题】:How to encode JSON after fetching data from 2 tables using Mysql Php?使用 Mysql Php 从 2 个表中获取数据后如何对 JSON 进行编码?
【发布时间】:2016-12-04 23:05:30
【问题描述】:
// Fetching Name Of Exam
$sql = "SELECT distinct(`nameofexam`) FROM `marks` WHERE `OrgId`=$schoolid AND `Admno`=$Admno";
$result = $objcon->query($sql);

if ($result->num_rows > 0) {
  $row1 = array();
  // output data of each row
  $array1 = array();
  $json = array();
  while ($row = $result->fetch_assoc()) {
    $exam = $row["nameofexam"];
    $json[] = $exam;

    header('Content-type: application/json');
    // Fetching Exam details

    $sql1 = "SELECT  distinct Admno,Class,subject,`markobtained`,Position
            FROM    ( SELECT  @r:= CASE WHEN @e = nameofexam THEN @r + CASE WHEN @p = `markobtained` THEN 0 ELSE @i END ELSE 1 END Position,
                        @i:= CASE WHEN @p = `markobtained` THEN @i + 1 ELSE 1 END incr,
                        @e:= nameofexam,
                        @p:= `markobtained`, 
                        Admno,
                        Class,
                        nameofexam,
                        subject,
                        `markobtained`
                      FROM    marks,
                        (SELECT @e:= '') e,
                        (SELECT @r:= 0) r,
                        (SELECT @p:= 0) p,
                        (SELECT @i:= 0) i
                      ORDER BY nameofexam, `markobtained` desc
                    ) T 
            WHERE `Admno`=$Admno AND `nameofexam`='$exam'
            ORDER BY `subject`,position";
    $result1 = $objcon->query($sql1);
    if ($result1->num_rows > 0) {

      while ($row1 = $result1->fetch_assoc()) {

        array_push($json, $row1);
      }
    }
  }

  header('Content-type: application/json');
  echo json_encode($array1, $response_array);
} else {
  echo "No Exam Details Avalable";
}

我希望有这样的输出。程序逻辑很简单。首先从 table1 中选择考试名称,然后从另一个表中获取该考试的相应评分表并对其进行编码。我需要使用 php 将 mysql 中的多行编码为 json。请帮助我...

预期输出

{"Onam Exam":[{"Admno":"3123","Class":"LKG-A","subject":"english ","markobtained": "50","Position": "1" }] }

【问题讨论】:

    标签: php mysql arrays json getjson


    【解决方案1】:

    终于有输出了。这是工作代码

     <?php
        $sql = "SELECT distinct(`nameofexam`) FROM `marks` WHERE `OrgId`=$schoolid AND `Admno`=$Admno";
        $result = $objcon->query($sql);
    
        if ($result->num_rows > 0) {
          $row1 = array();
          // output data of each row
          $array1 = array();
          $json = array();
           $j=0; 
          while ($row = $result->fetch_assoc()) {
            $exam = $row["nameofexam"];
    
            header('Content-type: application/json');
            // Fetching Exam details
            $sql1 = "SELECT  distinct Admno,Class,subject,`markobtained`,Position
                    FROM    ( SELECT  @r:= CASE WHEN @e = nameofexam THEN @r + CASE WHEN @p = `markobtained` THEN 0 ELSE @i END ELSE 1 END Position,
                                @i:= CASE WHEN @p = `markobtained` THEN @i + 1 ELSE 1 END incr,
                                @e:= nameofexam,
                                @p:= `markobtained`, 
                                Admno,
                                Class,
                                nameofexam,
                                subject,
                                `markobtained`
                              FROM    marks,
                                (SELECT @e:= '') e,
                                (SELECT @r:= 0) r,
                                (SELECT @p:= 0) p,
                                (SELECT @i:= 0) i
                              ORDER BY nameofexam, `markobtained` desc
                            ) T 
                    WHERE `Admno`=$Admno AND `nameofexam`='$exam'
                    ORDER BY `subject`,position";
            $result1 = $objcon->query($sql1);
            $test_array=array();
    
            if ($result1->num_rows > 0) {
            $i=0;       
              while ($row1 = $result1->fetch_assoc()) {
                  $test_array[$i]["Admno"]=$row1["Admno"];
                  $test_array[$i]["Class"]=$row1["Class"];
                  $test_array[$i]["subject"]=$row1["subject"];
                  $test_array[$i]["markobtained"]=$row1["markobtained"];
                  $test_array[$i]["Position"]=$row1["Position"];
    
                 $i++;
    
    
              }
    
         $response[$j]=array(
                $exam=> $test_array
        );  
        $j++;
    
            }
    
                }
    
          header('Content-type: application/json');
          echo json_encode($response);
        } else {
          echo "No Exam Details Avalable";
        }
          }
        ?> 
    

    输出

    [{"Onam Exam":[{"Admno":"3123","Class":"LKG-A","subject":"english ","markobtained":"50","Position":"1"},{"Admno":"3123","Class":"LKG-A","subject":"Physics","markobtained": "10","位置":"18"}]},{"圣诞节 考试":[{"Admno":"3123","Class":"LKG-A","subject":"Chemistry","markobtained":"9","Position":"2"},{" Admno":"3123","Class":"LKG-A","subject":"Hindi","markobtained":"100","Position":"21"},{"Admno":"3123" ,"Class":"LKG-A","subject":"物理","markobtained":"99","Position":"1"}]}]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-17
      • 1970-01-01
      相关资源
      最近更新 更多