Check page and assign DIV

Asked

Viewed 111 times

1

I have 10 pages, a part of the layout is equal in 9 of them, so I will make a include to spare me the trouble of repeating it 9 times.

The thing is, I’d like to use that 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 is needed.

For example, if I’m on the www.teste.com.br page/products inside that include, changes the div teste1, and the other 9 continue the pattern.

I don’t have code for this, because I haven’t started the project yet, I wonder if anyone has any example for me to take advantage.

  • You don’t quite understand the problem. You want a page that receives data via include? Do you have a "mom" page where you want to include one of the 10 files according to some condition? What changes on different page is the style or structure of HTML? You intend to provide some sort of identifier to know which of the 10 pages is on?

  • 1

    Get the part that interests you from the PHP URL, and make the condition with it

  • @Enoqueduarte then, you would have some example of how to create this condition?

  • 1

    <?php if (basename($_SERVER['PHP_SELF']) == "pagespread") { print "my html"; } ?>

  • @Felipestoker, explaining my question better to give you a suitable example: why make 9+1 pages that call the same layout, if you can make one with such a layout and change only the data that will go in it? If there are 10 pages, there would be no reason to do 10 includes, just copy the layout for each one and that’s it.

1 answer

1


It would really be better to use a template file loading the content through parameters in the URL, but if you are used to this structure, and/or prefer the URL to stick to the file names simply, follow a small example:

php contact.

<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>

php menu.

<?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 php contact. is going to include the <script src="js/contato.js"></script> and bonus was an example of how to apply the active class in the menu.

Browser other questions tagged

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