我尝试在PHP中生成选择选项HTML元素。首先,我无法使代码使用循环,其次,即使使用最基本的编码方式,我也无法使其正常工作。到目前为止,代码的第一部分和第三部分工作正常。但第二部分似乎没有用。我想知道我的代码有什么问题。我使用Localhost与XAMPP
V3.2.1
我很想知道是否可以使用loop
来实现下拉列表。
<?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>";
?>
以下链接中OUTPUT的屏幕截图。 http://oi57.tinypic.com/20gelfo.jpg
答案 0 :(得分:2)
您应该使用循环输出而不是手动添加选项。此外,输出html比使用echo
更容易出错。在您的代码中,有许多方法可以发生错误 - 在您的情况下 - 您没有关闭select
标记。检查这段代码:
<?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; ?>
答案 1 :(得分: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.
?>
这样做会更加清洁IMO:
<?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>";
然后无论您添加多少选项而不必更改除选项列表之外的代码,您都可以执行相同的操作。
答案 2 :(得分:0)
您忘了关闭所选标签...
<?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
?>