用php实现引导导航栏的动态化
我在bootstrap中创建了一个网站,现在我正在努力使它更具动态性。我遵循的课程不是使用引导。这是不是很明显我错过了什么?请你看一下我的密码好吗 我不想让这篇文章的代码过多,所以只显示nav.php和array.php,我已经将nav.php包含在 任何帮助都将不胜感激-我刚刚开始我的旅程,所以请温柔地对我说,就像我不知道很多一样,因为这是真的,我不知道!:-用php实现引导导航栏的动态化,php,html,css,bootstrap-4,Php,Html,Css,Bootstrap 4,我在bootstrap中创建了一个网站,现在我正在努力使它更具动态性。我遵循的课程不是使用引导。这是不是很明显我错过了什么?请你看一下我的密码好吗 我不想让这篇文章的代码过多,所以只显示nav.php和array.php,我已经将nav.php包含在 任何帮助都将不胜感激-我刚刚开始我的旅程,所以请温柔地对我说,就像我不知道很多一样,因为这是真的,我不知道!:- //nav.php which I took out of the header <a class="navbar-brand"
//nav.php which I took out of the header
<a class="navbar-brand" href="index.html">Harrisons Hair & Beauty</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="navbarNav">
<ul class="navbar-nav">
<?php
foreach($navItems as $item){
echo "<li class=\"nav-item\">
<a class=\"nav-link\" href=\"$item[slug]\">$item[title]</a>
</li>";
}
?>
</ul>
</div>
// array.php
<?php
$navItems = [
[
slug => "index.php"
title => "Home"
],
[
slug => "about.php",
title => "About"
],
[
slug => "gallery.php",
title => "Gallery"
],
[
slug => "other.php",
title => "Other Services"
],
[
slug => "contact.php",
title => "Find Us"
],
[
slug => "test.php",
title => "Test Page"
]
];
?>
为了连接echo语句中的字符串,可以使用 回声$项目['slug']。。。 或 回响{$item['slug']}。。。。 还要确保$navItems数组的声明在echo输出语句之前
祝你好运。你的数组格式不正确。在index.php后面包装键并添加逗号。还可以将foreach$navItems更改为$item{,将foreach$navItems更改为$key=>$item{,以检索嵌套数组值nav items 通过这些调整,您的代码可以正常工作
<!doctype html>
<html lang="en">
<head>
<!-- required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<!-- Libraries - jQuery first, then Popper.js, then Bootstrap.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</head>
<body>
<?php
$navItems = [
[ // keys (i.e. slug and title) need surrounding ""
"slug" => "index.php", // missing comma
"title" => "Home"
],
[
"slug" => "about.php",
"title" => "About"
],
[
"slug" => "gallery.php",
"title" => "Gallery"
],
[
"slug" => "other.php",
"title" => "Other Services"
],
[
"slug" => "contact.php",
"title" => "Find Us"
],
[
"slug" => "test.php",
"title" => "Test Page"
]
];
?>
<!-- nav.php which I took out of the header -->
<a class="navbar-brand" href="index.html">Harrisons Hair & Beauty</a>
<button style = "color: #0000FF !important; border: 1px solid blue !important;" class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
toggle menu<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="navbarNav">
<ul class="navbar-nav">
<?php
foreach($navItems as $key => $item){
echo "<li class=\"nav-item\" style='color: #FFFFFF !important;'>
<a class=\"nav-link\" href=\"$item[slug]\">$item[title]</a>
</li>";
}
?>
</ul>
</div>
</body>
</html>
“动态”是什么意思?如果有用户登录或浏览器中出现一些错误,动态会显示一些不同的项目。将echo;替换为echo“”;您好。我希望在不重新加载页面的情况下实现从一个页面切换到另一个页面,并且如果我想添加另一个菜单项,例如,我希望只添加该菜单项。”它会出现在数组中吗?嗨,Lovelace,非常感谢你发现了这一点。奇怪的是它仍然不起作用吗?已经发布了工作代码,看一看,希望能有所帮助。注意:您原来的foreach$navItems as$item{更改为foreach$navItems as$key=>$item{检索嵌套数组中的Navitem。非常感谢您的帮助。非常感谢!