Reputation: 1417
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
Upvotes: 0
Views: 1383
Reputation: 1015
<?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.
Upvotes: 1
Reputation: 5625
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; ?>
Upvotes: 2
Reputation: 28742
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
?>
Upvotes: 0