0

I tried Generating the Selection Option HTML Elements in PHP. First off, I couldn't make the code work with a loop and secondly even after using the most rudimentary way of coding I couldn't make it work properly. So far, the first and third section of the code works fine. But The second section doesn't seem to work. I wonder what is wrong with my code. I am using Localhost with XAMPP V3.2.1

I would love to see if one can implement the dropdown using a loop instead.

<?php
 echo "<select style='width: 300px'>";
 $securityset1 = array(
                'What was your childhood nickname?', 'What is the name of your favorite childhood friend?', 'What street did you live on in third grade?'
);


 $i=0;
    echo "<option value='";
    echo $i++;
    echo"'>";
    echo $securityset1[0];
    echo "</option>";

    echo "<option value='";
    echo $i++;
    echo"'>";
    echo $securityset1[1];
    echo "</option>";

    echo "<option value='";
    echo $i++;
    echo"'>";
    echo $securityset1[2];
    echo "</option>";
    echo "<br>";    
?>

<?php
 echo "<select style='width: 300px'>";
 $securityset2 = array(
                'In what city or town was your first job?', 'Where were you when you first heard about 9/11?', 'What is your dream vacation spot?', 'What is your best friends last name?'
);          

 $j=0;
    echo "<option value='";
    echo $j++;
    echo"'>";
    echo $securityset2[0];
    echo "</option>";

    echo "<option value='";
    echo $j++;
    echo"'>";
    echo $securityset2[1];
    echo "</option>";

    echo "<option value='";
    echo $j++;
    echo"'>";
    echo $securityset2[2];
    echo "</option>";

?>
<?php
 echo "<select style='width: 300px'>";
 $securityset3 = array(
                'In what city or town was your first job?', 'Where were you when you first heard about 9/11?', 'What is your dream vacation spot?', 'What is your best friends last name?'
);      


 $j=0;
    echo "<option value='";
    echo $j++;
    echo"'>";
    echo $securityset3[0];
    echo "</option>";

    echo "<option value='";
    echo $j++;
    echo"'>";
    echo $securityset3[1];
    echo "</option>";

    echo "<option value='";
    echo $j++;
    echo"'>";
    echo $securityset3[2];
    echo "</option>";

    echo "<option value='";
    echo $j++;
    echo"'>";
    echo $securityset3[4];
    echo "</option>";
    echo "<br>";

?>

Screenshot of OUTPUT in following link. http://oi57.tinypic.com/20gelfo.jpg

3
  • you are not closing your select element. Commented Mar 4, 2014 at 8:23
  • Why so echo? Try merging html tags. $select .= '<option... and later echo $select. Just an opinion. Commented Mar 4, 2014 at 8:25
  • @Gerald Schneider Thanks. I felt dumb but that basically solved the issue. Commented Mar 4, 2014 at 8:26

3 Answers 3

2

You should use loops for output instead of manually adding options. Also, there are much less error-prone ways of outputting html than using echo. In your code there are many ways to make errors - in your case - you didn't close select tags. Check this piece of code out:

<?php 
    $securityset = array(
        array(
            'What was your childhood nickname?', 
            'What is the name of your favorite childhood friend?', 
            'What street did you live on in third grade?'
        ),
        array(
            'In what city or town was your first job?', 
            'Where were you when you first heard about 9/11?', 
            'What is your dream vacation spot?', 
            'What is your best friends last name?'
        ),
        array(
            'In what city or town was your first job?', 
            'Where were you when you first heard about 9/11?', 
            'What is your dream vacation spot?', 
            'What is your best friends last name?'
        )
    );
?>

<?php foreach ($securityset as $set) : ?>
    <select style="width: 300px">
        <?php foreach ($set as $index=>$question) : ?>
           <option value="<?= $index ?>"><?= $question ?></option>
        <?php endforeach; ?>
    </select>
<?php endforeach; ?>   
Sign up to request clarification or add additional context in comments.

Comments

1
<?php
 echo "<select style='width: 300px'>";
 $securityset1 = array(
                'What was your childhood nickname?', 'What is the name of your favorite childhood friend?', 'What street did you live on in third grade?'
);


 $i=0;
    echo "<option value='";
    echo $i++;
    echo"'>";
    echo $securityset1[0];
    echo "</option>";

    echo "<option value='";
    echo $i++;
    echo"'>";
    echo $securityset1[1];
    echo "</option>";

    echo "<option value='";
    echo $i++;
    echo"'>";
    echo $securityset1[2];
    echo "</option>";
    echo "<br>";
    echo "</select>"; // this is what was missing.
?>

It would be MUCH cleaner IMO to do it like this:

<?php
 echo "<select style='width: 300px'>";
 $securityset1 = array(
                'What was your childhood nickname?', 'What is the name of your favorite childhood friend?', 'What street did you live on in third grade?'
);


 $i=0;
 while ( $i < count($securityset1)) {
    echo "<option value='";
    echo $i;
    echo"'>";
    echo $securityset1[i];
    echo "</option>";
    $i++;
    }
echo "</select>";

then you can do the same thing no matter how many options you add without having to change the code except for the options list.

Comments

0

You forgot to close your select tags...

<?php
 echo "<select style='width: 300px'>";
 $securityset1 = array(
                'What was your childhood nickname?', 'What is the name of your favorite childhood friend?', 'What street did you live on in third grade?'
);


 $i=0;
    echo "<option value='";
    echo $i++;
    echo"'>";
    echo $securityset1[0];
    echo "</option>";

    echo "<option value='";
    echo $i++;
    echo"'>";
    echo $securityset1[1];
    echo "</option>";

    echo "<option value='";
    echo $i++;
    echo"'>";
    echo $securityset1[2];
    echo "</option>";
    echo "</SELECT><br/>"; // Added closing select tag here    
?>

<?php
 echo "<select style='width: 300px'>";
 $securityset2 = array(
                'In what city or town was your first job?', 'Where were you when you first heard about 9/11?', 'What is your dream vacation spot?', 'What is your best friends last name?'
);          

 $j=0;
    echo "<option value='";
    echo $j++;
    echo"'>";
    echo $securityset2[0];
    echo "</option>";

    echo "<option value='";
    echo $j++;
    echo"'>";
    echo $securityset2[1];
    echo "</option>";

    echo "<option value='";
    echo $j++;
    echo"'>";
    echo $securityset2[2];
    echo "</option>";
    echo "</select><BR/>";//added closing select here
?>
<?php
 echo "<select style='width: 300px'>";
 $securityset3 = array(
                'In what city or town was your first job?', 'Where were you when you first heard about 9/11?', 'What is your dream vacation spot?', 'What is your best friends last name?'
);      


 $j=0;
    echo "<option value='";
    echo $j++;
    echo"'>";
    echo $securityset3[0];
    echo "</option>";

    echo "<option value='";
    echo $j++;
    echo"'>";
    echo $securityset3[1];
    echo "</option>";

    echo "<option value='";
    echo $j++;
    echo"'>";
    echo $securityset3[2];
    echo "</option>";

    echo "<option value='";
    echo $j++;
    echo"'>";
    echo $securityset3[4];
    echo "</option>";
    echo "</SELECT><BR/>";// Added closing select here

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.