0

I am trying to create some HTML using php/mysql.

My HTML should be something like below:

<div class="row">
  <div>Content Block</div>
  <div>Content Block</div>  
  <div>Content Block</div>
  <div>Content Block</div>
</div>
<div class="row">
  <div>Content Block</div>
  <div>Content Block</div>  
  <div>Content Block</div>
  <div>Content Block</div>
</div>
..........
<div class="row">
  <div>Content Block</div>
  <div>Content Block</div>  
  <div>Content Block</div>
  <div>Content Block</div>
</div>

There are 4 content divs in each row.

This is how I tried it in my PHP while loop, but its not work for me.

while($stmt->fetch()) {    

  if($i % 4 == 0) {
    $html  = "<div class='row'>\n";
  }

  $html .= "    <div class='checkbox col-sm-3'>\n"; 
  $html .= "       <label>\n"; 
  $html .= "         <input class='custom' name='facility[$fid]' type='checkbox'>\n"; 
  $html .= "         <span class='lbl'> {$fname}</span>\n"; 
  $html .= "       </label>\n";   
  $html .= "    </div>\n";   

  if($i++ % 4 == 4) { 
    $html .= "  </div>\n";
  } 

  $ckbxOurPut[] = $html;
} 

Can anybody tell me what it the wrong with this?

Thank you.

8
  • what you mean by this if($i++ % 4 == 4) ?. When this satisfy Commented Jan 19, 2017 at 8:39
  • 1
    what do you mean with "but its not work for me" ? . you have error .. wrong result ? .. no result .. ?. Commented Jan 19, 2017 at 8:39
  • @scaisEdge. It has a wrong result. It is not printing closing tag for .row div. Commented Jan 19, 2017 at 8:43
  • what is $ckbxOurPut and why it is inside loop ? Commented Jan 19, 2017 at 8:48
  • its is an array. Because I need to get the output into an array.. Commented Jan 19, 2017 at 8:50

5 Answers 5

3
if($i++ % 4 == 4) { 

$var % 4 can never be 4. What you want is this:

 if($i++ % 4 == 3) {

Edit: also in line 4 you redefine $html, when you should just concatenate (.= operator)

Sign up to request clarification or add additional context in comments.

2 Comments

@user3733831 yet, this answer is correct, i just tested it and it works fine.
@C̲̅R̲̅O̲̅Z̲̅E̲̅T̲̅ this was definitely an issue, but may be $ckbxOurPut[] = $html; is another problem
1

First, you have to change if($i++ % 4 == 4) to if($i++ % 4 == 3)

Also, on line 4 of your code, you use $html = "<div class='row'>\n"; instead of $html .= "<div class='row'>\n";.

Comments

1

Make this changes 1. Modulus changes 2. Push html in array after outer div close.

 if($i++ % 4 == 3) { 
   $html .= "  </div>\n";
   $ckbxOurPut[] = $html;
 }

Comments

0
<?php
$a = [
    1,2,3,4,5,6,7,8,9,0,11,12,13
];

$i = 0;
$html = "";
foreach($a as $val)
{



   if($i % 4 == 0) {
     $html  .= "<div class='row'>\n";
   }

  $html .= $val."\n";          

   if($i++ % 4 == 3) { 
     $html .= "  </div>\n";
   } 

} 

 if($i % 4 != 3) { 
     $html .= "  </div>\n";
 } 


echo $html;

2 Comments

anything modulus 4 will never give 4 so the div will never be closed
of course, I forgot change condition to: if($i % 4 == 3)
0

Try this:

$i = 0;
$html  = "<div class='row'>";

while($stmt->fetch()) {

    $i++;
    if($i == 5) {
         $html  .= "</div><div class='row'>";
         $i = 1;
     }

     $html .= "    <div class='checkbox col-sm-3'>\n";
     $html .= "       <label>\n";
     $html .= "         <input class='custom' name='facility[$fid]' type='checkbox'>\n";
     $html .= "         <span class='lbl'> {$fname}</span>\n";
     $html .= "       </label>\n";
     $html .= "    </div>\n";


}

$html  .= "</div>";
echo $html;

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.