php foreach loop with mysql -


i trying retrieve data database. need 3 pieces of data: kaupunki_id, opisto , pisteet_1. problem in different tables. code retrieve right data it's not super automatic. @ moment variable $t decides data pick.

my table structure:

table name: pisteet

enter image description here

table name: oppilaitokset

enter image description here

table name: kaupungit

enter image description here

what need: each opisto(row) need matching pisteet_1(row) , count average of points. , echo in <td> matching kaupunki_id(row) class.

in table oppilaitokset there can multiple rows have same kaupinki_id(row) id.

my code working showing 1 result , not not automatic:

$keskiynnays = 0; $kerto=0; foreach ($db->query("select kaupunki_id pisteet") $kaupunki_id) {     foreach ($kaupunki_id $kaikkinimet => $t) {         $t= 1;         foreach ($db->query("select opisto oppilaitokset kaupunki_id = '$t'") $kaupungit) {             foreach ($kaupungit $kaupunki => $k) {             }         }         foreach ($db->query("select pisteet_1 pisteet opisto_id = '$t'") $keskiarvo1) {             foreach ($keskiarvo1 $loopkeskiarvo1 => $kes1) {             }             $kerto++;             $keskiynnays +=$kes1;             $keskiarvolaskettu1 = $keskiynnays / $kerto;         }     }        } echo "<td class=\"" . $t . "\">" . $k  . ": " . $keskiarvolaskettu1 . "</td>"; 

as mentioned try sql join - this:

$query = "select opisto o, avg(pisteet_1) p, ka.kaupunki_id k ". "oppilaitokset op ". "left join pisteet pi ". "on op.id = pi.opisto_id ". "left join kaupungit ka ". "on op.kaupunki_id = ka.kaupunki_id ". "group opisto, ka.kaupunki_id";  foreach ($db->query($query) $row) {     print $row['o'] . "<br>";     print $row['p'] . "<br>";     print $row['k'] . "<br>"; } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -