0

I'm generating my check boxes from this code. But, I'm having difficulty with making it generate by 5 outputs then once the 5th output is displayed it should skip to the next line. Would appreciate any help.

<?php 
    $result=mysql_query("SELECT * FROM tbl_tourism_type order by type_name ");
    while($row = mysql_fetch_array($result))
    echo '<input type="checkbox" name="type" value='.$row['type_id'].'>'
            .'<label>'.$row['type_name']. '</label>'.'<br>'.'<br>';
?>
4
  • Can you explain a little more? Its already showing every next result in a new line Commented Jan 25, 2013 at 5:58
  • so put a counter in your while loop and do continue when it is == 5 Commented Jan 25, 2013 at 5:59
  • Kinda hacky but if you want a 1 liner: if (@++$i == 5) continue; Commented Jan 25, 2013 at 5:59
  • Yes. But, Instead of Checkbox <br> I would really like a Checkbox Checkbox Checkbox Checkbox Checkbox <br> Commented Jan 25, 2013 at 5:59

4 Answers 4

1
<?php 
$result=mysql_query("SELECT * FROM tbl_tourism_type order by type_name ");
$i=1;
while($row = mysql_fetch_array($result))
{
echo '<input type="checkbox" name="type" value='.$row['type_id'].'><label>'.$row['type_name']. '</label>';
if($i%5==0)
{
   echo "<br>";
}
$i++;
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0
$result = mysql_query("SELECT * FROM tbl_tourism_type order by type_name ");
                $i=0;
                while($row = mysql_fetch_array($result)){
                if($i%5 == 0){
                 //skip to next line
                 //continue??
                 }
                echo '<input type="checkbox" name="type" value='.$row['type_id'].'>'.'
                <label>'.$row['type_name']. '</label>'.'<br>'.'<br>';                   
                $i++;
                }

Comments

0

Do like this

<?php 
    $i =1;
    $result=mysql_query("SELECT * FROM tbl_tourism_type order by type_name ");
    while($row = mysql_fetch_array($result)){
    if($i == 5){
      echo "<br>";
      $i = 1;
    }
    else
    echo '<input type="checkbox" name="type" value='.$row['type_id'].'>'
            .'<label>'.$row['type_name']. '</label>';
    $i++;
}
?>

Comments

0

You question is how to skip every 5th entry and to put a linebreak instead?

Have a try like this:

<?php 
    $i=0;
    $result=mysql_query("SELECT * FROM tbl_tourism_type order by type_name ");
    while($row = mysql_fetch_array($result)) 
    {
        if (++$i % 5)
            echo sprintf ( '<input type="checkbox" name="type" value="%s">'
                          .'<label>%s</label>'."\n", 
                           htmlentities($row['type_id']), 
                           htmlentities($row['type_name']) );
        else
            echo "<br>\n";
    }
?>

In addition to iterating over all entries in the result just use a counter you increment with every iteration. Then, inside the iteration, only output the current entry if the counter modulo 5 is not zero. So if it is not a clean multiple of 5.

Comments

Your Answer

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.