I've following array in php
Array
(
[0] => Array
(
[team1_score] => 10
[team2_score] => 5
[round_number] => 1
[teamtitle1] => Chennai super kings
[teamtitle2] => Spartans
)
[1] => Array
(
[team1_score] => 15
[team2_score] => 10
[round_number] => 1
[teamtitle1] => Lions11
[teamtitle2] => Kings Xl Punjab
)
[2] => Array
(
[team1_score] => 15
[team2_score] => 5
[round_number] => 1
[teamtitle1] => Zimbabwe
[teamtitle2] => Red Steel
)
[3] => Array
(
[team1_score] => 10
[team2_score] => 15
[round_number] => 2
[teamtitle1] => Zimbabwe
[teamtitle2] => Chennai super kings
)
[4] => Array
(
[team1_score] =>
[team2_score] =>
[round_number] => 3
[teamtitle1] =>
[teamtitle2] =>
)
)
I want to generate tournament brackets from this array. I'd found jquery plugin for creating tournament brackets. For generating the tournament brackets I've to form json array round wise. for round 1 it should give me 3 records for round 2 1 records and for round 3 1 record. I've tried the following code :-
<script type="text/javascript">
(function (win, doc, $) {
win.TestData<?php echo $cnt; ?> = [
<?php
foreach ($roundmatcharr as $mt => $matchval) {
if (isset($matchval["teamtitle1"]) && $matchval["teamtitle1"] != "") {
$team1 = $matchval["teamtitle1"];
} else {
$team1 = "Team 1";
}
if (isset($matchval["teamtitle2"]) && $matchval["teamtitle2"] != "") {
$team2 = $matchval["teamtitle2"];
} else {
$team2 = "Team 2";
}
if (isset($matchval["team1_score"]) && $matchval["team1_score"] != "") {
$team1score = $matchval["team1_score"];
} else {
$team1score = 0;
;
}
if (isset($matchval["team2_score"]) && $matchval["team2_score"] != "") {
$team2score = $matchval["team2_score"];
} else {
$team2score = 0;
;
}
?>
[
[{"name": "<?php echo $team1; ?>", "id": "<?php echo $team1; ?>", "seed": 1, "score": "<?php echo $team1score; ?>"}, {"name": "<?php echo $team2; ?>", "id": "<?php echo $team2; ?>", "seed": 2, "score": "<?php echo $team2score; ?>"}],
],
<?php } ?>
];
$(".my_gracket").gracket({src: win.TestData<?php echo $cnt; ?>});
})(window, document, jQuery);
</script>
It is giving me output like this:-
win.TestData = [
[
[ {"name" : "Chennai super kings", "id" : "Chennai super kings", "seed" : 1, "score" : "10" }, {"name" : "Spartans", "id" : "Spartans", "seed" : 2, "score" : "5"} ],
],
[
[ {"name" : "Lions11", "id" : "Lions11", "seed" : 1, "score" : "15" }, {"name" : "Kings Xl Punjab", "id" : "Kings Xl Punjab", "seed" : 2, "score" : "10"} ],
],
[
[ {"name" : "Zimbabwe", "id" : "Zimbabwe", "seed" : 1, "score" : "15" }, {"name" : "Red Steel", "id" : "Red Steel", "seed" : 2, "score" : "5"} ],
],
[
[ {"name" : "Zimbabwe", "id" : "Zimbabwe", "seed" : 1, "score" : "10" }, {"name" : "Chennai super kings", "id" : "Chennai super kings", "seed" : 2, "score" : "15"} ],
],
[
[ {"name" : "Team 1", "id" : "Team 1", "seed" : 1, "score" : "0" }, {"name" : "Team 2", "id" : "Team 2", "seed" : 2, "score" : "0"} ],
],
];
which is not correct. The output should be like this:-
win.TestData = [
[
[ {"name" : "Chennai super kings", "id" : "Chennai super kings", "seed" : 1, "score" : "10" }, {"name" : "Spartans", "id" : "Spartans", "seed" : 2, "score" : "5"} ],
[ {"name" : "Lions11", "id" : "Lions11", "seed" : 1, "score" : "15" }, {"name" : "Kings Xl Punjab", "id" : "Kings Xl Punjab", "seed" : 2, "score" : "10"} ],
[ {"name" : "Zimbabwe", "id" : "Zimbabwe", "seed" : 1, "score" : "15" }, {"name" : "Red Steel", "id" : "Red Steel", "seed" : 2, "score" : "5"} ],
],
[
[ {"name" : "Zimbabwe", "id" : "Zimbabwe", "seed" : 1, "score" : "10" }, {"name" : "Chennai super kings", "id" : "Chennai super kings", "seed" : 2, "score" : "15"} ],
],
[
[ {"name" : "Team 1", "id" : "Team 1", "seed" : 1, "score" : "0" }, {"name" : "Team 2", "id" : "Team 2", "seed" : 2, "score" : "0"} ],
],
];
Round 1 matches should be in first array then second and the so on.
One approach is if you first build the data into the right structure in PHP, which makes it easier to manipulate. Then you can output it as JSON using json_encode
.
<?php
$rounds = [];
$output = [];
// Format data and sort by round
foreach ($roundmatcharr as $matchval) {
$match1 = new stdClass();
$match1->name = $matchval['teamtitle1'];
$match1->id = $matchval['teamtitle1'];
$match1->seed = 1;
$match1->score = $matchval['team1_score'];
$match2 = new stdClass();
$match2->name = $matchval['teamtitle2'];
$match2->id = $matchval['teamtitle2'];
$match2->seed = 2;
$match2->score = $matchval['team2_score'];
$rounds[$matchval['round_number']][] = [$match1, $match2];
}
// Extra loop to clean up round numbers
foreach ($rounds as $round) {
$output[] = $round;
}
?>
<script type="text/javascript">
(function (win, doc, $) {
win.TestData<?php echo $cnt; ?> = <?php echo json_encode($output); ?>;
$(".my_gracket").gracket({src: win.TestData<?php echo $cnt; ?>});
})(window, document, jQuery);
</script>