Something like this should work though I havent testested it:
PHP:
<?php
$inputs="<ul class="checkbox-columns">";
foreach($subjects as $subject){
$inputs .= '<li><label><input type="checkbox" name="subjects[]" value="' . $subject['subject'] . '"/>' . $subject['subject'] . '</label></li>';
}
$inputs .= '</ul>';
echo $inputs;
CSS:
.checkbox-columns {
width: 200px;
}
.checkbox-columns li {
display: block;
float: left;
width: 50%;
}
This creates a variable called $inputs
containing a string with an opening <ul>
tag with a CSS class of .checkbox-columns
. It then loops over an array of $subjects
and appends a string of list items (<li>
) containing the inputs (with it’s value attribute and label text set to the value of $subject['subject']
) to the $inputs
variable. Finally, when the loop is finished it appends a closing </ul>
tag and echo
s out the string of HTML that was built.
Then you can style the output with some CSS as needed.
A few notes on the code you posted:
- Instead of only using double quotes (e.g
"
), it’s a bit easier to read if you alternate between single and double quotes (e.g"
and'
). There is also no need to escape the HTML attribute’s quotes this way. - In the
foreach
parameters,AS
should be lowercase. - I wrapped the whole
<input />
tag in a<label>
tag. This lets you add the label text inside HTML instead of just dumping it into a random text node. - Depending on where the
$subjects
array comes from you should strongly consider Sanitizing the output.