简体   繁体   中英

Create nested dynamic json with mysql data in php?

I want to display year and month in json format.Below is my json to get data but it not looks that i want please help me to get proper json.

<?php
    include 'conn.php';
      $year=  array();
      $sql = mysqli_query($conn,'SELECT SALARY_SLIP_ID,ORG_ID,EMPLOYEE_ID,PAY_MONTH,PAY_YEAR FROM india_salary_slip_details where EMPLOYEE_ID = 31');
      while ($row = mysqli_fetch_array($sql ,MYSQLI_ASSOC)) 
      {
          $year[] = array(                 

              'PAY_YEAR' => $row['PAY_YEAR'],
              'PAY_MONTH' => $row['PAY_MONTH'],       

          );

      }       
      echo json_encode($year);

    ?>

json data that i got with this is :

[
  {
    "PAY_YEAR": "2014",
    "PAY_MONTH": "February"
  },
  {
    "PAY_YEAR": "2014",
    "PAY_MONTH": "April"
  },
  {
    "PAY_YEAR": "2014",
    "PAY_MONTH": "May"
  },
  {
    "PAY_YEAR": "2014",
    "PAY_MONTH": "June"
  },
  {
    "PAY_YEAR": "2014",
    "PAY_MONTH": "July"
  },
  {
    "PAY_YEAR": "2014",
    "PAY_MONTH": "November"
  },
  {
    "PAY_YEAR": "2014",
    "PAY_MONTH": "December"
  },
  {
    "PAY_YEAR": "2015",
    "PAY_MONTH": "January"
  },
  {
    "PAY_YEAR": "2015",
    "PAY_MONTH": "February"
  },
  {
    "PAY_YEAR": "2015",
    "PAY_MONTH": "March"
  },
  {
    "PAY_YEAR": "2015",
    "PAY_MONTH": "April"
  },
  {
    "PAY_YEAR": "2015",
    "PAY_MONTH": "May"
  },
  {
    "PAY_YEAR": "2015",
    "PAY_MONTH": "June"
  },
  {
    "PAY_YEAR": "2015",
    "PAY_MONTH": "July"
  },
  {
    "PAY_YEAR": "2015",
    "PAY_MONTH": "August"
  },
  {
    "PAY_YEAR": "2015",
    "PAY_MONTH": "September"
  },
  {
    "PAY_YEAR": "2015",
    "PAY_MONTH": "October"
  },
  {
    "PAY_YEAR": "2015",
    "PAY_MONTH": "November"
  },
  {
    "PAY_YEAR": "2015",
    "PAY_MONTH": "December"
  }
]

I dont want this kind of json. i want like this image 在此处输入图片说明

my static json code is below:

<script type="text/javascript">
      $(document).ready(function(){
        // tree data
        var data = [{
            id: "0",
            text: "2017",
            data: {},
            children: [               
                    {id: "1", text: "January", data: {Action: '|'}},
                    {id: "2", text: "February", data: {Action: '|'}},
                    {id: "3", text: "March", data: {Action: '|'}},
                    {id: "4", text: "April", data: {Action: '|'}},
                    {id: "5", text: "May", data: {Action: '|'}},
                    {id: "6", text: "June", data: {Action: '|'}},
                    {id: "7", text: "July", data: {Action: '|'}},
                    {id: "8", text: "August", data: {Action: '|'}},
                    {id: "9", text: "September", data: {Action: '|'}},
                    {id: "10", text: "October", data: {Action: '|'}},
                    {id: "11", text: "November", data: {Action: '|'}},
                    {id: "12", text: "December", data: {Action: '|'}}            
            ],
            'state': {'opened': false},  
          }];

        // load jstree
        $("div#jstree").jstree({
          plugins: ["table","dnd","contextmenu"],
          core: {
            data: data,
            check_callback: true
          },
          // configure tree table
          table: {
            columns: [
              {width: 200, header: "Year"},
              {width: 150, value: "Action", header: "Action", format: function(v) {if (v){ return 'Print | Delete' }}},

            ],
            resizable: true,
            draggable: true,
            contextmenu: true,
            width: 400,
            height: 300
          }
        });
      });
    </script>

please help me to get this kind of data with dynamic json.i tried lot but dont get any idea. I want nested json as you can see.

Try this:

    <?php
  include 'conn.php';
  $year=  array();
  $sql = mysqli_query($conn,'SELECT SALARY_SLIP_ID,ORG_ID,EMPLOYEE_ID,PAY_MONTH,PAY_YEAR FROM india_salary_slip_details where EMPLOYEE_ID = 31');
  while ($row = mysqli_fetch_array($sql ,MYSQLI_ASSOC)) 
  {

        if( !isset( $year[ $row['PAY_YEAR'] ] ) ) {

            $year[ $row['PAY_YEAR'] ] = array();

        }

        if( !isset( $year[ $row['PAY_YEAR'] ][ $row['PAY_MONTH'] ] ) ) {

            $year[ $row['PAY_YEAR'] ][ $row['PAY_MONTH'] ] = array();

        }


  }       
  echo json_encode($year);

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM