br Automatic

1

I have a little problem, I have a page that makes the listing of several movies that I have in the database, which is placed on a table but only that I create that every 5 movies of that an Automatic br!

HowcouldIdothis?

Code:

<!DOCTYPEhtml><html><head><scripttype="text/javascript">
        function exit(){
            var link = window.location.href;
            var split = link.split("/");
            var url = split[2];
            window.location = "http://" + url + "/IMMStart/principal/sair";
        }
    </script>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="<?php echo PATH; ?>css/bootstrap.min.css" rel="stylesheet">
    <?php
    if(isset($_COOKIE['key-login'])){
        $keylogin = $_COOKIE['key-login'];
        $pesquser = BD::conn()->prepare("SELECT * FROM 'auth-keys' WHERE auth_key = ?");
        $pesquser->execute(array($keylogin));
        if($pesquser->rowCount() == 0){
        setcookie("key-login", "",time()-3600, "/");
        echo '<script>alert("Cookie Invalido!"); location.href="'.PATH.'"; </script>';
        }else{
        $respesquser = $pesquser->fetchObject();
        $iduser = $respesquser->id_user;
        $queryuser = BD::conn()->prepare("SELECT * FROM 'users' WHERE id = ?");
        $queryuser->execute(array($iduser));
        $resuser = $queryuser->fetchObject();
        $firstacesspospass = $resuser->firstacessposrecoverpass;
        $firstacess = $resuser->firstacess;
        if($firstacesspospass == 1){
            header("Location: " . PATH . "pospass");
        }
        if($firstacess == 1){
            header("Location: " . PATH . "start");          
        }
    }
    }else{
        echo '<script>alert("Você não tem permissões para aceder a esta area!"); location.href="'.PATH.'"; </script>';
    }
    $precat = BD::conn()->prepare("SELECT * FROM 'filmes'");
    $precat->execute();
    ?>
    <title>IMM | Start</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        body{
            background: #030000;
        }
        .link{
            color: #fff;
        }
        .link:hover{
            text-decoration: underline;
            color: #fff;
        }
        .background{
            background: #e60002;
            width: 100%;
        }
        .txt{
            color: #fff;
        }
        div#filmes{
            margin-left: 1px;
            text-align: center;
            margin-top: 5px;
        }
        div#barra{
            border-top: 1px solid #000;
            height: 42px;
            width: 100%;
            background: #e60002;
            color: #fff;
        }
        .title{
            text-align: center;
            font-size: 28px;
        }
        .bk{
            background: #000;
            color: #fff;
        }
        .marg{
            margin-left: 5px;
            background: 
        }
        .marg:hover{
            background: red;
        }
        .mt{
            margin-top: 5px;
        }
    </style>
</head>
<body>
<?php include_once "pages/menu.html"; ?>
<table class="mt">
<tr>
    <?php while($res = $precat->fetchObject()){ ?>
    <th><a class="link" href="<?php echo PATH; ?>detalhes/url/<?php echo $res->url; ?>"><img class="marg" width="264" height="351" src="<?php echo PATH; ?>thumbs/<?php echo $res->patch_thumb; ?>" /><br><?php echo $res->nome; ?></a></th>
    <?php } ?>
</tr>
</table>
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">IMM | START</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        Desaja realmente terminar a sessão da IMM | START?
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>
        <button type="button" class="btn btn-primary" onclick="exit()">Sim, eu quero sair!</button>
      </div>
    </div>
  </div>
</div>
<script src="<?php echo PATH; ?>js/jquery.js"></script>
<script src="<?php echo PATH; ?>js/bootstrap.min.js"></script>
</body>
</html>
    
asked by anonymous 06.04.2018 / 12:09

1 answer

5

You have to handle this in your loop.

Example:

$c = 0;
while($res = $precat->fetchObject()){

    ... seu código aqui para <br> depois

    if ($c <= 4) {
        $c++;
    } else if ($c > 4) {
        echo '<br>';
        $c = 0;
    }

    ... seu código aqui para <br> antes
}

You set the contactor $c and set it to 0.

While $c is less than or equal to 4 (4 because it considers 0 as well), it adds 1.

If $c is greater than 4, it prints <br> and returns counter to 0.

  

Your error on the table

You need to hit the table loop, basically this way it solves:

$c = 0;
while($res = $precat->fetchObject()){

  if ($c == 0) echo '<th>';

    echo 'CONTEUDO';

    if ($c <= 4) {
        $c++;
    } else if ($c > 4) {
        echo '</th>';
        $c = 0;
    }

}

In your code:

<table class="mt">

      <?php 

      $c = 0;
      while($res = $precat->fetchObject()){ 

      if ($c == 0) echo '<tr>';

        ?>
        <td>
          <a class="link" href="<?php echo PATH; ?>detalhes/url/<?php echo $res->url; ?>">
            <img class="marg" width="264" height="351" src="<?php echo PATH; ?>thumbs/<?php echo $res->patch_thumb; ?>" />
            <br><?php echo $res->nome; ?>
          </a>
        </td>
        <?php 

          if ($c <= 4) {
            $c++;
          } else if ($c > 4) {
            echo '</tr>';
            $c = 0;
          }

        } ?>
</table>
    
06.04.2018 / 12:21