[英]How can I give menu item id="active", if its subpage in a drop-down menu is open? PHP
If a menu item has sub-pages, main page itself is not clickable - only its sub-pages are.如果菜单项有子页面,则主页面本身是不可点击的——只有它的子页面是可点击的。 My question is, how can i add id="active" to menu item in case its sub-page is open?
我的问题是,如果它的子页面打开,我如何将 id="active" 添加到菜单项?
I am afraid the solution can't be achieved with URL, because the URL for subpages is: subpage.php?id=5
and in the database, pages
and subpages
tables have the same page_id
.恐怕不能用URL解决,因为子页面的URL是:
subpage.php?id=5
并且在数据库中, pages
和subpages
表具有相同的page_id
。
So using (basename($_SERVER['REQUEST_URI']) == 'subpage.php?id='.$page['page_id'])
gives a wrong result.. It makes items active such as $subpage['subpage_id'] = $page['page_id']
but I need a result like this: $subpage['page_id'] = $page['page_id']
..所以使用
(basename($_SERVER['REQUEST_URI']) == 'subpage.php?id='.$page['page_id'])
给出了错误的结果.. 它使诸如$subpage['subpage_id'] = $page['page_id']
项目处于活动状态$subpage['subpage_id'] = $page['page_id']
但我需要这样的结果: $subpage['page_id'] = $page['page_id']
..
How can I achieve that without the need of URL?如何在不需要 URL 的情况下实现这一目标?
Tables: pages = page_id (AI, primary), page_name; subpages = subpage_id (AI, primary), page_id, subpage_name;
表:
pages = page_id (AI, primary), page_name; subpages = subpage_id (AI, primary), page_id, subpage_name;
pages = page_id (AI, primary), page_name; subpages = subpage_id (AI, primary), page_id, subpage_name;
My subpages.php file:我的 subpages.php 文件:
<?php
include_once('classes.php');
$page = new Page;
$subpage = new Subpage;
$pages = $page->fetch_all();
$subpages = $subpage->fetch_all();
?>
<div class="sidenav">
<?php foreach ($pages as $page) { ?>
<button class="dropdown-btn"
<?php foreach ($subpages as $subpage) {
//DOWN HERE IS THE PROBLEM!
if ($subpage['subpage_id'].$page['page_id']) {
echo('id="active" ');
} }?> >
<?php echo $page['page_name']; ?>
<i class="fa fa-caret-down"></i>
</button>
//DOWN HERE I MAKE SUB-PAGES ACTIVE
<div class="dropdown-container">
<?php foreach ($childpages as $childpage): ?>
<a
<?php
if (basename($_SERVER['REQUEST_URI']) == 'subpage.php?id='.$subpage['subpage_id'].'') {
echo('id="active-sub" ');
}
?>
href="subpage.php?id=<?php echo $childpage['subpage_id']; ?>">
<?php echo $childpage['subpage_name']; ?>
</a>
<?php endforeach; ?>
</div>
<?php } ?>
</div>
classes.php file: classes.php 文件:
class Page {
public function fetch_all(){
global $pdo;
$query = $pdo->prepare("SELECT * FROM pages");
$query->execute();
return $query->fetchAll();
}
public function fetch_data($page_id) {
global $pdo;
$query = $pdo->prepare("SELECT * FROM pages WHERE page_id = ?");
$query->bindValue(1, $page_id);
$query->execute();
return $query->fetch();
}
}
class Subpage {
public function fetch_all(){
global $pdo;
$query = $pdo->prepare("SELECT * FROM subpages");
$query->execute();
return $query->fetchAll();
}
public function fetch_data($subpage_id) {
global $pdo;
$query = $pdo->prepare("SELECT * FROM subpages WHERE subpage_id = ?");
$query->bindValue(1, $subpage_id);
$query->execute();
return $query->fetch();
}
}
Right now, I get all pages 'active' when any sub-pages is open..现在,当任何子页面打开时,我让所有页面都处于“活动状态”。
Summary:概括:
I need a result like this: $subpage['page_id'] = $page['page_id']
.我需要这样的结果:
$subpage['page_id'] = $page['page_id']
。 How can I achieve that without the need of URL?如何在不需要 URL 的情况下实现这一目标?
Maybe like this, i added the id of the current sub-page as a variable::也许像这样,我将当前子页面的 id 添加为变量:
<?php
include_once('classes.php');
$page = new Page;
$subpage = new Subpage;
$pages = $page->fetch_all();
$subpages = $subpage->fetch_all();
$currentSubpageId = intval($_GET['id']),
?>
<div class="sidenav">
<?php foreach ($pages as $page) { ?>
<button class="dropdown-btn"
<?php foreach ($subpages as $subpage) {
if($currentSubpageId == $subpage['subpage_id'] && $subpage['page_id'] == $page['page_id'])
{
echo('id="active" ');
}
}?> >
<?php echo $page['page_name']; ?>
<i class="fa fa-caret-down"></i>
</button>
//DOWN HERE I MAKE SUB-PAGES ACTIVE
<div class="dropdown-container">
<?php foreach ($childpages as $childpage): ?>
<a
<?php
if (basename($_SERVER['REQUEST_URI']) == 'subpage.php?id='.$subpage['subpage_id'].'') {
echo('id="active-sub" ');
}
?>
href="subpage.php?id=<?php echo $childpage['subpage_id']; ?>">
<?php echo $childpage['subpage_name']; ?>
</a>
<?php endforeach; ?>
</div>
<?php } ?>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.