Php 用相同的选项选择不同的选择框
我有4个不同的选择框(从下拉列表中选择一个选项)。他们都有相同的选择。例如,每个选择框都有以下选项,“Div 1”、“Div 2”、“Div 3”、“Div 4”。 假设以下是在每个选择框中选择的4个值Php 用相同的选项选择不同的选择框,php,html,select,Php,Html,Select,我有4个不同的选择框(从下拉列表中选择一个选项)。他们都有相同的选择。例如,每个选择框都有以下选项,“Div 1”、“Div 2”、“Div 3”、“Div 4”。 假设以下是在每个选择框中选择的4个值 $select1 = 'Div 1' $select2 = 'Div 4' $select3 = 'Div 2' $select4 = 'Div 1' 现在,对于每个按钮,我都要显示一个特定的“div id”。我使用一个简单的if语句来检查每个select的值: //show content
$select1 = 'Div 1'
$select2 = 'Div 4'
$select3 = 'Div 2'
$select4 = 'Div 1'
现在,对于每个按钮,我都要显示一个特定的“div id”。我使用一个简单的if语句来检查每个select的值:
//show content for button 1:
if ($select1 == 'Div 1'){ ?>
<div id="div1"> .... </div>
<? } else if ($select1 == 'Div 2'){ ?>
<div id="div2"> .... </div>
<? }
else if ($select1 == 'Div 3'){ ?>
<div id="div3"> .... </div>
<? }
else if ($select1 == 'Div 4'){ ?>
<div id="div4"> .... </div>
<? } ?>
//显示按钮1的内容:
如果($select1=='div1'){?>
....
....
....
....
我必须为每个选择框重复上述代码,我确信这不是一个好方法。我想知道这个过程是否可以用更短的方式完成?编写一个函数:
function writeDiv($id) {
switch($id) {
case 'div1':
echo '<div id="div1">...';
break;
case 'div2':
echo '<div id="div2">...';
break;
...
}
}
编写一个函数:
function writeDiv($id) {
switch($id) {
case 'div1':
echo '<div id="div1">...';
break;
case 'div2':
echo '<div id="div2">...';
break;
...
}
}
您可以将输出放入数组,然后执行以下操作:
<?php echo $arr[$select1]; ?>
如果无法将此数据放入数组,请将else/If语句更改为开关,并封装到函数中
get_html_by_id($id)
{
var $html = "";
switch($id)
{
case "DIV 1":
$html= "<div 1 />"
break;
case "DIV 2":
$html = "<div 2 />"
break;
default:
break;
}
return $html;
}
<?php echo get_html_by_id($select1); ?>
get\u html\u by\u id($id)
{
var$html=“”;
交换机($id)
{
案件“第1分部”:
$html=“”
打破
案件“第2分部”:
$html=“”
打破
违约:
打破
}
返回$html;
}
您可以将输出放入数组,然后执行以下操作:
<?php echo $arr[$select1]; ?>
如果无法将此数据放入数组,请将else/If语句更改为开关,并封装到函数中
get_html_by_id($id)
{
var $html = "";
switch($id)
{
case "DIV 1":
$html= "<div 1 />"
break;
case "DIV 2":
$html = "<div 2 />"
break;
default:
break;
}
return $html;
}
<?php echo get_html_by_id($select1); ?>
get\u html\u by\u id($id)
{
var$html=“”;
交换机($id)
{
案件“第1分部”:
$html=“”
打破
案件“第2分部”:
$html=“”
打破
违约:
打破
}
返回$html;
}
有没有想过使用数组
$selects = array(
array('tag'=>'Div 1', 'content'=>'...'),
array('tag'=>'Div 4', 'content'=>'...'),
array('tag'=>'Div 2', 'content'=>'...'),
array('tag'=>'Div 1', 'content'=>'...'),
);
foreach ( $selects as $select )
{
print '<div id="'.$select['tag'].'">'.$select['content'].'</div>';
}
$selects=array(
数组('tag'=>'div1','content'=>'…'),
数组('tag'=>'div4','content'=>'…'),
数组('tag'=>'div2','content'=>'…'),
数组('tag'=>'div1','content'=>'…'),
);
foreach($select作为$select进行选择)
{
打印“”。$select['content'].';
}
有没有想过使用数组
$selects = array(
array('tag'=>'Div 1', 'content'=>'...'),
array('tag'=>'Div 4', 'content'=>'...'),
array('tag'=>'Div 2', 'content'=>'...'),
array('tag'=>'Div 1', 'content'=>'...'),
);
foreach ( $selects as $select )
{
print '<div id="'.$select['tag'].'">'.$select['content'].'</div>';
}
$selects=array(
数组('tag'=>'div1','content'=>'…'),
数组('tag'=>'div4','content'=>'…'),
数组('tag'=>'div2','content'=>'…'),
数组('tag'=>'div1','content'=>'…'),
);
foreach($select作为$select进行选择)
{
打印“”。$select['content'].';
}