动态导航函数在php中的单独文件


Dynamic Navigation Function in php in separate file

我试图使一个动态的php导航系统,显示活动选项卡,但似乎不能使显示活动选项卡部分的工作。虽然我找到了更简单的方法来实现这一点,但它们都需要在原始文件中使用php。我想能够只是调用返回或回声导航和设置活动页面的函数。任何建议都是非常欢迎的!

我的index.php文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">    
<!-- Helps with rendering on different size machines -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Keeps user from scrolling -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<!-- Main CSS style -->
<link rel="stylesheet" href="media/css/mainStyle.css">
<!-- Import php form with functions for navigation-->
<title> Shoe Site </title>
</head>
<body>
    <?php
    require 'mainPhp.php'; 
    echo Navigation(); 
    ?>
        <!-- Jumbotron -->
        <div class="jumbotron">
            <h1>Welcome to the Shoe Site</h1>
            <p class="lead">Description of site.</p>
        </div>
        <!-- use this on items that are on sale
        <h3><span class="label label-primary" width="auto"> Items on Sale </span></h3>
        -->
        <!-- On Sale columns -->
        <h2 class="sectionHeader"> Shoes on Sale Below </h2>
        <div class="row">
            <div class="col-lg-4">
                <h2>Heading</h2>
                <p>Description</p>
                <p><a class="btn btn-primary" href="#" role="button">Add to cart &raquo;</a></p>
                <p><span class="label label-primary" width="auto">Sale! </span> </p>
            </div>
        </div>
        </br>
        <hr>
        </br>
        <!-- Regular Item columns -->
        <h2 class="sectionHeader"> Entire Shoe Catalog </h2>
        <div class="row">
            <div class="col-lg-4">
                <h2>Heading</h2>
                <p>Description</p>
                <p><a class="btn btn-primary" href="#" role="button">Add to cart &raquo;</a></p>
            </div>
        </div>
        <!-- Site footer -->
        </br>
        <hr>
        <footer class="footer">
            <p>&copy; Brianna Jones XD</p>
        </footer>
    </div> <!-- /container -->
    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</body>
</html>

我的mainPhp.php文件:

<?php
function PageName() {
  return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
$current_page = PageName();
function Navigation(){
//can not get the page to stay active
$current_page = PageName();
echo ('
    <div class="container">
        <div class= "masthead">
            <nav>
                <h1 class="text-muted">Shoe Site</h2>
                <ul class="nav nav-justified">
                    <li class="$current_page == ''index.php'' ? ''active'':NULL"><a href="index.php">Home</a></li>
                    <li class="$current_page == ''cart.php'' ? ''active'':NULL"><a href="cart.php">Cart</a></li>
                    <li class="$current_page == ''admin.php'' ? ''active'':NULL"><a href="admin.php">Admin</a></li>
                </ul>
            </nav>
        </div>
');
}
?>

为您的mainPhp.php文件试试这段代码

<?php
function PageName() {
    return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
$current_page = PageName();
function Navigation(){
    //can not get the page to stay active
    $current_page = PageName();
    echo '<div class="container">';
        echo '<div class= "masthead">';
            echo '<nav>';
                echo '<h1 class="text-muted">Shoe Site</h2>';
                echo '<ul class="nav nav-justified">';
                    $btnActiveClass = ($current_page=='index.php') ? 'active' : 'NULL';
                    echo '<li class="'.$btnActiveClass.'"><a href="index.php">Home</a></li>';
                    $btnActiveClass = ($current_page=='cart.php') ? 'active' : 'NULL';
                    echo '<li class="'.$btnActiveClass.'"><a href="index.php">Cart</a></li>';
                    $btnActiveClass = ($current_page=='admin.php') ? 'active' : 'NULL';
                    echo '<li class="'.$btnActiveClass.'"><a href="index.php">Admin</a></li>';
                echo '</ul>';
            echo '</nav>';
        echo '</div>';
    echo '</div>';
}
?>