Add if loop inside a function

Hi folks,

I have a form for filling out a business card online.
Most fields are predefined to avoid typos and the work position list has a corresponding English value.
One of the titles in Norwegian has different add-ons in English so for the user to pick the correct one I want it to show the English value in parentheses.
I got some help here earlier for when the user picks his position at first and got this script (excerpt):

$q7=mysql_query("select id, name, en_name from met_pos WHERE parentid=0 ORDER BY name");
echo mysql_error();
while($nt7=mysql_fetch_array($q7)){
    if ($nt7[name] == 'Seksjonssjef') {
        echo "addOption(document.drop_list.Pos, '$nt7[id]', '$nt7[name] ($nt7[en_name])');";
    } else {
        echo "addOption(document.drop_list.Pos, '$nt7[id]', '$nt7[name]');";
    }
}

If a user wants to change his title (or anything) before ordering the list shows up again with a selected value but this value comes from different function.
How can I add the same “functionality” to this function?

	function SelectPos() {
		<?php
		$index = 1;
		$resultPos=mysql_query("select id, name, en_name from met_pos ORDER BY name asc");
		while ($row = mysql_fetch_array($resultPos)) {
	?>
		if (<?php echo $row['id']; ?> == <?php echo $_POST['Pos']; ?>)
		{
			var option = document.createElement("option");
			option.text = "<?php echo $row['name']; ?>";
			option.value = "<?php echo $row['id']; ?>";
			document.drop_list.Pos.options.add(option);			
			document.drop_list.Pos.options[<?php echo $index; ?>].selected = true;
		} else {
			var option = document.createElement("option");
			option.text = "<?php echo $row['name']; ?>";
			option.value = "<?php echo $row['id']; ?>";
			document.drop_list.Pos.options.add(option);
		}

I have managed to put the English title in parentheses but then it applies for all values, not only the one I want.

option.text = "<?php echo $row['name']; ?> (<?php echo $row['en_name']; ?>)";

Appreciate any help.
Thanks!

Firstly, you can really simplify this block of code.
Since there is only one line that differs between the two cases in the ‘if’, no need to repeat them.


 function SelectPos() {
        <?php 
        $index = 1;
        $resultPos=mysql_query("select id, name, en_name from met_pos ORDER BY name asc");
        while ($row = mysql_fetch_array($resultPos)) { 
    ?>
       <!-- This is common in regardless of the result of the 'if' below  -->
        var option = document.createElement("option");
        option.text = "<?php echo $row['name']; ?>";
        option.value = "<?php echo $row['id']; ?>";
        document.drop_list.Pos.options.add(option);            

        if (<?php echo $row['id']; ?> == <?php echo $_POST['Pos']; ?>)
        {
            document.drop_list.Pos.options[<?php echo $index; ?>].selected = true;
        }

But , if I understand your question correctly, you can simply add the line you provided in the correct case


        var option = document.createElement("option");
        document.drop_list.Pos.options.add(option);            
        option.value = "<?php echo $row['id']; ?>";
 
        if (<?php echo $row['id']; ?> == <?php echo $_POST['Pos']; ?>)
                {
                    option.text = "<?php echo $row['name']; ?> (<?php echo $row['en_name']; ?>)";
                    document.drop_list.Pos.options[<?php echo $index; ?>].selected = true;
                } else { 
                    option.text = "<?php echo $row['name']; ?>";
                }

Directly after the While:

 if($row['name'] == 'Seksjonssjef') { $row['name'] .= " (".$row['en_name'].")"; }

That worked fine:-)
I tried to make it a lot more complicated…

Thank you very much for your help!