【发布时间】:2019-01-25 06:21:29
【问题描述】:
我有两个 php 文件。 fetch.php 创建过滤后的数组(从leagueboxes.php 过滤)并通过ajax json 将其传回leagueboxes.php。
fetch.php
<?php
include_once 'dbconfig.php';
if(isset($_GET['filter'])){
$filter = $_GET['filter'];
$query ="SELECT box_id, league_id, position, ";
$query .="(SELECT GROUP_CONCAT(DISTINCT CONCAT(U.first_name, '
',U.last_name) SEPARATOR ' & ')FROM reg_players RP,users U ";
$query .="WHERE RP.id = LE.reg_num AND U.id = RP.user)AS player, ";
$query .="points, place ";
$query .="FROM league_entries LE ";
$query .="JOIN round_league_boxes2 RLB ON RLB.id =
LE.round_league_box_id ";
$query .="JOIN leagues L ON L.id = RLB.league_id ";
$query .="JOIN boxes B ON B.id = RLB.box_id ";
$query .="WHERE league_id =:league ";
$query .="ORDER BY round_league_box_id, position";
$stmnt = $DB_con->prepare($query);
$stmnt->bindParam(":league",$filter);
$stmnt->execute();
$final=$stmnt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($final);
}
?>
leagueboxes.php
<?php
include_once 'dbconfig.php';
include_once 'header.php';
?>
<script>
$(document).ready(function(){
function create_list(data){
var table = $('#table');
table.html('');
var table_head = $('<tr>');
table_head.append($('<th>').text('Box'))
table_head.append($('<th>').text('Position'))
table_head.append($('<th>').text('Player'))
table_head.append($('<th>').text('Points'))
table_head.append($('<th>').text('Place'))
table.append(table_head);
for(var x in data){
var tr = $('<tr>');
tr.append($('<td>').text(data[x].box_id));
tr.append($('<td>').text(data[x].position));
tr.append($('<td>').text(data[x].player));
tr.append($('<td>').text(data[x].points));
tr.append($('<td>').text(data[x].place));
table.append(tr);
};
}
$.ajax({
url:'fetch.php?filter=1',
type:'GET',
success:function(data){
var d = JSON.parse(data);
// console.log(d);
create_list(d);
}
});
$('.filter').on('click',function(){
var filter = $(this).val();
var title = $(this).text();
$('#selected').text(title);
$.ajax({
url:'fetch.php?filter='+filter,
type:'GET',
success:function(data){
var d = JSON.parse(data);
// console.log(d);
create_list(d);
}
});
});
});
</script>
<style>
table {
margin-top:10px;
}
table tr {
border-bottom: 1px solid #ddd;
}
table tr th {
text-transform:uppercase;
padding:5px 10px;
}
table tr td {
padding:5px 10px;
}
table tr:nth-child(odd){
background:#eee;
}
</style>
<div class="clearfix"></div><br/>
<div class="container">
<div class="card-header">
<h1> League Tables</h1>
</div>
</div>
<div class="container">
<div class="card-body">
<button type="button" class="filter btn-primary" value=1>Singles</button>
<button type="button" class="filter btn-primary" value=2>Ladies Doubles</button>
<button type="button" class="filter btn-primary" value=3>Mens Doubles</button>
<button type="button" class="filter btn-primary" value=4>Mixed Doubles</button>
</div>
<br>
<div class="col-md-8 col-md-offset-2" >
<h3 id="selected">Singles</h3>
</div>
<div class="col-md-8 col-md-offset-2" >
<table class="table table-striped" id="table">
<caption id="box"></caption>
</table>
</div>
</div>
<?php include_once 'footer.php'; ?>
我想用 box_id 填充表格标题,并为过滤后的联赛中的每个盒子都有一个表格。
我的问题是我认为我不能循环遍历我生成的数组以将每个位置、玩家、点和位置放在一个框中。我想也许我需要创建一个嵌套数组。也许在我将其编码为 json 之前?
我使用 (PDO::FETCH_GROUP|PDO::FETCH_ASSOC) 创建了一个嵌套数组,然后使用 php foreach 使用 php 填充 html,这样我就有了按 box_id 分组的数据。但是,这似乎不适用于使用 json,或者我无法使用我们的方法? 这是未分组的数组:-
[{
"box_id": "1",
"league_id": "1",
"position": "1",
"player": "Caroline Hogan",
"points": "27",
"place": "0"
}, {
"box_id": "1",
"league_id": "1",
"position": "2",
"player": "Fran Gordon",
"points": "11",
"place": "0"
}, {
"box_id": "1",
"league_id": "1",
"position": "3",
"player": "Josh Handley",
"points": "39",
"place": "0"
}, {
"box_id": "2",
"league_id": "1",
"position": "1",
"player": "Gillian Shaw",
"points": "0",
"place": "0"
}, {
"box_id": "2",
"league_id": "1",
"position": "2",
"player": "Sally Westwood",
"points": "0",
"place": "0"
}, {
"box_id": "2",
"league_id": "1",
"position": "3",
"player": "Sandy Eley",
"points": "0",
"place": "0"
}]
这是分组数组,我不知道如何定义box_id等。
{
"1": [{
"league_id": "1",
"position": "1",
"player": "Caroline Hogan",
"points": "27",
"place": "0"
}, {
"league_id": "1",
"position": "2",
"player": "Fran Gordon",
"points": "11",
"place": "0"
}, {
"league_id": "1",
"position": "3",
"player": "Josh Handley",
"points": "39",
"place": "0"
}],
"2": [{
"league_id": "1",
"position": "1",
"player": "Gillian Shaw",
"points": "0",
"place": "0"
}, {
"league_id": "1",
"position": "2",
"player": "Sally Westwood",
"points": "0",
"place": "0"
}, {
"league_id": "1",
"position": "3",
"player": "Sandy Eley",
"points": "0",
"place": "0"
}]
}
谢谢
【问题讨论】:
-
嗨。据我了解,您的问题是在 php 中创建一个符合您需要的 json 结构,对吗?您能否发布 (1) 您当前的 json 结构示例(
echo json_encode($final)的结果)和 (2) 您想要/期望创建的 json 结构。我认为这将有助于其他人更好地理解您的问题 -
谢谢。以下是没有分组的json,但在一个表中所有框。