首页 > 解决方案 > 如何在使用 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>

在这里,但它没有工作。它显示选择了多个条...任何建议将不胜感激!

标签: phphtmlcss

解决方案


您可以使用 get 参数并为导航创建单独的 css 样式文件并动态包含它们。例如:

<?php
$categryId = isset($_GET['categoryId']) ? $_GET['categoryId'] : 1;
?>

...
<link rel="stylesheet" href="css/category_<?=$categoryId;?>.css">

或者,如果您有不同的导航结构,您可以通过包含带有 html 结构的 php 文件来执行相同的操作。


推荐阅读