简体   繁体   English

我如何在PHP foreach循环中列出选定的值作为选定的值和其他值作为列表

[英]How i can list selected value as selected and other values as listin php foreach loop

I want to display selected value as selected and others list in drop down.how it is possible inforeach loop 我想将选定的值显示为选定的值,而其他的则显示在下拉列表中。

<?php $physicaldata = explode(",",$physical); 
?>

<select name="PEPhysical[]" multiple onchange="GetrestPhysical(this.value);">
<?php   foreach ($physicaldata as $physical)

{?>   
<option <?php if($physical   == 'Normal'){ echo ' selected="selected"'; } ?> value="Normal" >Normal Person</option>
<option<?php if($physical   == 'Deaf/Dumb'){ echo ' selected="selected"'; } ?> value="Deaf/Dumb">Deaf/Dumb</option>
<option<?php if($physical   == 'Blind'){ echo ' selected="selected"'; } ?> value="Blind">Blind</option>
<option<?php if($physical   == 'Physically Challenged'){ echo ' selected="selected"'; } ?> value="Physically Challenged">Physically Challenged</option>
<option <?php if($physical   == 'Mentally Challenged'){ echo ' selected="selected"'; } ?>value="Mentally Challenged">Mentally Challenged</option>
<option <?php if($physical   == 'With other Disability'){ echo ' selected="selected"'; } ?> value="With other Disability">With other Disability</option>
<?php  } ?> 

but now listing morethan onetime(look uploaded image) 但现在列出的不止一次(看上传的图片) 这是现在我正在输出

how i can change listing value display ones.? 我如何更改列表值显示的?

Either do the foreach over possible values, and check whether the value is in selected values (only one option tag in your code then since you'r printing it in a loop): 要么对可能的值进行foreach检查,然后检查该值是否在选定的值中(由于循环打印,所以代码中仅包含一个选项标签):

<?php   foreach ($possibleValues as $possibleValue)

{?>   
<option <?php
    if($selectedValues[$possibleValue->id]){ echo ' selected="selected"'; } 
?> value="<?php
    echo $possibleValue->id
?>" ><?php
    echo $possibleValue->description
?></option>
<?php  } ?> 

Or print them as you do, but then don't use a for loop to print them - use a loop or better an associative array to check whether each of them is selected. 或按您的方式打印它们,但是不要使用for循环来打印它们-使用循环或更好的关联数组来检查是否选择了它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在带有选定值的for循环中显示多个下拉列表 - How can i display multiple dropdown list in for loop with selected value PHP发布如何在foreach循环中获取选项的值以显示所选值 - PHP post how to get values of options inside a foreach loop to display the selected value PHP-Foreach循环-回显所选值 - PHP - Foreach loop - echo selected value 我如何在php中获取下拉列表的选定值? - how can i get the selected value of dropdown list in php? 无法使foreach循环中的所有其他复选框在php / javascript中被选中 - Unable to make all other checkboxes in the foreach loop to be selected in php/javascript 如果从最高顺序中选择了值,则PHP foreach循环仅显示所选选项 - PHP foreach loop only display selected option if a value is selected from top order 如何在PHP中使用foreach循环在多个复选框中设置所选属性? - How to set selected properties in multiple checkboxes with foreach loop in PHP? 如何在查询中执行foreach循环以正确显示列表中的选定选项 - How to execute a foreach loop in a query to correctly display selected options in a list 我如何在php中获取foreach循环的当前索引值 - How can I get the current index value of the foreach loop in php 我怎样才能让 php 只写一次选择到列表中? - How can i make to php write only once selected to the list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM