A table within a table - by golly, you're a genius!!
When I finally got it right I realized I have a bug in the underlying code. Have tried desperately to solve it...
If I search for names first, I get to many instances of apt no. The names are joined together, but the duplicated aptnumbers causes a duplication of the 'portkod' in the next search. It doesn't help to do $portkod = array_unique($portkod). Instead I tried to strip doubles out of the $andel array. But don't know how to call it in my next search.
/search db1 for surnames and apartmentno (Andel)
initdb($medlemmardb);
$sql = mysql_query("SELECT Andel, substring_index(Namn,' ',1) AS surname FROM Uppgifter WHERE Andel>=24 AND Andel <=48 GROUP BY Andel, surname ORDER BY surname ");
while ($row = mysql_fetch_assoc ($sql)) {
$people[$row['Andel']][] =$row['surname'];
$andel[]=$row['Andel'];
}
$andel = array_unique($andel);
//search db2 for 'portkod'variable using aptno from previous search.
initdb($andelardb);
$result = mysql_query("SELECT Portkod FROM Uppgifter WHERE Andel='$andel[$i]'");
while ($rowc = mysql_fetch_assoc($result)) {
$portkod[] = $rowc['Portkod'];
}
Then I tried to switch it around, and search for Andel and portkod first (since there is only one instance of andel/portkod in that db).
initdb($andelardb);
$resultat = mysql_query("SELECT Andel, Portkod FROM Uppgifter WHERE Andel>=24 AND Andel <=48 ");
while ($rowc = mysql_fetch_assoc($resultat)) {
$andel=$rowc['Andel'];
$portkod[] = $rowc['Portkod'];
initdb($medlemmardb);
$svar = mysql_query("SELECT Andel, substring_index(Namn,' ',1) AS surname FROM Uppgifter WHERE Andel=$andel GROUP BY Andel, surname ORDER BY surname ");
while ($row = mysql_fetch_assoc($svar)) {
$people[$row['Andel']][] =$row['surname'];
}
}
That works fine! BUT I lose the sorting of the list. It should be sorted alpabetically, but the above script sorts according to the first search (which is by aptno). Doesn't seem to help that the second search has another ORDER BY statement.
Any good ideas on how to solve it?