Solved

How to organize a large number of checkboxes into columns (using Twitter Bootstrap V3)

Posted on 2015-02-02
5
789 Views
Last Modified: 2016-02-25
I have a large number of checkboxes ...

 				<div class="control-group">
					<label for="location" class="control-label">Location: </label>
					<div class="controls">
						<div class="input-append">
							<?php
							
							$tagsdb = explode(',',$_REQUEST['locations']);
							
							foreach ($GLOBALS['all_towns'] AS $tag) {
								$tagid = str_replace(' ', '_', $tag);
								if(in_array($tag, $tagsdb)) $checked = ' CHECKED'; else unset($checked);
								echo "<label class='inline' style='padding-right:8px;'><input type='checkbox' id='location__{$tagid}' name='location[]' value='$tag'{$checked}> $tag</label>";
							}
							?>
						</div>
					</div>
				</div>

Open in new window


What's the best way to automatically organize them into columns...
0
Comment
Question by:Mark
  • 3
  • 2
5 Comments
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 40585091
Add the "row" class to your outer <div>.

Add  the relevant column sizing  to each <label> element e.g.

http://jsbin.com/bicogu/1/edit?html,js,output

<div class="input-append row">
      <label class='inline' style='padding-right:8px;'><input type='checkbox' id='location__{$tagid}' name='location[]' value='$tag'>$tag</label>
...
</div>

Open in new window


I've added the class via script but you could easily do this as you're outputting the php

$(function() {
  // for testing, change to col-xs-[num] to see other configurations
  $('.inline').addClass('col-xs-4');
});

Open in new window


in php:
foreach ($GLOBALS['all_towns'] AS $tag) {
	$tagid = str_replace(' ', '_', $tag);
	if(in_array($tag, $tagsdb)) $checked = ' CHECKED'; else unset($checked);
	echo "<label class='inline col-xs-4' style='padding-right:8px;'><input type='checkbox' id='location__{$tagid}' name='location[]' value='$tag'{$checked}> $tag</label>";
}

Open in new window

0
 

Author Comment

by:Mark
ID: 40585334
I will try this in the morning, thanks!
0
 

Author Closing Comment

by:Mark
ID: 40586641
WOW, works perfect, thanks a million
0
 

Author Comment

by:Mark
ID: 40586654
Anyway to make it collapse into one row or two depending on the size of the screen? (on my phone it just bunches together into 3 columns and also some of the checkboxes break off from the labels line:

Example:

[]
My checkbox label
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40587321
Sure, just change the class to col-sm instead of xs
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now