Reputation:
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?
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
.
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']
..
How can I achieve that without the need of URL?
Tables:
pages = page_id (AI, primary), page_name;
subpages = subpage_id (AI, primary), page_id, subpage_name;
My subpages.php file:
<?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:
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']
.
How can I achieve that without the need of URL?
Upvotes: 2
Views: 109
Reputation: 5878
Maybe like this, i added the id of the current sub-page as a variable::
<?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>
Upvotes: 1