gpt4 book ai didi

php - 比较 foreach 和 checkbox 的值

转载 作者:行者123 更新时间:2023-11-29 11:58:06 25 4
gpt4 key购买 nike

我有一个 foreach 循环,它将把“1,2”格式的文本拆分为“1 2”。

我试图将每个分割值与 $rowCentral['tuitionAreaID'] 进行比较,当它匹配时,复选框将被选中。我能够实现这一点,但是,我的所有复选框都是重复的。

    <?php
while($rowCentral=mysql_fetch_array($resultCentral))
{
?>
<?php
foreach($myArray as $my_Array){
//echo $my_Array.'<br>';
?>
<input type="checkbox" name="centralArea[]" value="<?php echo $rowCentral['tuitionAreaID']?>" <?php if($my_Array == $rowCentral['tuitionAreaID']){echo "checked";}?>> <?php echo $rowCentral['tuitionAreaDesc']?><br>
<?php
}
?>
<?php
}
?>

这是上面显示的代码的输出(每行都是重复的):

 Anson, Tanjong Pagar
Anson, Tanjong Pagar
Beach Road, High St, Hill St
Beach Road, High St, Hill St
Cairnhill, Newton, Orchard, Scotts Rd
Cairnhill, Newton, Orchard, Scotts Rd
Cecil, Chinatown, Marina, People's Park, Raffles Place
Cecil, Chinatown, Marina, People's Park, Raffles Place

最佳答案

我认为你想要实现的是这个......你应该检查是否需要单独设置复选框的选中属性,并在 foreach 中测试它,然后只回显该复选框一次......请注意,我还没有测试我的代码..

<?php
while($rowCentral=mysql_fetch_array($resultCentral))
{
$checked = "";
foreach($myArray as $my_Array)
{
if($my_Array == $rowCentral['tuitionAreaID'])
{
$checked = "checked";
}
}
echo "<input type='checkbox' name='centralArea[]' value='".$rowCentral['tuitionAreaID']."' ".$checked.">".$rowCentral['tuitionAreaDesc']."<br>"
}

关于php - 比较 foreach 和 checkbox 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32840443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com