复选框,使用多对多关系 php

我正在尝试创建一个管理表单,允许用户选择他们的需求并使用多对多关系保存到数据库。我可以使用下面的 while 循环生成表单项并提交这些


while ($rows = $equipresults->fetch()) {

$eqid = $rows['req_id'];

$eqname = $rows['requirement'];

echo "

<label class='checkbox'>

<input type='checkbox' name='requirement[]' value='$eqid'> $eqname 

</label><br>

";}

上面的代码产生这样的结果:

https://img1.mukewang.com/64feb7e8000182b806330423.jpg

当我在提交后返回表单时,即使值已添加到数据库中,复选框也不会被选中。我明白为什么上面没有返回任何内容。


所以我的问题是如何编写一个循环,允许我以数组形式显示选中的复选框,类似于下面的内容?


    $equipresult = array ( 

[0] => Array ( [artistid] => 2 [req_id] => 1 [requirement] => Microphone ) 

[1] => Array ( [artistid] => [req_id] => 2 [requirement] => Table ) 

[2] => Array ( [artistid] => 2 [req_id] => 3 [requirement] => Chair ) 

[3] => Array ( [artistid] => [req_id] => 4 [requirement] => Microphone Stand ) 

[4] => Array ( [artistid] => [req_id] => 5 [requirement] => Personal Artifacts ) 

[5] => Array ( [artistid] => [req_id] => 6 [requirement] => Set Dressing ) 

[6] => Array ( [artistid] => [req_id] => 7 [requirement] => Raised Step )

[7] => Array ( [artistid] => [req_id] => 8 [requirement] => Other ) ) 

我试图根据上面的数组显示下面的内容

https://img3.mukewang.com/64feb7f5000154d406250422.jpg

我一直在玩 foreach 循环,但我无法设法检查复选框。我如何循环遍历上面的数组,如果artistid = 2,当foreach循环生成时,在html中将其显示为“已选中”


$req=explode(",",$rows['requirement']);


if (!empty($equipresult)) {

    foreach ($equipresult as $row) {

      $checked = (in_array($row, $req)) ? 'checked="checked"' : '';

  ?>

            <label class='checkbox'>

                <input type="checkbox" name="requirement[]" value="<?php echo $row;?>"

                    <?php $checked;?>><?php echo implode(", ", $row);?>

            </label><br>

            <?php


}

}

?>

最终我试图避免对 html 进行硬编码。这里的任何指导将非常感激。我也确信可能有一种非常简单的方法可以做到这一点。


慕田峪9158850
浏览 59回答 2
2回答

慕沐林林

例子:<?php$options =&nbsp; [&nbsp; &nbsp; ["artistid" => 1,&nbsp; "req_id" => 1,&nbsp; "requirement" => "Microphone"],&nbsp; &nbsp; ["artistid" => 1,&nbsp; "req_id" => 2,&nbsp; "requirement" => "Table"],&nbsp; &nbsp; ["artistid" => 1,&nbsp; "req_id" => 3,&nbsp; "requirement" => "Chair"],&nbsp; &nbsp; ["artistid" => 1,&nbsp; "req_id" => 4,&nbsp; "requirement" => "Microphone Stand "],&nbsp; &nbsp; ["artistid" => 1,&nbsp; "req_id" => 5,&nbsp; "requirement" => "Personal Artifacts"],&nbsp; &nbsp; ["artistid" => 1,&nbsp; "req_id" => 6,&nbsp; "requirement" => "Set Dressing"],&nbsp; &nbsp; ["artistid" => 1,&nbsp; "req_id" => 7,&nbsp; "requirement" => "Raised Step "]];$checkedOptions = [2, 7];foreach ($options as $option) {&nbsp; &nbsp; ?>&nbsp; &nbsp; <label class='checkbox'>&nbsp; &nbsp; &nbsp; &nbsp; <input type="checkbox" name="requirement[]" value="<?= $option['req_id']?>" <?= in_array($option['req_id'], $checkedOptions)&nbsp; ? 'checked' : '' ?> >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<?= $option['requirement'] ?>&nbsp; &nbsp; </label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br><?php&nbsp; &nbsp; }?>

跃然一笑

您需要一个条件语句来查看变量是否设置为来自$_POST[]或$row[]数组的输入字段的名称,以及是否设置回显checked输入字段中的属性。如果您动态创建输入列表,则需要对 value 属性执行相同的操作。<input type='checkbox' name='microphone' value='$eqid' <?php if($eqname === "microphone"){ echo 'checked'; }>&nbsp;<input type='checkbox' name='table' value='$eqid' <?php if($eqname === "table"){ echo 'checked'; }>&nbsp;<input type='checkbox' name='chair' value='$eqid' <?php if($eqname === "chair"){ echo 'checked'; }><input type='checkbox' name='microphone_stand' value='$eqid' <?php if($eqname === "microphone_stand"){ echo 'checked'; }>&nbsp;<input type='checkbox' name='personal_artifacts' value='$eqid' <?php if($eqname === "personal_artifacts"){ echo 'checked'; }>&nbsp;<input type='checkbox' name='set_dressing' value='$eqid' <?php if($eqname === "set_dressing"){ echo 'checked'; }>&nbsp; &nbsp;&nbsp;<input type='checkbox' name='raised_step' value='$eqid' <?php if($eqname === "raised_step"){ echo 'checked'; }>&nbsp;<input type='checkbox' name='other' value='$eqid' <?php if($eqname === "other"){ echo 'checked'; }>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5