Datatables create correct nested array in php
Datatables create correct nested array in php
michiiee
Posts: 1Questions: 1Answers: 0
i am trying to populate a datatable with a nested array like this:
var table = jQuery("#gruppeneinteilung_datatable").DataTable( {
ajax: {
dataSrc: 'turnier_details',
type: 'POST',
url: "../custom_scripts/get_turnier_details.php",
data: {
turnier_id: turnier_id
},
},
columnDefs: [
{ data: 'angemeldete_mannschaften.anmeldung_id', className: "spalte_anmeldung_id", "targets": [0], "visible": false },
{ data: 'angemeldete_mannschaften.anmeldung_verein', className: "spalte_verein", "targets": [1], "width": "45%" },
{ data: 'angemeldete_mannschaften.anmeldung_jugend', className: "spalte_mannschaft", "targets": [2], "width": "45%" },
]
});
my json generated from php looks like this:
{"turnier_details":
{"turnier":"Hinterglemm Cup","turnier_adresse":"Zum Hirschdamm 3, 85716 Unterschlei\u00dfheim, Deutschland","datum_von":"28.02.2022","datum_bis":"28.02.2022","uhrzeit_von":"10:00:00","uhrzeit_bis":"17:00:00","anzahl_teilnehmer":"8","spieldauer_vorrunde":"15","spieldauer_finalrunde":"10","bezahlart":"0","anzahl_angemeldete_mannschaften":"4","
angemeldete_mannschaften":
[{"anmeldung_id":"512","anmeldung_verein":"SC Zauchensee","anmeldung_verein_id":"159","anmeldung_jugend":"A1"},{"anmeldung_id":"514","anmeldung_verein":"Red Bull Salzburg","anmeldung_verein_id":"161","anmeldung_jugend":"A1"},{"anmeldung_id":"523","anmeldung_verein":"FC Ryzen","anmeldung_verein_id":"165","anmeldung_jugend":"A1"},{"anmeldung_id":"525","anmeldung_verein":"FC HORIZONT","anmeldung_verein_id":"158","anmeldung_jugend":"1993"}]
}}
** The table sadly is not loaded, what am i doing wrong?
Is the responded json in the wrong format maybe? **
Thats how i response from php
$arr = array(
'turnier'=>$turnier_daten['turnier_name'],
'turnier_adresse'=>$turnier_daten['turnier_address'],
'datum_von'=>$datum_von,
'datum_bis'=>$datum_bis,
'uhrzeit_von'=>$turnier_daten['turnier_uhrzeit_von'],
'uhrzeit_bis'=>$turnier_daten['turnier_uhrzeit_bis'],
'anzahl_teilnehmer'=>$turnier_daten['turnier_anzahl_teams'],
'spieldauer_vorrunde'=>$turnier_daten['turnier_spieldauer_gruppenphase'],
'spieldauer_finalrunde'=>$turnier_daten['turnier_spieldauer_korunde'],
'bezahlart'=>$turnier_daten['turnier_bezahlung'],
'anzahl_angemeldete_mannschaften'=>$anzahl_angemeldete_mannschaften,
'angemeldete_mannschaften'=>$angemeldete_mannschaften,
);
//put into array called turnier_details
$data_array = array("turnier_details" => $arr);
Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
Answers
Two things:
First, if you run the JSON through a validator. you'll see an error with quoting. This might just be an effect of the cut&paste, but it's worth taking a look.
Second, you have specified
turnier_details
asajax.dataSrc
, but that's just an object. I suspect you wantajax.dataSrc
to beturnier_details.angemeldete_mannschaften
to get that array. You would then removeangemeldete_mannschaften
from all yourcolumns.data
,Colin