mercredi 27 décembre 2017

Add HTML breaker with PHP if statement

I have a PHP loop listing results from a database:

foreach($_SESSION['all'] as $result) {
echo $result;
}

What I have done is write an if statement in PHP so that every time 4 results have been outputted, execute a line of code. In order to do this I have declared '$i = 0;' outside of this loop and then inside the loop I have '$i++;'. I then say:

if ($i %4 == 0) {
//execute code inside this if statement
}

I then put a breaker inside this loop so that every 4 results, there would be a breaker ('<br />'). It looks like this:

if ($i %4 == 0){
echo "<br />";
echo $i;
}

When I look at the source code of the site the '<br />' is there but it doesn't move the rest of the information to another line. When I add a different line of code it shows, for example, if I print '<p>Hello</p>', it outputs 'Hello'. It just seems to be the '<br />' that doesn't work. This results in all the results after the first 4 being off the end of the screen.

Here is the whole page and a screenshot of the output:

<?php

session_start();

?>

<section class="hero is-dark is-halfheight is-bold">
<div class="hero-head">

</div>

  <div class="hero-body">

    <div class="container has-text-centered">

      <div class="columns">

      <?php

              $i = 0;

              foreach($_SESSION['all'] as $result) {

              echo '<div class="column is-3">';

              echo '<a href="#">';
              echo '<div class="box has-text-centered">';
              echo $result;
              echo '</div>';
              echo '</a>';

              echo '</div>';

              $i++;

              if ($i %4 == 0){

                echo "<br />";
                echo $i;

              }

              }

      ?>

      </div>

    </div>

  </div>

<div class="hero-foot">
  <?php echo $i; ?>
  <div class="container has-text-centered">
      <a class="button is-light is-small" href="add.php">
        Add Client
      </a>
    </div>
    <br>
</div>

</section>

And...

Screenshot of output in browser

Thanks in advance for any answers. Help will be greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire