Using require, where to use this tag?

Asked

Viewed 208 times

1

I had asked this question before, but I was not so specific so I decided to reformulate my code here so that you understand better. Here I have the main file that will be used as template for all pages:

<!DOCTYPE html>

<html lang="pt-BR">
<head>
<title>Guia Norte Capixaba</title>
<meta charset="utf-8">
<link rel="icon" href="img/guianortecapixaba.ico" type="image/gif" sizes="42x42">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="js/jquery-3.2.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<style>
    /* Remove the navbar's default margin-bottom and rounded borders */
    .navbar {
        margin-bottom: 0;
        border-radius: 0;
    }

    /* Set height of the grid so .sidenav can be 100% (adjust as needed) */
    .row.content {height: 450px}

    /* Set gray background color and 100% height */
    .sidenav {
        padding-top: 20px;
        background-color: #f1f1f1;
        height: 100%;
    }

    /* Set black background color, white text and some padding */
    footer {
        background-color: #7faec3;
        color: white;
        padding: 15px;
    }

    /* On small screens, set height to 'auto' for sidenav and grid */
    @media screen and (max-width: 767px) {
        .sidenav {
            height: auto;
            padding: 15px;
        }
        .row.content {height:auto;}
    }
</style>
</head>


<body>

<nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>

        </div>
        <div class="collapse navbar-collapse" id="myNavbar">
            <ul class="nav navbar-nav">
                <li class="active"><a href="http://localhost:8888/GuiaNorteCapixaba" target="_self">Home</a></li>
                <li><a href="#">Sobre</a></li>
                <li><a href="#"><span class="glyphicon glyphicon-pencil"></span> Cadastre-se Gratuitamente</a></li>
                <li><a href="#">Contato</a></li>
            </ul>

        </div>
    </div>
</nav>

<!--Aqui o cabeçalho do portal-->

<div class="jumbotron">
    <img src="img/header-template.png" class="img-thumbnail" alt="Cinque Terre" width="100%">

</div>

</div>
<!--Fim do cabeçalho do portal-->

<!--Barra esquerda-->

<div class="container-fluid text-center">
    <div class="row content">
        <div class="col-sm-2 sidenav">
            <img src="img/banner-divulgacao.png" class="img-rounded" alt="Divulgação" width="100%">

        </div>
<!--Essa é a página central-->

<div class="col-sm-8 text-left">

    <h3>O que você esta procurando? Digite aqui:</h3>
    <form class="form-inline" action="busca.php" method="post">
        <div class="form-group">
            <input type="text" class="form-control" id="palavra" placeholder="Digite aqui..." name="palavra">
        </div>
        <div class="form-group">
            <label for="cidade">Selecione a cidade:</label>
            <select name="cidade" class="form-control" id="cidade">
                <option value="sao-gabriel-da-palha">São Gabriel da Palha</option>
                <option value="sao-domingos-do-norte">São Domingos do Norte</option>
                <option value="vila-valerio">Vila Valério</option>
            </select>
        </div>
        <button type="submit" class="btn btn-default" value="Buscar">Buscar</button>
    </form>
    <hr>
        </div>

<!--fim da página central-->

        <div class="col-sm-2 sidenav">
            <img src="img/banner-divulgacao-2.png" class="img-rounded" alt="Divulgação" width="100%">
        </div>


    </div>
</div>

</body>
<footer class="container-fluid text-center">
    <p>Desenvolvido por <a href="https://andreyferraz.com" target="_blank">Andrêy Ferraz</a> </p>
</footer>
</html>

So here I have a page called about.php on which I’m calling this template via require, so the content of my page isn’t staying in the body where it should be, but after the footer, how do I fix it? Here the page about:

<!DOCTYPE html>

<html lang="pt-BR">
<head>
    <title>Guia Norte Capixaba</title>
    <meta charset="utf-8">
    <link rel="icon" href="img/guianortecapixaba.ico" type="image/gif" sizes="42x42">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script src="js/jquery-3.2.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <style>
        /* Remove the navbar's default margin-bottom and rounded borders */
        .navbar {
            margin-bottom: 0;
            border-radius: 0;
        }

        /* Set height of the grid so .sidenav can be 100% (adjust as needed) */
        .row.content {height: 450px}

        /* Set gray background color and 100% height */
        .sidenav {
            padding-top: 20px;
            background-color: #f1f1f1;
            height: 100%;
        }

        /* Set black background color, white text and some padding */
        footer {
            background-color: #7faec3;
            color: white;
            padding: 15px;
        }

        /* On small screens, set height to 'auto' for sidenav and grid */
        @media screen and (max-width: 767px) {
            .sidenav {
                height: auto;
                padding: 15px;
            }
            .row.content {height:auto;}
        }

    </style>
    <?php
    require 'principal.php';
    ?>
</head>


<body>


<!--Fim do cabeçalho do portal-->

<!--Barra esquerda-->

        <!--Essa é a página central-->


            <p>aqui entra o meu texto</p>


        <!--fim da página central-->
</body>

</html>

Aqui como o arquivo ta ficando, o texto teria que aparecer no centro abaixo do formulário

  • You cannot include it in the site you are including or with the content you are including. If you imagine the html coming from the page principal.php and assuming it’s the first one you show, placed where you have the require, will get two labels <body>, etc, and therefore with an incorrect document. You have to segment the page into blocks and only include each piece in the right place

  • @Isac thank you so much for the information, but I’m still learning would have as you show me how it would look?

1 answer

2


Wpfan,

You cannot call a require that results in two tags .

In a weird example, it would be the same thing as two people trying to wear the same T-shirt.

Imagine your HTML as a puzzle and require it as the fit of this puzzle.

So I suggest you make a footer.php and a header.php separate from your pages about.php and main.php

Because?

Because then you eliminate the error of your require. It turns out you’re calling the main.php within the tag on your.php page and this is causing the browser to get lost when it comes to reading HTML.

I suggest the following:

Create the files header.php; footer.php; main.php;

Inside header.php add:

<html lang="pt-BR">
<head>
    <title>Guia Norte Capixaba</title>
    <meta charset="utf-8">
    <link rel="icon" href="img/guianortecapixaba.ico" type="image/gif" sizes="42x42">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script src="js/jquery-3.2.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <style>
        /* Remove the navbar's default margin-bottom and rounded borders */
        .navbar {
            margin-bottom: 0;
            border-radius: 0;
        }

        /* Set height of the grid so .sidenav can be 100% (adjust as needed) */
        .row.content {height: 450px}

        /* Set gray background color and 100% height */
        .sidenav {
            padding-top: 20px;
            background-color: #f1f1f1;
            height: 100%;
        }

        /* Set black background color, white text and some padding */
        footer {
            background-color: #7faec3;
            color: white;
            padding: 15px;
        }

        /* On small screens, set height to 'auto' for sidenav and grid */
        @media screen and (max-width: 767px) {
            .sidenav {
                height: auto;
                padding: 15px;
            }
            .row.content {height:auto;}
        }

    </style>
</head>
<body>

Inside footer.php add:

<!--fim da página central-->

        <div class="col-sm-2 sidenav">
            <img src="img/banner-divulgacao-2.png" class="img-rounded" alt="Divulgação" width="100%">
        </div>


    </div>
</div>
<footer class="container-fluid text-center">
    <p>Desenvolvido por <a href="https://andreyferraz.com" target="_blank">Andrêy Ferraz</a> </p>
</footer>
</body>
</html>

And within your main.php so:

<nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>

        </div>
        <div class="collapse navbar-collapse" id="myNavbar">
            <ul class="nav navbar-nav">
                <li class="active"><a href="http://localhost:8888/GuiaNorteCapixaba" target="_self">Home</a></li>
                <li><a href="#">Sobre</a></li>
                <li><a href="#"><span class="glyphicon glyphicon-pencil"></span> Cadastre-se Gratuitamente</a></li>
                <li><a href="#">Contato</a></li>
            </ul>

        </div>
    </div>
</nav>

<!--Aqui o cabeçalho do portal-->

<div class="jumbotron">
    <img src="img/header-template.png" class="img-thumbnail" alt="Cinque Terre" width="100%">

</div>

</div>
<!--Fim do cabeçalho do portal-->

<!--Barra esquerda-->

<div class="container-fluid text-center">
    <div class="row content">
        <div class="col-sm-2 sidenav">
            <img src="img/banner-divulgacao.png" class="img-rounded" alt="Divulgação" width="100%">

        </div>
<!--Essa é a página central-->

<div class="col-sm-8 text-left">

    <h3>O que você esta procurando? Digite aqui:</h3>
    <form class="form-inline" action="busca.php" method="post">
        <div class="form-group">
            <input type="text" class="form-control" id="palavra" placeholder="Digite aqui..." name="palavra">
        </div>
        <div class="form-group">
            <label for="cidade">Selecione a cidade:</label>
            <select name="cidade" class="form-control" id="cidade">
                <option value="sao-gabriel-da-palha">São Gabriel da Palha</option>
                <option value="sao-domingos-do-norte">São Domingos do Norte</option>
                <option value="vila-valerio">Vila Valério</option>
            </select>
        </div>
        <button type="submit" class="btn btn-default" value="Buscar">Buscar</button>
    </form>
    <hr>

There on.php you put together all the pages you want this way:

require 'header.php';
require 'principal.php';
<p>aqui entra o seu texto</p>
require 'footer.php';
  • Thank you very much, that was very helpful. now I’m going to test here!

  • still printing the text in the wrong place

  • Post a photo of where the text is coming out and where you wanted it to be

  • I edited the question and added the image. Note that the text was printed in the bottom left corner, when it should be printed below the search form

  • The text has been adding correctly, what is wrong is how the html has been dismembered. I will edit the question to with the answer to solve your problem

  • changed footer.php and the main.php, see if it solves.

  • now mess is all... I don’t understand why this is so complicated

  • Solved, I had to create a require for each div, ie for each block!

Show 3 more comments

Browser other questions tagged

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