Well I stared at this one last night til I was cross-eyed, so I guess it's time to ask for help... 
I'm working with a map website/database. Each map is in one or more languages. I store languages and their codes (as the pkey) in a separate language table, with a join table to manage the relationships between maps and languages. I'm trying to display an HTML select list of all languages - that works fine. But I want to make the ones that are joined to the current map ($mapid) appear selected. For that, I created a second SQL statement ($sql2) and used a nested loop. The problem is that I don't think I'm comparing my arrays properly ("if $blah1 == $blah2"). If the current map has the first language in the list, that first language will appear selected. But only that first language.
Please forgive the horrible programming, I was a liberal arts student and my only other programming experience was a high school Pascal class a long time ago. And I scored in the bottom 50% in math on the GRE
I'm working with a map website/database. Each map is in one or more languages. I store languages and their codes (as the pkey) in a separate language table, with a join table to manage the relationships between maps and languages. I'm trying to display an HTML select list of all languages - that works fine. But I want to make the ones that are joined to the current map ($mapid) appear selected. For that, I created a second SQL statement ($sql2) and used a nested loop. The problem is that I don't think I'm comparing my arrays properly ("if $blah1 == $blah2"). If the current map has the first language in the list, that first language will appear selected. But only that first language.
Please forgive the horrible programming, I was a liberal arts student and my only other programming experience was a high school Pascal class a long time ago. And I scored in the bottom 50% in math on the GRE
PHP:
// SQL statement for outer loop
$sql = "SELECT code, name FROM lang ORDER BY name";
$result = pg_exec($sql) or die ('Error: Query failed') ;
$numrows = pg_numrows($result);
// SQL statement for inner loop
$sql2 = "SELECT lang_code FROM map_lang_join WHERE map_id = $mapid";
$result2 = pg_exec($sql2) or die ('Error: Query failed');
$numrows2 = pg_numrows($result2);
for ( $i = 0; $i < $numrows; $i++ ) {
$row = pg_fetch_array($result);
echo "<option value=\"" . $row['code'] . "\"";
$blah1 = $row['code'];
for ( $j = 0; $j < $numrows2; $j++ ) {
$row2 = pg_fetch_array($result2);
$blah2 = $row2['lang_code'];
if ($blah1 == $blah2) {
echo " selected=\"selected\"";
break;
}
}
echo ">" . $row['code'] . " " . $row['name'] . "</option>\n";
}