php - 如何在使用 PHP 的动态网站中正确使用 GET 方法?
问题描述
抱歉标题非常模糊,我不知道如何描述我的问题...我正在修复我的网站以使用 PHP 使其动态化,并且我正在尝试更改我的侧导航栏。因此,当我单击选项 2 时,它会更改选项 2 的背景颜色,依此类推。但是,我不太确定在哪里实现foreach
以不同的方式设置选项。
.flexContainer {
display: flex;
}
.flexContainer>div {
margin: 10px;
padding: 20px;
font-size: 20px;
text-align: center;
}
.sidebarContainer {
flex-shrink: 1;
}
.sidebar {
width: 200px;
background-color: #dd1d5e;
overflow: auto;
border-radius: 30px;
}
.sidebar a {
display: block;
color: white;
padding: 16px;
text-decoration: none;
}
.sidebar a:hover:not(.active) {
background-color: #ffffff;
color: #dd1d5e;
}
.sidebar a.selected {
background-color: #ffffff;
color: #dd1d5e;
}
<section>
<?php
if (isset($_GET['categoryId'])) {
$selectedPage = $_GET['categoryId'];
} else {
$selectedPage = '1';
}
if ($selectedPage == '1') {
echo "<div class=\"flexContainer\">
<div class=\"sidebarContainer\">
<div class=\"sidebar\">
<a href=\"category.php\" class=\"selected\">Holiday</a>
<a href=\"category.php\">Vegetarian</a>
<a href=\"category.php\">Dessert</a>
<a href=\"category.php\">Cultural Cuisine</a>
</div>
</div>";}
elseif ($selectedPage == '2') {
echo "<div class=\"flexContainer\">
<div class=\"sidebarContainer\">
<div class=\"sidebar\">
<a href=\"category.php\">Holiday</a>
<a href=\"category.php\" class=\"selected\">Vegetarian</a>
<a href=\"category.php\">Dessert</a>
<a href=\"category.php\">Cultural Cuisine</a>
</div>
</div>";}
elseif ($selectedPage == '3') {
echo "<div class=\"flexContainer\">
<div class=\"sidebarContainer\">
<div class=\"sidebar\">
<a href=\"category.php\">Holiday</a>
<a href=\"category.php\">Vegetarian</a>
<a href=\"category.php\" class=\"selected\">Dessert</a>
<a href=\"category.php\">Cultural Cuisine</a>
</div>
</div>";}
elseif ($selectedPage == '4') {
echo "<div class=\"flexContainer\">
<div class=\"sidebarContainer\">
<div class=\"sidebar\">
<a href=\"category.php\">Holiday</a>
<a href=\"category.php\">Vegetarian</a>
<a href=\"category.php\">Dessert</a>
<a href=\"category.php\" class=\"selected\">Cultural Cuisine</a>
</div>
</div>";
}
?>
<!-- <div class="flexContainer">-->
<!-- <div class="sidebarContainer">-->
<!-- <div class="sidebar">-->
<!-- <a href="category.php" class="selected">Holiday</a>-->
<!-- <a href="category.php">Vegetarian</a>-->
<!-- <a href="category.php">Dessert</a>-->
<!-- <a href="category.php">Cultural Cuisine</a>-->
<!-- </div>-->
<!-- </div>-->
侧面导航栏看起来像这样,例如,当我单击 时Dessert
,我希望使用 PHP 为该选项设置不同的样式。我怎样才能做到这一点?我尝试添加
<a href=\"category.php?categoryId=$categoryId\" class=\"selected\">Holiday</a>
<a href=\"category.php?categoryId=$categoryId\">Vegetarian</a>
<a href=\"category.php?categoryId=$categoryId\">Dessert</a>
<a href=\"category.php?categoryId=$categoryId\">Cultural Cuisine</a>
在这里,但它没有工作。它显示选择了多个条...任何建议将不胜感激!
解决方案
您可以使用 get 参数并为导航创建单独的 css 样式文件并动态包含它们。例如:
<?php
$categryId = isset($_GET['categoryId']) ? $_GET['categoryId'] : 1;
?>
...
<link rel="stylesheet" href="css/category_<?=$categoryId;?>.css">
或者,如果您有不同的导航结构,您可以通过包含带有 html 结构的 php 文件来执行相同的操作。
推荐阅读
- ios - 跟踪仅包含 WebView 的应用的退出流程
- filter - 0 到 65535 通过低通数字滤波器
- google-cloud-platform - GCF:Pubsub 调用旧版本
- xml - 如何解决android stuido中的元素类型错误
- python - Python从Excel中的字符串列中提取度量单位和附加数字
- flutter - Flutter_gen 的 Android Studio 自动导入
- tableau-desktop - 在 Tableau 中的同一条形图中绘制每个类别的总销售额以及总销售额
- flutter - 请求 GET 时如何更新(而不是添加)容器中的数据?飘飘然
- sql-server - 带有 SQL Server 2019 Express 的 Windows Server 2019 上的 Hangfire
- python - Python。如何导出 PDF 专色或隐藏某些颜色?