Include PHP Menu

Asked

Viewed 9,312 times

1

I’m having trouble 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 from the menu.php file in the index.php file, it accuses an error in the.php menu file line 2 of "<". I will post the codes to make it easier to understand the problem. This.php menu is for study purposes only.

File includes/menu.php:

<?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";

?>

  • in his menu.php has the tags <?php and ?> and no code PHP, you could remove the tags and see if it works.

  • this is the question friend, if I remove the php tag, how will I include my menu in other pages?

  • He’ll call the file .php. Or you have to do as @Allan Ramos' reply.

  • @Rafael2f, usually code snippets in PHP (in a file .php, obvious) in an HTML structure, not the other way around. That is, you will open the tag <?php only where you need to use PHP (print a variable on the screen, for example).

  • @Rafael2f do you want to call the menu in another view? You are using some framework?

  • I’m not using framework

Show 1 more comment

2 answers

5


Remove the tags <?php ?> menu.php file (You will leave only html, and save with the php extension as already).

and then will call you normally:

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

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

Where have you:

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

It should be 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 on this php page you could just take the <?php ?> being like this:

<head>
<title>Construção de interface em HTML e CSS</title>
<meta charset="UTF-8">
...
  • Milrak I did like Oce said and it worked, but I still don’t understand if this is good practice, how is the menu.php a php file without tags? i added the <html></html> tags, did it right or could leave without?

  • @rafael2f I’m sorry I don’t know if this is good programming practice, let the more experienced crowd respond. But if the problem is having a. php page only with html, just change the extension to html and include it in the html page -> include "includes/menu.html"; (I guess that way you won’t be sinning.

  • Hey Milrak, but include is not exclusive to php? I did how Voce said and worked until I edicted a <Section> tag in the index file, after it added returned an error. What might have happened?

  • @Rafael2f I just tested it here and it worked with html, so it is not unique. It shows the bug in the tag <section>

4

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

  • Put the echo right after the PHP opening tag, so that it identifies the HTML tags.
  • Take the PHP open/close tags - if you don’t use anything related to 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 a comment from you, and it seems like you’re looking to modularize HTML, which is good practice, since the same chunk of code will be used several times. I will post a code below giving a basic read on your code of 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 added 3 PHP files, being them:

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

I considered the content of <section> and <main> like the one that would be different inside 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>

php menu.

<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>
  • Dude, I have to do this for all tags? including the header? I’m really lost, I think your answer was very succinct.

  • @Rafael2f Comment updated.

  • Exactly that Alan, I did this way there too, thank you for clarifying the fact whether or not it is good practices.

Browser other questions tagged

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