아직 초보여서 오류가 있으면 알려주세요^^
<!-- index.php -->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Menu demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
</head>
<body>
<?php
$MENUS = [
[
"name" => "home",
"href" => "/"
],
[
"name" => "dropdown test",
"dropdown" => [
[
"name" => "test",
"href" => "/test"
],
[
"name" => "dropdown test",
"dropdown" => [
[
"name" => "test1",
"href" => "/test1"
]
]
]
]
]
];
function dropdownMenu($active, $menus) {
$html = '';
$isActive = false;
foreach ($menus as $element) {
if (!isset($element['dropdown'])) {
$menuHref = strtolower(trim($element['href'], '/'));
$activeHref = strtolower(trim($active, '/'));
$activeMenu = $menuHref === $activeHref;
if ($activeMenu) $isActive = true;
$html .= '<li><a class="dropdown-item' . ($activeMenu ? " active" : "") . '" href="' . $element['href'] . '">' . $element['name'] . '</a></li>';
} else {
$menuData =Menu($active, $element['dropdown']);
if ($menuData[1]) $isActive = true;
$html .= '<li class="dropdown dropend"><a class="dropdown-item dropdown-toggle' . ($menuData[1] ? " active" : "") . '" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false" data-bs-auto-close="outside">' . $element['name'] . '</a><ul class="dropdown-menu">' . $menuData[0] . '</ul></li>';
}
}
return [
$html,
$isActive
];
}
function menu($active, $menus) {
$html = '';
$isActive = false;
foreach ($menus as $element) {
if (!isset($element['dropdown'])) {
$menuHref = strtolower(trim($element['href'], '/'));
$activeHref = strtolower(trim($active, '/'));
$activeMenu = $menuHref === $activeHref;
if ($activeMenu) $isActive = true;
$html .= '<li class="nav-item"><a class="nav-link' . ($activeMenu ? " active" : "") . '" aria-current="page" href="' . $element['href'] . '">' . $element['name'] . '</a></li>';
} else {
$menuData = dropdownMenu($active, $element['dropdown']);
if ($menuData[1]) $isActive = true;
$html .= '<li class="nav-item dropdown"><a class="nav-link dropdown-toggle' . ($menuData[1] ? " active" : "") . '" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false" data-bs-auto-close="outside">' . $element['name'] . '</a><ul class="dropdown-menu">' . $menuData[0] . '</ul></li>';
}
}
return [
$html,
$isActive
];
}
?>
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<? echo menu($_SERVER['REQUEST_URI'], $MENUS)[0] ?>
</ul>
</div>
</div>
</nav>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
</body>
</html>
게시글 목록
| 번호 | 제목 |
|---|---|
| 17927 | |
| 17926 | |
| 17922 | |
| 17921 | |
| 17915 | |
| 17910 | |
| 17907 | |
| 17892 | |
| 17888 | |
| 17879 | |
| 17878 | |
| 17874 |
JavaScript
글자수 제한 가장 쉬운판.중 하나
|
| 17873 | |
| 17872 |
PHP
포럼 출력소스 [ 샘플본 ]
|
| 17871 |
PHP
게시판별 카테고리 만들기.
|
| 17870 |
PHP
댓글이나 답글은 로직이 같다.
|
| 17869 |
PHP
카테고리 만드는 예제.
|
| 17868 | |
| 17866 |
JavaScript
문구 복사하기 자바스크립트
|
| 17865 | |
| 17864 | |
| 17863 |
PHP
랜덤값 만들기 겹치기는
|
| 17862 | |
| 17859 | |
| 17856 | |
| 17845 |
JavaScript
페이지 로딩 애니메이션
|
| 17835 | |
| 17834 |
PHP
데이타 1개 가져오기
5
|
| 17826 | |
| 17823 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기