2017-01-10 152 views
-3

我正在使用PHP包括在我的网页上包含页眉和页脚。我在所有标题上都有一个导航栏,并且想知道是否有办法让header.phph在某些页面上不显示某些导航项目。例如,用户不需要从联系页面导航到联系页面,因此该导航项目不是必需的。希望有人能够协助。PHP包含,但只有某些东西

的index.php

<?php 
include('header.php'); 
?> 
<div class="third_bar"> 
    <div class="second_image"> 
    </div> 
    <div class="form"> 
     <form action= "search.php" method="post"> 
      <input type="text" id="search_bar" placeholder="" value="Search" max length="30" autocomplete="off" onMouseDown="active();" onBlur="inactive();"/><input type="submit" id="search_button" value="Go!"/> 
     </form> 
    </div> 
</div> 

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

的header.php

!doctype html> 

<html> 
    <head> 
     <meta charset="utf8"> 
     <link rel="icon" href="http://www.com/favicon.png?v=2"/> 
     <title>Wcom</title> 
     <link rel= "stylesheet" href="style5.css"/> 
    </head> 
    <script type="text/javascript"> 
     function active() { 
      var search_bar= document.getElementById('search_bar'); 
      if(search_bar.value == 'Search here') { 
       search_bar.value='' 
       search_bar.placeholder= 'Search here' 
      } 
     } 

     function inactive() { 
      var search_bar= document.getElementById('search_bar'); 
      if(search_bar.value == '') { 
       search_bar.value='Search here' 
       search_bar.placeholder= '' 
      } 
     } 
    </script> 
    <div id="container"> 
     <div class="top_section"> 
      <div class="first_image"> 
       <a href="#"><img src="logo.png"/></a> 
      </div> 
     </div> 

     <div class="nav_bar"> 
      <a href ="#"> About us</a> 
      <a href ="#"> Products & Pricing</a> 
      <a href ="contactpage.php"> Contact us</a> 
      <a href ="#"> login</a> 
     </div> 

<!doctype html> 
<html> 
    <head> 
     <meta charset="utf8"> 
     <link rel="icon" href="http://"/> 
     <title>?.com</title> 
     <link rel= "stylesheet" href="style5.css"/> 
    </head> 
    <script type="text/javascript"> 
     function active() { 
      var search_bar= document.getElementById('search_bar'); 
      if(search_bar.value == 'Search here') { 
       search_bar.value='' 
       search_bar.placeholder= 'Search here' 
      } 
     } 

     function inactive() { 
      var search_bar= document.getElementById('search_bar'); 
      if(search_bar.value == '') { 
       search_bar.value='Search here' 
       search_bar.placeholder= '' 
      } 
     } 
    </script> 
    <div id="container"> 
     <div class="top_section"> 
      <div class="first_image"> 
       <a href="#"><img src="logo.png"/></a> 
      </div> 
     </div> 

     <div class="nav_bar"> 
      <a href ="#"> About us</a> 
      <a href ="#"> Products & Pricing</a> 
      <a href ="contactpage.php"> Contact us</a> 
      <a href ="#"> login</a> 
     </div> 
+0

荣誉的网站上的工作威士忌! –

+0

那么有人必须这样做,它是一种艰苦的生活。 – Bill04

+1

你应该在'header.php'代码中实现一个逻辑,该代码决定显示哪些内容,哪些不显示。实际上你可以通过使用客户端逻辑来隐藏事物。 – arkascha

回答

2

一个简单的方法来做到这一点是增加有条件的header.php文件页面上。

说你必须在其中包含的页面名称每一页的开头的变量,例如在“contactpage.php”:

$page = 'contact'; 

(请注意,还有一种方式来获得的名称当前页面上的$_SERVER超全局变量)

然后你可以conditionnally打印您的链接在导航栏:

<div class="nav_bar"> 
<a href ="#"> About us</a> 
<a href ="#"> Products & Pricing</a> 
<?php if ($page !== 'contact') { ?> 
<a href ="contactpage.php"> Contact us</a> 
<?php } ?> 
<a href ="#"> login</a> 
+0

OP:人们不需要从联系页面导航到联系页面,所以这个导航项目不是必需的。 – Progrock

+0

@Progrock yup意思相反,谢谢! –

+0

谢谢Pierre,感谢帮助。现在唯一的问题是,我的标题样式表会覆盖我的索引页的样式表。我有我的索引,联系页面等样式表。目前只有两页网站。我是否需要为所有页面制作一个样式表? – Bill04