I didn't code for years and I probably forgot everything, but I'm trying to start again to develop my personal website. I would like to put the div header and the div footer in a .php file so I don't have to manually modify every single page in case of modifications. My idea was like:
header-footer.php
<?php function head()
{echo '<ul>
<li><a href="tutorial.html"' if($current == "tutorial") {echo 'class="menu_link_active"';} else {echo 'class="menu_link"';} '>Tutorials</a></li>
<li><a href="contact.html"' if($current == "contact") {echo 'class="menu_link_active"';} else {echo 'class="menu_link"';} '>Contatti</a></li>
</ul>';}
function foot()
{echo '...html...';} ?>
The function head() should recognize the current page to highlight it. Then in the pages of my website I include the header-footer.php like this:
<head><?php include("php/header-footer.php"); ?><head>
Calling into the the two functions:
<body><?php $current = "projemi";
head();?>
...html code...
<?php foot(); ?></body>
My questions are: 1) the page displays this error: ----syntax error, unexpected 'if' (T_IF), expecting ',' or ';'---- and it referres to the head function. Where is the error? 2) Is it correct the process to obtain what I want or you would suggest something different?
Thanks you so much guys, and forgive me for my bad english!
You have to pass the value into the function to use
<?php
function head($current){
echo '<ul>
<li><a href="tutorial.html"';
if($current == "tutorial") {
echo 'class="menu_link_active"';
}else {
echo 'class="menu_link"';
}
echo '>Tutorials</a></li><li><a href="contact.html"';
if($current == "contact") {
echo 'class="menu_link_active"';
} else {
echo 'class="menu_link"';
}
echo '>Contatti</a></li></ul>';
}
?>
Call your function like
<?php
$current = 'someVal';
head($current);
?>
Well, you forgot to end your echo statement with a semicolon first off, hence the error.
echo '<ul>
<li><a href="tutorial.html"'; if($current == "tutorial") {echo 'class="menu_link_active"';} else {echo 'class="menu_link"';} '>Tutorials</a></li>
<li><a href="contact.html"'; if($current == "contact") {echo 'class="menu_link_active"';} else {echo 'class="menu_link"';} '>Contatti</a></li>
</ul>';
Also, suggestion, I believe this would be more efficient (I'd recommend to just develop an MVC-like system, but that's a bit tad too complicated for what you want do to):
echo '<ul>
<li><a href="tutorial.html"'.(($current === 'tutorial') ? 'class="menu_link_active"' : 'class="menu_link"').'>Tutorials</a></li>
<li><a href="contact.html"'.(($current === 'contact') ? 'class="menu_link_active"' : 'class="menu_link"').'>Contatti</a></li>
</ul>';