Skip to content
Advertisement

How to have only 3 checkboxes per line with dynamic checkbox number

How to have only 3 checkboxes per line with dynamic checkbox number, for example I have 21 checkboxes (read checkbox name form a database).

This is my code

<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>

I have the following situation

enter image description here

But I would like to have only 3 checkboxes per line, for example the 3 with black dot on the first line, the 3 with the red dot on another line, the 3 with the green dot on another line and so on. Something like this done using PAINT

enter image description here

Advertisement

Answer

<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>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement