user2168066 user2168066 - 1 month ago 19
PHP Question

Line breaks for array echo

I have a group of checkboxes in an array that I am echoing the values for. However, I am having trouble trying to get the format that I want the data to be displayed in

Right now the texts is printed as

1 2 3 4 5 6 7 8 9


I am trying to get the format to three columns

1 2 3
4 5 6
7 8 9


This is the code I am using

form ex:
<input class='8' value="1" type="checkbox" id='Form_test8_4' name='Form_test[]'/>
<input class='8' value="2" type="checkbox" id='Form_test8_4' name='Form_test[]'/>
<input class='8' value="3" type="checkbox" id='Form_test8_4' name='Form_test[]'/>
<input class='8' value="4" type="checkbox" id='Form_test8_4' name='Form_test[]'/>


Code:
<?php
if(!empty($_POST['Form_test'])){
foreach($_POST['Form_test'] as $check){
echo $check;
}
}
?>

Answer
for ($i = 0; $i < count($_POST['Form_test']); ++$i) {
  echo $_POST['Form_test'][$i];
  if (($i + i) % 3 === 0) {
    echo '<br />';
  }
}
Comments