【问题标题】:How to display checked check boxes together with non-checked ones如何显示选中的复选框和未选中的复选框
【发布时间】:2019-10-25 21:21:13
【问题描述】:

我有 5 个复选框,用户可以从中选择一个或多个选项。然后在数据库中更新选定的选项。然后在另一个页面上显示/查看用户的选择。但是我的问题是,在 PHP 中执行 foreach 循环时,我想将更新的选项与未选择的选项一起显示。

这是 5 个复选框

<input type="checkbox" name="interest[]" value="fishing">Fishing
<input type="checkbox" name="interest[]" value="camping">Camping
<input type="checkbox" name="interest[]" value="hiking">Hiking
<input type="checkbox" name="interest[]" value="swimming">Swimming
<input type="checkbox" name="interest[]" value="running">Running

<br><br>
<input type="submit" name="submit" value="Submit">

这是更新的代码

if (isset($_POST["submit"])) {

 $interestArr = $_POST['interest'];


$interest = new Interest();
$newArr = implode(',', $interestArr);

$interest->updateInterests($id=19, $newArr);

}

这是显示的代码

<?php 

 $interest = new Interest();
 $interests = $interest->showInterests($userid=19)->interests;

 $newArr = explode(',', $interests);

 foreach ($newArr as $data) {

 echo '<input type="checkbox" name="interest[]" value="'.$data .'" checked>'.$data;
}

更新选项存储在 DB 的兴趣列下,如下所示 钓鱼、露营、跑步

foreach 循环显示它们选中的复选框以及正确的对应标签。

如何显示其他未被选中的复选框,以便用户可能想要进行更改?

谢谢。

【问题讨论】:

  • 此代码将所有选中的复选框保存到数据库中,而不是未选中的复选框。因此,您必须将所有复选框的记录保存到一个全局数组或数据库中,然后检查您打印复选框的 foreach 循环中的复选框。如果您的全局复选框与数据库中的值相同,请保持选中状态,否则保持未选中状态。
  • 谢谢@Vantiya。我想我得到了你的凹槽,它看起来不错。你介意提供一些代码 sn-ps 让我开始。谢谢。
  • 兴趣列表是静态数组还是从另一个表中获取?
  • @MohammedYassineCHABLI,它来自数据库中的一个表。更新的同一张表。
  • 我要将所有兴趣变量存储在同一张表的另一列中,就像我们的好朋友 Vantiya 建议的那样,我将做一个比较循环,看看我们如何进行。

标签: php arrays checkbox foreach


【解决方案1】:

这里有一个简单的例子来说明主要思想。此代码旨在在单个脚本中运行。

主要思想是:

  • 使用全局兴趣列表来驱动表单。
  • 保留一个单独的全局列表选中复选框,您可以比较这些复选框以确定是否应选中复选框。
  • 提交表单后,填充跟踪检查项目的列表
  • 呈现表单并将可用复选框列表与选中复选框进行比较。如果在两个列表中都找到项目,则表示我们希望将复选框显示为选中状态。

index.php

<?php
// Keep this outside of the if statement so the form has access to it.
$availableInterests = [
    'fishing',
    'camping',
    'hiking',
    'swimming',
    'running',
];

// Keep this outside of the if statement so the form has access to it.
$selectedInterests = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    // Cast the posted interests to an array in case the user submitted an empty list. 
    // An empty list would be NULL if we didn't cast it. 
    $selectedInterests = (array)$_POST['interest'];

    // foreach $selectedInterests insert into DB...

    // Let this code `fall through` to render the form again.
    // $selectedInterests is now populated and can be used in to the form below to keep the selected checkboxes checked.
}
?>

<form method="post">

    <!-- Using the `global` $availableInterests array to drive our form. -->
    <? foreach ($availableInterests as $interest): ?>

        <!-- Do we want to render the checkbox as checked? -->
        <?php $checked = (in_array($interest, $selectedInterests)) ? ' checked' : ''; ?>

        <input type="checkbox"
               name="interest[]"
               value="<?php echo $interest; ?>"
            <?php echo $checked; ?>>
        <?php echo ucfirst($interest); ?>

    <?php endforeach; ?>

    <br><br>

    <input type="submit" name="submit" value="Submit">

</form>

【讨论】:

    【解决方案2】:

    假设您有一个选择列表数组,例如:

    $choices = ["Fishing", "Camping" , "Hiking","Swimming" , "Running" ]
    

    用户选择他们的选择后

    $interests = ["Fishing" , "Running" ];
    

    在您的情况下,对应的行是:

    $interest = new Interest();
    $interests = $interest->showInterests($userid=19)->interests;
    $newArr = explode(',', $interests); 
    

    假设 $newArr 在可能的例子中等于 $interests

     foreach ($interests as $interest) {
     echo 'you have choose '.$interest.PHP_EOL;
    }
    

    结果:

    you have choose Fishing
    you have choose Running
    

    未选中:

    $notInterests = array_diff($choices,$interests);
    
     foreach ($notInterests as $notInterest) {
     echo 'you have not choose '.$notInterest.PHP_EOL;
    }
    

    结果:

    you have not choose Camping
    you have not choose Hiking
    you have not choose Swimming
    

    单循环处理:

    foreach ($choices as $choice) {
    
      if(in_array($choice,$interests )){
         echo'you have choose '.$choice.PHP_EOL ;
      }else{
          echo 'you have not choose '.$choice.PHP_EOL;
      }
    
    }
    

    希望对您有所帮助。

    【讨论】:

    • 干得好兄弟,我将尝试将另一个 foreach 循环 ($notInterests) 嵌套在第一个 foreach 循环下并一次性回显。我可能还需要帮忙。非常感谢。希望我能给你投票,但我的声望点仍然低于 15。
    • 如果您想在一个循环中处理它,请再次检查答案。
    • 太棒了,我会运行它并返回。
    • 你才华横溢的 Mohammed Yassine CHABLI。非常感谢,工作就像一个魅力。
    【解决方案3】:

    为了帮助可能处于类似情况的其他人,我想根据 Mohammed Yassine CHABLI 的灵感发布至少对我来说现在可行的解决方案。 Vantiya 第一个发表评论真的帮助我了解接下来会发生什么。谢谢大家。

    <?php
    
      $interest = new Interest();
      $interests = $interest->showInterests($userid=19)->interests;
      $choices = $interest->showInterests($userid=19)->choices;
    
      $selected = explode(',', $interests);
      $choices = explode(',', $choices);
    
      foreach ($choices as $choice) {
    
       if(in_array($choice,$selected )){
            echo '<input type="checkbox" name="interest[]" value="'.$choice .'" 
        checked>'.$choice;
    
        }else{
         echo '<input type="checkbox" name="interest[]" value="'.$choice .'" 
          unchecked>'.$choice;
    
        } 
    }
    

    【讨论】:

      猜你喜欢
      • 2014-08-10
      • 1970-01-01
      • 2012-09-11
      • 2018-09-23
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      • 1970-01-01
      • 2014-06-26
      相关资源
      最近更新 更多