我想用json格式显示年份和月份。我是json获取数据,但它看起来不是我想要的,请帮助我找到合适的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数据是:
[
{
"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"
}
]
我的静态json代码如下:
<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>
请帮助我用动态json.i尝试获得这种数据,但是没有任何想法。我希望你能看到嵌套的json。
答案 0 :(得分:0)
试试这个:
<?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);
?>