<br> Automatico

Asked

Viewed 62 times

0

I have a small problem, I have a page that lists several movies that I have in the database, which is placed in a table but only that I create that every 5 movies a br Automatic!

Imagem do site sem o <br>

How could I do that?

Code:

<!DOCTYPE html>
<html>
<head>
    <script type="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>

1 answer

4


You have to treat that 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 define the contactor $c and arrow the value 0.

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

Case $c is greater than 4, it prints the <br> and turn the counter to 0.


Your mistake on the table

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

$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>
  • For some reason he’s giving a kind of margin-top. And he doesn’t give the <br>

  • I’ve got the error, it has to do with the structure of the table, thanks for the help!

  • Actually what you have to do is generate the right table because if not it just creates column in the same row. The <br> example works: <?php $x = 0; $c = 0; while($x < 30){ echo 'FILM ' . $c . ' - '; if ($c <= 4) { $c++; } Else if ($c > 4) { echo '<br>'; $c = 0; } $x++; }

  • Exactly. You need to loop the table, every 5 <td> you create a new <tr>.

  • @Tomásdinis I put in your code.

  • Thank you the page is already working correctly.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.