I am trying to retrieve data from my database. I need three pieces of data: kaupunki_id, Opisto and pisteet_1. The problem is that they all are in different tables. My code does retrieve the right data but it's not super automatic. At the moment the variable $t decides which data to pick.
My table structure:
Table name: pisteet

Table name: oppilaitokset

Table name: kaupungit

What i need: For each opisto(row) i need to get the matching pisteet_1(row) and count the average of the points. and then echo it in a <td> with the matching kaupunki_id(row) class.
In the table oppilaitokset there can be multiple rows that have the same kaupinki_id(row) ID.
My code that is working but is only showing one result and its not not so automatic:
$keskiynnays = 0;
$kerto=0;
foreach ($db->query("SELECT kaupunki_id FROM pisteet") as $kaupunki_id) {
foreach ($kaupunki_id as $kaikkinimet => $t) {
$t= 1;
foreach ($db->query("SELECT opisto FROM oppilaitokset WHERE kaupunki_id = '$t'") as $kaupungit) {
foreach ($kaupungit as $kaupunki => $k) {
}
}
foreach ($db->query("SELECT pisteet_1 FROM pisteet WHERE opisto_id = '$t'") as $keskiarvo1) {
foreach ($keskiarvo1 as $loopkeskiarvo1 => $kes1) {
}
$kerto++;
$keskiynnays +=$kes1;
$keskiarvolaskettu1 = $keskiynnays / $kerto;
}
}
}
echo "<td class=\"" . $t . "\">" . $k . ": " . $keskiarvolaskettu1 . "</td>";