Include menu in all pages

Asked

Viewed 4,597 times

0

I made the menu of a system I am doing in html and php, I wanted to insert this menu in all pages, however I wanted to change the active menu according to the open page, is possible ? My current menu is in html for now:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<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>
<script defer src="js/fontawesome-all.min.js"></script>



<?php
require 'config.php';
//require 'verificalogin.php';
$sql = new Sql();
$nome = $_SESSION['nome'];
?>

<link rel="stylesheet" href="css/cssbase.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Administrativo DoUp</title>
</head>
<body>
    <!-- inicio da navbar -->
    <header>
        <div class="navbar navbar-default navbar-fixed-top" role="navigation">
            <div class="container"> 
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span> 
                    </button>
                    <img src="imagens/logo.png" id="logo">
                </div>
                <div class="collapse navbar-collapse">
                    <ul class="nav navbar-nav">
                        <li class="active"><a href="#">Inicio</a></li>
                        <li ><a href="noticias.php">Noticias</a></li>
                    </ul>
                    <ul class="nav navbar-nav navbar-right">
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                                <i class="fas fa-user-circle fa-lg"></i> 
                                <strong>Igor de Oliveira</strong>
                                <i class="fas fa-caret-down "></i>
                            </a>
                            <ul class="dropdown-menu">
                                <li>
                                    <div class="navbar-login">
                                        <div class="row">
                                            <div class="col-lg-8">
                                                <p class="text-left"><strong>Igor Oliveira</strong></p>
                                                <p class="text-left small">[email protected]</p>
                                                <p class="text-left small">Praia da Costa</p>
                                                <p class="text-left">
                                                    <a href="#" class="btn btn-primary btn-block btn-sm">Alterar Senha</a>
                                                </p>
                                            </div>
                                        </div>
                                    </div>
                                </li>
                                <li class="divider"></li>
                                <li>
                                    <div class="navbar-login navbar-login-session">
                                        <div class="row">
                                            <div class="col-lg-12">
                                                <p>
                                                    <a href="#" class="btn btn-danger btn-block">Deslogar</a>
                                                </p>
                                            </div>
                                        </div>
                                    </div>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </header>
</body>
</html>

2 answers

1


Good morning, you can create separate files in php and give require_once to import it anywhere on the page, including in other files, this is very useful not to rewrite the code every time in each file

Example:

header.php

<header>
    Aqui você faz seu header todo dentro de um arquivo chamado = header.php
</header>

then just give one:

<?php require_once "header.php"; ?>

in the part that wants to insert the header

<!DOCTYPE html>
<html>
    <head>
        <title>Título da página</title>
        <meta charset="UTF8"/>
    </head>
    <body>
        <?php require_once "header.php" ?>
    </body>
</html>
  • It worked, but the rest of the content is under the menu, would know how to leave it under the menu in an elegant way without margin top?

  • I think the ideal would be include <?php include 'header.php';?> because with the require, if the . php does not find the file it will lock the whole page loading.

-1

You can do this with Jquery, example:

<html>
<head>
<title>Include Common Files</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="header"></div><br />
<div id="content">
Main Content
</div><br />
<div id="footer"></div>
<script>
$("#header").load("header.html");
$("#footer").load("footer.html");
</script>
</body>
</html>

has a more detailed explanation here, if you want to understand better: https://phpsmashcode.com/tips-and-tricks/include-common-files-in-html#. Wlqakpzg3iw

Browser other questions tagged

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