Hi!

I have a dynamic checkbox group to chose drills for golf players. When finished there might be 100 or so drills to select from and every player might have 20 drills selected.

The code below works fine but prints a long vertical list of ckeckboxes straight down. Is it possible to arrange the checkboxes in lets say four columns instead of just one?

<form id="drills" name="drills" method="post" action="">
  <p>
    <label>
    <?php
// build multiple checkbox group with contents of exercise table
$allCats = 'SELECT * FROM exercise';
$catList = mysql_query($allCats);
while ($row = mysql_fetch_assoc($catList)) {
?>  
<label>
<input type="checkbox" name="selectDrills" value="<?php echo $row['drillID']; ?>" id="selectDrills" />
      <?php echo $row['rubrik']; ?></label>
      <br />
<?php 
}
?>     
<input name="spelare_id" type="hidden" id="spelare_id" value="<?php echo $row_Recordset1['id']; ?>" />
</form>

Recommended Answers

All 4 Replies

Member Avatar for diafol

You could put them imto a table or make them fixed width for inline-block elements. This may be better tan table as the "columns" will change with window width.

Member Avatar for diafol

OK, here's an old sample of mine I've refactored. You may find it useful, perhaps not:

<?php
    //HARD CODED ARRAY - USUALLY GET THIS FROM DB
    $content = array(
        array('title'=>'title 1','id'=> 1,'label'=>'a'),
        array('title'=>'title 2','id'=> 2,'label'=>'b'),
        array('title'=>'title 3','id'=> 3,'label'=>'c'),
        array('title'=>'title 4','id'=> 4,'label'=>'d'),
        array('title'=>'title 5','id'=> 5,'label'=>'e'),
        array('title'=>'title 6','id'=> 6,'label'=>'f'),
        array('title'=>'title 7','id'=> 7,'label'=>'g'),
        array('title'=>'title 8','id'=> 8,'label'=>'h'),
        array('title'=>'title 9','id'=> 9,'label'=>'i'),
        array('title'=>'title 10','id'=> 10,'label'=>'j')
    );
    //GET FROM DB INSTEAD OF 0, OTHERWISE FROM POST DATA [should be from DB every time though - need update on last post]
    //THIS METHOD ALLOWS YOU TO STORE ALL SELECTIONS TOTALLED AS A SINGLE INTEGER - V.USEFUL!
    $selected_ids = (isset($_POST['listcheck'])) ? array_sum($_POST['listcheck']) : '0';

    //JUST SOME DISPLAY STUFF TO DEBUG/CHECK
    if(isset($_POST['listcheck'])){
        echo "<p>Posted checkbox data:</p><pre>";
        print_r($_POST['listcheck']);
        echo "</pre>";   
        echo "<p>Selected ids total = $selected_ids</p>";
    }

    //START OUTPUT OF CHECKBOXES
    $inputs = '<form method="post"><ul id="my_checks">';
    foreach($content as $c){
        $id = "listcheck_{$c['id']}";
        $name = "listcheck[{$c['id']}]";
        //CALC THE IDS
        $value = pow(2,($c['id']-1));
        $checked = (isset($selected_ids) && ($selected_ids & $value)) ? ' checked="checked"' : '';
        $inputs .= "<li><input type=\"checkbox\" id=\"$id\" name=\"$name\" value=\"$value\" title=\"{$c['title']}\" $checked/><label for = \"{$c['id']}\">{$c['label']}</label></li>";
    }
    $inputs .= '</ul><br /> <input type="submit" value="go" name="sub" id="sub" />';
?>

<html>
<head>
    <style>
        ul#my_checks{
            list-style:none;
            padding:0;
            margin:0;   
        }
        ul#my_checks li{
            width: 100px;
            float:left;
        }
        br{
            clear:both; 
        }
    </style>
</head>
<body>
    <?php echo $inputs;?>
</body>
</html>

Thanks for your help diafol!

Unfortunately I couldn't get i to work out.

Member Avatar for diafol

OK, no prob.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.