I know the question sounds a bit out of line, but trying many options without success ( 1 2 3 ) I turn to esSO for help.
I have my navbar
in a PHP file that I call with the statement include
on all my other pages.
I make the call like this:
<head>
<style>...</style>
</head>
<body>
<?php include "include/navbar.php"; ?>
...
<script>...</script>
</body>
And my navbar.php
is like this:
<div class="navbar">
<ul>
<li>Inicio</li>
<?php if ($_SESSION["usuario"]["privilegio"] == 1){?>
<li>Admin</li> <?php }?>
</ul>
</div>
The code works fine as it is, the sentence I make to the Admin Menu is to validate if the user has permissions to see that menu or not. I request these data with PDO in a login form, but that is getting off topic.
The problem with such code is that for some reason (the menu in navbar.php
is much longer) when I load each page it messes up the CSS and the page load order.
I managed to fix the mess and crash on load by putting that navbar in a echo
, like this:
<?php
echo
'
<div class="navbar">
<ul>
<li>Inicio</li>
<?php if ($_SESSION["usuario"]["privilegio"] == 1){?>
<li>Admin</li> <?php }?>
</ul>
</div>
'
?>
The problem is that in this way, I lose the statement in PHP, apparently it should be plain text, the strange thing is that the browser hides it, you might think that it is because it works, but it is not, because the admin menu is always shown.
How can I include such a PHP statement inside an echo?
Do you suggest another way to do it?
If you ask me if you break the site rules how:
- Creation of minimal, complete and verifiable example
- too wide
- based on opinions
Please let me know, so I can rephrase my question, otherwise, if the goal is understood, I am grateful for your help.
Good day!
Mixing PHP/HTML code with constant block openings and closings produces confusing code that is hard to parse and hard to fix when there are errors.
To avoid this you can concatenate everything into a variable (
$html
in this case), and thus work all the time within a single PHP block. At the end you print the variable that you have been concatenated.For example: