【问题标题】:How to have only 3 checkboxes per line with dynamic checkbox number如何每行只有 3 个带有动态复选框编号的复选框
【发布时间】:2021-02-01 03:52:25
【问题描述】:

如何每行只有 3 个带有动态复选框编号的复选框,例如我有 21 个复选框(从数据库中读取复选框名称)。

这是我的代码

<div class='form-group'>
    <div class="col-lg-12">
        <?php 
            $sql = "SELECT id, posizione, nome
                    FROM user_livelli";                 
            $result = $conn->query($sql);
            if (!empty($result) && $result->num_rows > 0) {         
                // output data of each row
                while($row = $result->fetch_assoc()) {
                    $id = $row['id'];
                    $posizione = $row['posizione'];
                    $nome = $row['nome'];
                                        
                    echo "<input type='checkbox' name='nome_posiz[]'>" ." " .$nome ." ";
                }
            }
        ?>
    </div>
</div>

我有以下情况

但我希望每行只有 3 个复选框,例如第一行带有黑点的 3 个、另一行带有红点的 3 个、另一行带有绿点的 3 个等等。使用 PAINT 完成类似的操作

【问题讨论】:

  • 请注意,如果您希望这些框按特定顺序排列,则需要在查询中或解析结果数组时指定该顺序

标签: php html mysql checkbox bootstrap-4


【解决方案1】:
<div class='form-group'>
    <div class="col-lg-12">
<?php 
    $counter=0;
    $sql = "
    SELECT id
         , posizione
         , nome 
      FROM user_livelli
    ";                 
    $result = $conn->query($sql);
    if (!empty($result) && $result->num_rows > 0) {         
        // output data of each row
        while($row = $result->fetch_assoc()) {
            $counter++;
            $id = $row['id'];
            $posizione = $row['posizione'];
            $nome = $row['nome'];
                                        
            echo "<input type='checkbox' name='nome_posiz[]'>" ." " .$nome ." ";

            if (($counter % 3)==0){
                echo "<br />";//or other formatting you desire,
            }
        }
    }
?>
    </div>
</div>

【讨论】:

  • 是的,如此简单的解决方案!我怎么没想到?
猜你喜欢
  • 2016-01-10
  • 2020-05-15
  • 2021-04-04
  • 2021-11-23
  • 2013-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-11
相关资源
最近更新 更多