gpt4 book ai didi

php - 如果下拉菜单中的子页面已打开,我如何提供菜单项 ID ="active"? PHP

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

如果一个菜单项有子页面,主页本身是不可点击的——只有它的子页面可以。我的问题是,如果子页面打开,我如何将 id="active"添加到菜单项?

恐怕解决方案无法通过 URL 实现,因为子页面的 URL 是:subpage.php?id=5 而在数据库中,pagessubpages 表具有相同的 page_id

所以使用 (basename($_SERVER['REQUEST_URI']) == 'subpage.php?id='.$page['page_id']) 给出了错误的结果。它使项目处于事件状态,例如 $subpage['subpage_id'] = $page['page_id'] 但我需要这样的结果:$subpage['page_id'] = $page[ 'page_id']..

如何在不需要 URL 的情况下实现这一目标?

表格:pages = page_id (AI, primary), page_name;
subpages = subpage_id (AI, primary), page_id, subpage_name;

我的 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 文件:

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();
}
}

现在,当任何子页面打开时,所有页面都处于“事件”状态。

总结:

我需要这样的结果:$subpage['page_id'] = $page['page_id']。我如何在不需要 URL 的情况下实现这一点?

最佳答案

可能是这样,我把当前子页面的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>

关于php - 如果下拉菜单中的子页面已打开,我如何提供菜单项 ID ="active"? PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57414677/

26 4 0