SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Location
    Oslo, Norway
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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):

    PHP Code:
    $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?
    PHP Code:
        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.
    PHP Code:
    option.text = "<?php echo $row['name']; ?> (<?php echo $row['en_name']; ?>)";
    Appreciate any help.
    Thanks!

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,287
    Mentioned
    182 Post(s)
    Tagged
    4 Thread(s)
    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.
    Code php:
     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
    Code php:
            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']; ?>";
                    }
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Directly after the While:
    PHP Code:
     if($row['name'] == 'Seksjonssjef') { $row['name'] .= " (".$row['en_name'].")"; } 
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2007
    Location
    Oslo, Norway
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That worked fine:-)
    I tried to make it a lot more complicated...

    Thank you very much for your help!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •