0

I have some 'html' contents to display in php while loop. However, I want to have only 3 content blocks in every row and rendered html should be something like below:

<div class="row">
  <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>
<div class="row">
  <div>Content Block</div>
  <div>Content Block</div>
  <div>Content Block</div>
</div> 

This is how I tried it:

$output=[];    
$i=0; 
$html='';
while($row = $stmt->fetch()) {    

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

  $html.="<div class='col-sm-4'>
            <div class='room-box'>
              <img src='$thumb' class='img-responsive' >
              <h4>$name</h4>
            </div>
          </div>\n";

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

But its not working for me. Can anybody tell me What am I doing wrong? Thank you

1
  • What are you getting so far with the code you're testing? Commented Jan 28, 2018 at 8:03

3 Answers 3

1

I think you have to reset the $html = ''; in the while loop after you have added it to the $output=[];

while($row = $stmt->fetch()) {

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

    $html.="<div class='col-sm-4'>
            <div class='room-box'>
              <img src='$thumb' class='img-responsive' >
              <h4>$name</h4>
            </div>
          </div>\n";

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

Test output

Note: When you have for example 11 items, you will miss the last 2.

If you also want to add the last 2, you could add this after the while loop which will check if $html is not an empty string :

if ($html !== '') {
    $html .= "</div>\n";
    $output[] = $html;
}

Output php

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

2 Comments

yes, it miss the last 2. can you tell how its get last 2 also?
@user3733831 I have updated my answer on how to include the last 2.
1

try this-

$output=[];    
$i=0; 
$html='';
while($row = $stmt->fetch()) {    

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

$html .="<div class='col-sm-4'>
        <div class='room-box'>
          <img src='$thumb' class='img-responsive' >
          <h4>$name</h4>
        </div>
      </div>\n";

   if($i % 3 == 2) { 
      $html .= "</div>\n";
      $output[] = $html;
      $html = '';
   }
  $i++;
}
if($i%3!=0){
  $html .= "</div>\n";
  $output[] = $html;
  $html = '';
} 

Comments

0

Maybe the issue is mostly how you're trying to access the variables:

$output=[];    
$i=0; 
$html='';
while($row = $stmt->fetch()) {    

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

  $html.="<div class='col-sm-4'>
            <div class='room-box'>
              <img src='{$row['thumb']}' class='img-responsive'>
              <h4>{$row['name']}</h4>
            </div>
          </div>\n";

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

Do the records have fields called thumb and name?

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.