gpt4 book ai didi

php - php中的动态导航功能在单独的文件中

转载 作者:行者123 更新时间:2023-11-28 05:05:33 25 4
gpt4 key购买 nike

我正在尝试制作一个显示事件选项卡的动态 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>';
}

?>

关于php - php中的动态导航功能在单独的文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39843652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com