2017-01-10 168 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

相關問題