Я хочу создать PHP API с сервера для анализа JSON до Android, но не говоря уже об этом.
Основная проблема в том, как я могу написать этот код
Это мой PHP код
<?php
include 'includes/conn.php';
$sql = "SELECT * FROM positions ORDER BY priority ASC";
$res = array();
$query = $conn->query($sql);
while($row = $query->fetch_assoc()){
$csql = "SELECT * FROM candidates WHERE position_id='".$row['id']."'";
$cquery = $conn->query($csql);
$cs = array(
'pos_id' => $row['id'],
'pos_des' => $row['description'],
'max_votes' => $row['max_vote']);
while($crow = $cquery->fetch_assoc()){
$fe = array(
'candidates' => array(
'id' => $crow['id'],
'pos_id' => $crow['position_id'],
'firstname' => $crow['firstname'],
'lastname' => $crow['lastname'],
'photo' => $crow['photo'],
'platform' => $crow['platform']
array_push($cs,fe);
)
);
}
array_push($res,$cs);
}
echo json_encode($res);
?>
Теперь он работает нормально, но вместо этого
[{
"pos_id": "1",
"pos_des": "Reprisentative",
"max_votes": "4",
"candidates": {
"id": "1",
"pos_id": "1",
"firstname": "G-9-JAY",
"lastname": "MCARTHUR",
"photo": "",
"platform": ""
}
},
{
"pos_id": "1",
"pos_des": "Reprisentative",
"max_votes": "4",
"candidates": {
"id": "2",
"pos_id": "1",
"firstname": "G-10-JOHN",
"lastname": "MARTIN",
"photo": "",
"platform": ""
}
},
{
"pos_id": "1",
"pos_des": "Reprisentative",
"max_votes": "4",
"candidates": {
"id": "5",
"pos_id": "1",
"firstname": "GRADE 7",
"lastname": "Lang ya",
"photo": "",
"platform": ""
}
},
{
"pos_id": "2",
"pos_des": "President",
"max_votes": "1",
"candidates": {
"id": "3",
"pos_id": "2",
"firstname": "User",
"lastname": "Name",
"photo": "",
"platform": ""
}
},
{
"pos_id": "2",
"pos_des": "President",
"max_votes": "1",
"candidates": {
"id": "4",
"pos_id": "2",
"firstname": "TEST",
"lastname": "USER",
"photo": "",
"platform": ""
}
}
]
Я хочу этот результат
[{
"pos_id": "1",
"pos_des": "Reprisentative",
"max_votes": "4",
"candidates": [{
"id": "1",
"pos_id": "1",
"firstname": "G-9-JAY",
"lastname": "MCARTHUR",
"photo": "",
"platform": ""
}, {
"id": "2",
"pos_id": "1",
"firstname": "G-10-JOHN",
"lastname": "MARTIN",
"photo": "",
"platform": ""
}, {
"id": "5",
"pos_id": "1",
"firstname": "GRADE 7",
"lastname": "Lang ya",
"photo": "",
"platform": ""
}]
},
{
"pos_id": "2",
"pos_des": "President",
"max_votes": "1",
"candidates": [{
"id": "3",
"pos_id": "2",
"firstname": "User",
"lastname": "Name",
"photo": "",
"platform": ""
}, {
"id": "4",
"pos_id": "2",
"firstname": "TEST",
"lastname": "USER",
"photo": "",
"platform": ""
}]
}
]
Резюме: Я хочу Пользователь, который должен указать, в каком pos_id он / она назначает должности, например, МАРТИН, баллотируется на пост президента, поэтому МАРТИН должен показывать на посту президента
Как мне этого добиться?