Verify page and assign DIV

1

I have 10 pages, one part of the layout is the same in 9 of them, so I'll make a include to spare my job of repeating this 9 times.

What happens is that I would like to use this include on every page, and when that part of the layout was different from the others, I would do some sort of validation and change the CSS or whatever it needed.

For example, if I am on the page www.teste.com.br/produtos within this include, changes the% div_with%, and in the other 9 remains the default.

I do not have code for this, because I have not started the project yet, I would like to know if anyone has an example so I can enjoy it.

    
asked by anonymous 07.08.2014 / 16:48

1 answer

1

It would really be better to use a template file by loading the content via parameters in the URL, but if you are used to this structure, and / or prefers to have the file names simply, / p>

contact.php

<html>
<head>
<title>Demo</title>
</head>

<body>

<?php
include('menu.php');
?>

<div class="conteudo">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur eget varius urna. Quisque convallis augue quis magna consequat eleifend. Nulla posuere sit amet lectus sed porta. Ut quis mauris luctus, lobortis ligula et, interdum massa. Praesent varius tempor laoreet. Vestibulum vulputate massa vel nunc vulputate, ut pellentesque massa rutrum. Aenean posuere, arcu eget eleifend auctor, diam lorem volutpat massa, vel commodo quam urna a mauris.
</div>

</body>
</html>

menu.php

<?php
$pagina_atual = basename($_SERVER['PHP_SELF']);

if ($pagina_atual == 'contato.php') {
    echo '<script src="js/contato.js"></script>';
}

?>
<ul id="menu" class="clearfix">
     <li<?php echo ($pagina_atual == 'index.php' ? ' class="ativo"' : ''); ?>>
         <a href="index.php">Início</a>
     </li>
     <li<?php echo ($pagina_atual == 'sobre.php' ? ' class="ativo"' : ''); ?>>
         <a href="sobre.php">Sobre</a>
     </li>
     <li<?php echo ($pagina_atual == 'contato.php' ? ' class="ativo"' : ''); ?>>
         <a href="contato.php">Contato</a>
     </li>
</ul>

As you can see, only when the page is contact.php will it include <script src="js/contato.js"></script> and bonus was an example of how to apply the active class in the menu.

    
07.08.2014 / 22:28