I got the following table in a database
- ID | successor
- 01 | 02
- 01 | 04
- 01 | 08
- 02 | 03
- 04 | 05
- 05 | 06
This is just a simple assignment table (is this the correct english term?) for content of an E-Learning system.
My Current php code is this
$sqlget = "SELECT * from successor";
$result = $conn->query($sqlget);
$dbsuccessor = [];
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$dbsuccessor[] = $row;
}
} else {
echo "0 results";
}
after parsing this to js with json_encode i get a not usefull construct like this
var successors = <?php echo json_encode($dbsuccessor);?>;
console.log(successor);
Array[9]
0:
Object ID: "1" - successor: "2"
1:
Object ID: "1" - successor: "4"
2:
Object ID: "1" - successor: "8"
to be fair, this is obviously exactly what you would expect with my code used above.
My goal is an array output like this:
ID: "1" -> successor"2","4","8"
so i can see all successors of module with ID 1 when typing console.log(successors[1]); in javascript
best thing would be if my db table stay like it is. How can i achieve my goal?