I have simple problem. I am fetching data from database, and I am sending it to Javascript client over Ajax. I am having trouble with formatting array that needs to be sent. My code:
$queryData = $this->execute_query("SELECT tema, opis, id FROM predlogtema WHERE (idpred = '$oblast' AND idprof = '$profesor' AND odabrana = '0')",1);
foreach ($queryData as $key => $value) {
$tema=$queryData[$key]['tema'];
$opis=$queryData[$key]['opis'];
$id=$queryData[$key]['id'];
$profesors[] = array
(
'tema' => ($tema),
'opis' => ($opis),
'id'=>($id)
);
}
echo json_encode($profesors);
Array $queryData looks like this:
Array ( [0] => Array ( [tema] => tema1 [opis] => opis [id] => 1 ) [1] => Array ( [tema] => tema1 [opis] => fsdfds [id] => 4 ) [2] => Array ( [tema] => fsd [opis] => fsdf [id] => 6 ) [3] => Array ( [tema] => ewqrwqr [opis] => etwretrewtre [id] => 21 ) [4] => Array ( [tema] => ewqre [opis] => rewtrwert [id] => 23 ) [5] => Array ( [tema] => ava [opis] => aba [id] => 26 ) [6] => Array ( [tema] => prob1 [opis] => prrobaaa [id] => 30 ) )
Line echo json_encode($profesors) gives this:
Array
(
[0] => Array
(
[tema] => tema1
[opis] => opis
[id] => 1
)
[1] => Array
(
[tema] => tema1
[opis] => fsdfds
[id] => 4
)
[2] => Array
(
[tema] => fsd
[opis] => fsdf
[id] => 6
)
[3] => Array
(
[tema] => ewqrwqr
[opis] => etwretrewtre
[id] => 21
)
[4] => Array
(
[tema] => ewqre
[opis] => rewtrwert
[id] => 23
)
[5] => Array
(
[tema] => ava
[opis] => aba
[id] => 26
)
[6] => Array
(
[tema] => prob1
[opis] => prrobaaa
[id] => 30
)
)
[{"tema":"tema1","opis":"opis","id":"1"},{"tema":"tema1","opis":"fsdfds","id":"4"},{"tema":"fsd","opis":"fsdf","id":"6"},{"tema":"ewqrwqr","opis":"etwretrewtre","id":"21"},{"tema":"ewqre","opis":"rewtrwert","id":"23"},{"tema":"ava","opis":"aba","id":"26"},{"tema":"prob1","opis":"prrobaaa","id":"30"}]
However it should look only like this:
[{"tema":"tema1","opis":"opis","id":"1"},{"tema":"tema1","opis":"fsdfds","id":"4"},{"tema":"fsd","opis":"fsdf","id":"6"},{"tema":"ewqrwqr","opis":"etwretrewtre","id":"21"},{"tema":"ewqre","opis":"rewtrwert","id":"23"},{"tema":"ava","opis":"aba","id":"26"},{"tema":"prob1","opis":"prrobaaa","id":"30"}]
What should I change in my code to achieve this?