1
$browsers = $day1;
foreach($browsers as $browser) 
{
 echo "<input type='checkbox'/>$browser";
}

When I use this code, it gives me output in single line, but I want to print two entries in single line and other in after the break.

Example:

1 2
3 4
5 6

Can anyone tell me how can we use a line-break in loop?

George Brighton
5,1719 gold badges30 silver badges36 bronze badges
asked Mar 4, 2011 at 4:05
4
  • 3
    $browsers as $i => $browser and then use a condition for $i to break it Commented Mar 4, 2011 at 4:08
  • Can you please be more spesific? Commented Mar 4, 2011 at 4:09
  • Can you put your pencils on the table in groups by two? The algorithm is exactly the same Commented Mar 4, 2011 at 4:16
  • @Joseadrian, that should be an answer. It would be the best one, but as a comment it might not be noticed. Commented Mar 4, 2011 at 4:23

3 Answers 3

1

If this is the output you want:

browser1 browser2
browser3 browser4
...
browserN-1 browserN

You could try this:

$bIndex = 0;
foreach($browsers as $browser) 
{
 echo "<input type='checkbox'/>$browser";
 if (($bIndex % 2) == 1) { // only true for odd bIndex values
 echo "<br>";
 }
 $bIndex++;
}
answered Mar 4, 2011 at 4:17
Sign up to request clarification or add additional context in comments.

1 Comment

You could also do foreach($browsers as $bIndex => $browser) if it's a numerical array.
0

From what you're saying, I think you mean you want to have each checkbox and label on a new line.

foreach($browsers as $browser){
 echo '<p><input type="checkbox" /><label>'.$browser.'</label></p>';
}

Ofcourse, you'd want to be adding a name attribute on each the input tags (and the label tags), if you wanted to be able to process them on the server side.

answered Mar 4, 2011 at 4:09

Comments

0

If I got you correctly you want to show two in each line than after a break, try this

echo "<p>";
for($i=0;$i<count($browsers);$i++) {
if($i>0 && ($i%2==0)) { echo "</p><p>"; }
echo "<input type='checkbox' name='browser[".$browsers[$i]."]' /><label>".$browsers[$i]."</label>";
}
echo "</p>";

Edit for comment:

if it's not enumerated, still easy:

$i=0;
echo "<p>";
foreach ($browsers as $randomstuff=>$browser) { //or whatever array structure.
if($i>0 && ($i%2==0)) { echo "</p><p>"; }
echo "<input type='checkbox' name='browser[".$browsers."]' /><label>".$browsers."</label>";
$i++;
}
echo "</p>";
answered Mar 4, 2011 at 4:13

6 Comments

what if $browsers is not enumerated list?
$kbrowser = array_keys($browsers); $browsers[$kbrowser[$i]]
Updated the anser for the situations where it's not enumerated.
thanks can you tell me one more thing how can we submit the checked value in the database. because i want to submit the value of all checked in the database.
Updated the code once again. After you submit, values will be sent as an array. e.g: For PHP, it'll be like $_POST['browser'] which will be an array, and with a foreach or for loop (like in the question or the examples) or with serializing you can process the data.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.