PHP包含但仅限于某些内容

时间:2017-01-10 08:30:37

标签: php html

我正在使用php include在我的网页上包含页眉和页脚。我在所有标题上都有一个导航栏,想知道是否有办法让header.phph不显示某些页面上的某些导航项。例如,人们不需要从联系人页面导航到联系人页面,因此不需要此导航项。希望有人可以提供协助。

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

1 个答案:

答案 0 :(得分:2)

一种简单的方法是在header.php页面上添加条件。

假设您在每个页面的开头都有一个包含页面名称的变量,例如“contactpage.php”:

$page = 'contact';

(注意还有一种方法可以在$_SERVER超全局变量上获取当前页面的名称)

然后您可以在导航栏中有条件地打印链接:

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