Anonymous Anonymous - 1 year ago 70
PHP Question

php arrange checkbox values in two columns

I have a form with checkboxes and I want to arrange the checkboxes in two columns instead of one single long column.

How can i split it to two columns?

Here is my code:

if($proceder == True) {
echo "<form name='formulario' method='POST' action='iconizar.php'>";

$iconosm = 0;

$server_groups = $ts3_VirtualServer->serverGroupList();
$servergroups = array();

# En vez de iterar por todos los grupos intenten
foreach($server_groups as $group) {
if($group->type != 1) { continue; }
if(in_array($group["sortid"], $SID_GROUP)) {
$servergroups[] = array('name' => (string)$group, 'id' => $group->sgid, 'type' => $group->type);
}
}
$_SESSION['grupos'] = $servergroups;

foreach($servergroups as $group) {

$miembros = $ts3_VirtualServer->serverGroupClientList($group["id"]);
$estaengrupo = False;
foreach($miembros as $m) {
if($m["client_unique_identifier"] == $client_uid) {
$estaengrupo = True;
}
}

if($estaengrupo) {
$iconosm = $iconosm + 1;
echo '<li><img src="./iconos/icons/'.$group['id']. '.png" alt="" /> ';
echo '<label><input type=checkbox name=grupos['.$group["id"].'] id="'.$group["id"].'" value="'. $group["id"] .'"class="icono" checked >'.$group["name"].'</label><br>';
} else {
echo '<li><img src="./iconos/icons/'. $group['id'] . '.png" alt="" /> ';
echo '<label><input type=checkbox name=grupos['.$group["id"].'] id="'. $group["id"] .'" value="'. $group["id"] .'" class="icono"> '.$group["name"].'</label><br>';
}
}

Answer

I'm assuming your have additional code not posted that close your form and li elements, but in any case you can do this using CSS. Set a width on your form (or it's wrapping element that will accomodate two of your li elements, side by side, then set the float property to 'left' for your li tags... Using inline styles, you might echo html that looks like this:

form tag: <form style="width: 400px;" name='formulario' method='POST'>

li tags: <li style="width 50%;float: left;">...

If you need your checkboxes to appear first half down the left column, then second half down the write, that will involve some PHP.