Looking for some help if possible.
$pilotsids is an array of ids. merged is the table that holds the data. For each pilotid, I'd like to create an array of newrat values, which I am going to use later to populate a chart. The index of the for loop must be added to the name of each array like this:
$data0[]
$data1[]
$data2[]...etc
It works when I do it separately:
$result = $mysqli->query("select newrat from merged where pilotid = $pilotids[0] order by mid asc");
while($row = mysqli_fetch_assoc($result)) {
$data0[] = $row['newrat'];
}
$result = $mysqli->query("select newrat from merged where pilotid = '$pilotids[1]' order by mid asc");
while($row = mysqli_fetch_assoc($result)) {
$data1[] = $row['newrat'];
}
$result = $mysqli->query("select newrat from merged where pilotid = '$pilotids[2]' order by mid asc");
while($row = mysqli_fetch_assoc($result)) {
$data2[] = $row['newrat'];
}
It doesn't if I try to to iterate automatically for lets say 10 times:
for($i=0; $i < 10; $i ) {
$result = $mysqli->query("select newrat from merged where pilotid = $pilotids[$i] order by mid asc");
while($row = mysqli_fetch_assoc($result)) {
${$data.$i}[] = $row['newrat'];
}
}
CodePudding user response:
You can try this way:
$data = [];
for($i=0; $i < 10; $i ) {
$result = $mysqli->query("select newrat from merged where pilotid = $pilotids[$i] order by mid asc");
while($row = mysqli_fetch_assoc($result)) {
$data[$i][] = $row['newrat'];
}
}
As you can see I'm using a two dimensional array which is defined outside of the loop.