1

我正在编写一个编辑表单,但我无法让您检查之前选中的复选框的部分正常运行。

例如,我在数据库中有一个包含五个项目的列表,我之前检查了其中两个并保存了表单。当我在表单上按编辑时,我需要再次获取:

[ ] Item 1
[X] Item 2
[ ] Item 3
[X] Item 4
[ ] Item 5

我有两个数组:

  • $amenities数据库中的所有项目都在这里。

  • $related所有之前检查的项目都在这里。(在这种情况下,其中两个)

我如何遍历两个数组来比较它们,所以如果在$related中找到一个项目$amenities,它将打印选中的框,如果没有,它将打印未选中的框。这是我与该部分相关的代码。

$amenities (print_r)

  Array
(
    [0] => Array
        (
            [itemID] => 3
            [itemName] => Crema Chantilly
        )

    [1] => Array
        (
            [itemID] => 4
            [itemName] => Caribe Cooler
        )

    [2] => Array
        (
            [itemID] => 5
            [itemName] => Cacahuates Japoneses
        )

    [3] => Array
        (
            [itemID] => 6
            [itemName] => Cerveza Sol (lata)
        )

    [4] => Array
        (
            [itemID] => 7
            [itemName] => Chocolate derretido
        )

)

$相关(print_r)

  Array
(
    [0] => Array
        (
            [itemID] => 3
            [itemName] => Crema Chantilly
        )

    [1] => Array
        (
            [itemID] => 4
            [itemName] => Caribe Cooler
        )

)

如您所见,其中有两个项目$related匹配 中的两个项目$amenities

我在这里尝试的是这样的:

<?php foreach ($related as $single) : ?>

    <?php foreach ($amenities as $amenity) : ?>

      <?php if ( $single === $amenity) : ?>

        <input type="checkbox" class="left" checked="yes" name="amenities[]" value="<?=$amenity['itemID']?>">
        <label class="checkbox"><?=$amenity['itemName']?></label>

       <?php else : ?>

        <input type="checkbox" class="left" name="amenities[]" value="<?=$amenity['itemID']?>">
        <label class="checkbox"><?=$amenity['itemName']?></label>

      <?php endif ?>

    <?php endforeach;?>

<?php endforeach;?>

但是,对于完整的选项列表,结果是重复的。我得到这个:

[X] Item 1
[ ] Item 2
[ ] Item 3
[ ] Item 4
[ ] Item 5
[ ] Item 1
[ ] Item 2
[X] Item 3
[ ] Item 4
[ ] Item 5

我需要得到

[X] Item 1
[ ] Item 2
[X] Item 3
[ ] Item 4
[ ] Item 5

也许这很容易做到,但我不知道如何完成。也许我对两者采取了错误的方法foreach

4

2 回答 2

1

而不是在每个循环中写入检查它的存在并将其状态设置为已检查

<?php foreach ($amenities as $single) : ?>
<?php $strChecked = '';?>
    <?php foreach ($related as $amenity) : ?>

      <?php if ( $single === $amenity) : ?>
          <?php $strChecked = ' checked="checked"';break;?>

      <?php endif ?>

    <?php endforeach;?>

        <input type="checkbox" class="left" <?php echo $strChecked;?> name="amenities[]" value="<?=$amenity['itemID']?>">
        <label class="checkbox"><?=$amenity['itemName']?></label>
<?php endforeach;?>
于 2012-04-09T17:28:59.967 回答
0

如果你使用,你实际上可以用一个foreach循环来做到这一点in_array

<?php
    foreach ($amenities as $amenity) {
        $id = $amenity['itemID'];
        $name = $amenity['itemName'];
        $checked = in_array($amenity, $related) ? ' checked="checked"' : '';
        echo <<<HTML
        <input type="checkbox" class="left"$checked name="amenities[]" value="$id">
        <label class="checkbox">$name</label>
        <br>
HTML;
    }
?>

如您所见,我还将$amenity['itemID']和的值存储$amenity['itemName']在变量中,以使 HTML 代码更易于阅读。

您还可以使用稍微复杂但更灵活的方法:

function filter_array_to_ids ($a) {
    return $a['itemID'];
}

// Get an array that contains the id of each item in the
// $related array
$checked_ids = array_map(filter_array_to_ids, $related);

foreach ($amenities as $amenity) {
    $id = $amenity['itemID'];
    $name = $amenity['itemName'];
    $checked = in_array($id, $checked_ids) ? ' checked="checked"' : '';
    echo 'checked = ' . $checked . '<br>';
    echo 'amenity = ' ; print_r($amenity); echo '<br>';
    echo <<<HTML
    <input type="checkbox" class="left"$checked name="amenities[]" value="$id">
    <label class="checkbox">$name</label>
    <br>
HTML;
}

This code uses array_map to create an array that contains only the itemIDs of items in the $related array and then searches for the current amenity's id in that array. This will allow you to find matching ids even if item in $amenities and the item in $related are not exactly the same (if one has extra members, etc).

于 2012-04-10T01:35:16.547 回答