Include PHP Menu

1

I'm having difficulty including a php code with html on another page using include, in case the code is a menu written in html but with php extension. When having to include the code of the file menu.php in the file index.php it accuses an error in file menu.php line 2 of "<". I'll post the codes to make it easier to understand the problem. This menu.php is for study purposes only.

Includes / menu.php file:

<?php <head>
<title>Construção de interface em HTML e CSS</title>
<meta charset="UTF-8">
<style type="text/css">
  @import "estilo/layout.css";
</style>
</head>

<body>
  <div id="container">
    <header>
      <h1>The Door</h1>
    </header>
    <div id="login">
      <ol>
        <li><a href="">Login</a>
        </li>


      </ol>
    </div>

    <nav>

      <ol>
        <li><a href="">Principal</a>
        </li>
        <li><a href="">Faculdade</a>
        </li>
        <li><a href="">Links</a>
        </li>
        <li><a href="">Outros</a>
        </li>
      </ol>
    </nav>
    <section id="destaque">
      <figure>
        <img src="imagens/gatostop.jpg">
        <figcaption>Gatinhos zueiros</figcaption>
      </figure>
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Lorem ipsum dolor
        sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Lorem ipsum dolor sit amet,
        consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
      </p>
    </section>
    <main>
      <section>
        <article>
          <h3>Título 1</h3>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
            in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing
            elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
          </p>
        </article>
        <article>
          <h3>Título 2</h3>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
            in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing
            elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
          </p>
        </article>
      </section>
      <aside>
        <div>Link externo</div>
        <div>Conteúdo adicional</div>
        <div>Publicidade</div>
      </aside>
    </main>
    <footer>Development by
      <h4>Rafael<h4></footer>
		</div>
	</body>
?>

Index.php file:

<?php

include "includes/menu.php";

?>
    
asked by anonymous 15.05.2015 / 17:46

2 answers

5

Remove the <?php ?> tags from the menu.php file (You'll just leave html, and save with the php extension as it already is).

and then ça would normally call her:

<?php
include "includes/menu.php";
?>

The problem is occurring because you are trying to print wrongly in php.

Where to get:

<?php <head>
<title>Construção de interface em HTML e CSS</title>
<meta charset="UTF-8">
...

It should look like this:

<?php echo "<head>".
"<title>Construção de interface em HTML e CSS</title>".
"<meta charset="UTF-8">";

And since you only have html in that php page you could just have taken <?php ?> and so on:

<head>
<title>Construção de interface em HTML e CSS</title>
<meta charset="UTF-8">
...
    
15.05.2015 / 19:11
4

The problem is that it is interpreting HTML tags as part of PHP syntax, since you've put everything inside <?php ?> . To display HTML content in PHP you have 2 alternatives, which are:

  • Place echo right after the PHP opening tag so that it identifies HTML tags.
  • Take the PHP open / close tags - if you do not use anything related to the language in this code.

Example with echo

echo '
    <html>
        <head>
            <title>Meu Site</title>
        </head>
        <body>
            <div id="meuId">
                <p>Meu parágrafo</p>
            </div>
        </body>
    </html>
';

Example without opening tags

<html>
    <head>
        <title>Meu Site</title>
    </head>
    <body>
        <div id="meuId">
            <p>Meu parágrafo</p>
        </div>
    </body>
</html>

Modularizing HTML

I just read your comment, and it looks like you're looking to modularize HTML, which is good practice, since the same snippet of code will be used several times. I will post a code below giving a basic code in your code on how you could do this division.

index.php

<?php
    include 'template/header.php';
    incldue 'template/menu.php';
?>

<section>
    código da section
</section>
<main>
    código dentro do main
</main>

<?php
    include 'template/footer.php'
?>

Here I included the inclusion of 3 PHP files, which are:

  • header.php - This file will contain from the opening of your <html> to the opening of the <body> tag.
  • menu.php - This will contain your <nav> tag with the menu.
  • footer.php - This will contain the part of <footer> and closing of the tags </body> and </html> .

I considered the contents of <section> and <main> as the one that would be different within that page.

header.php

<!DOCTYPE html>
<html>
    <head>
        <title>Construção de interface em HTML e CSS</title>
        <meta charset="UTF-8">
        <style type="text/css">
            @import "estilo/layout.css";
        </style>
    </head>
    <body>

menu.php

<div id="container">
    <header>
        <h1>The Door</h1>
    </header>
    <div id="login">
        <ol>
            <li><a href="">Login</a></li>
        </ol>
    </div>
    <nav>
        <ol>
            <li><a href="">Principal</a></li>
            <li><a href="">Faculdade</a></li>
            <li><a href="">Links</a></li>
            <li><a href="">Outros</a></li>
       </ol>
    </nav>
</div>

footer.php

    <footer>Development by <h4>Rafael<h4></footer>
    </body>
</html>
    
15.05.2015 / 17:52