【问题标题】:How can I have an element change based on the URL? PHP Switch statement?如何根据 URL 更改元素? PHP Switch 语句?
【发布时间】:2015-09-17 10:18:34
【问题描述】:

短版: 使用 php 包含文件时,如何在导航菜单中突出显示当前页面?

完整版: 我有一个网站,我使用 navmenu.php 文件通过 php include 语句生成导航菜单。我需要在导航栏中用顶部和底部边框标记当前查看的页面。在继续使用 php 包含文件的同时执行此操作的最佳方法是什么?

起初我想如果使用 if/elseif 语句,但到目前为止我最好的想法是在 PHP 中创建一个 switch 语句来搜索 url,然后进行相应的调整。那可能吗?如果是这样,我该如何提取页面名称?例如,如果 url 是 www.domain.com/contact.php 我想拉 'contact'

<?php
   $page = [file name];

   switch ($page) {
       case "services.php":
          //Add the border to the services page <li>
       break;

       case "about.php":
          //Add the border to the about us page <li>
          break;

       case "contact.php":
          //Add the border to the contactpage <li>
       break;

       default:
          //Add the border to the home page <li>
}
?>

基于this question,看来我应该可以使用

switch($_GET['????'])

提前感谢您的帮助!

添加: 这是php文件中的代码:

<?php
session_start();
    echo "<nav class='nav-wrap'>
    <ul class='navLeft'>
        <li><a href='http://www.domain.com' title='Home'>HOME</a></li>
        <li><a href='http://www.domain.com/about.php' title='About Us'>ABOUT US</a></li>
        <li><a href='http://www.domain.com/portfolio.php' title='View our Portfolio'>PORTFOLIO</a></li>
    </ul>
    <ul class='navRight'>
        <li><a href='http://www.domain.com/giving.php' title='Giving Back'>GIVING BACK</a></li>
        <li><a href='http://www.domain.com/services.php' title='Services'>SERVICES</a></li>
        <li><a href='http://www.domain.com/contact.php' title='Contact Us'>CONTACT US</a></li>
    </ul>
</nav>";
?>

第二次编辑: 谢谢@anujnehra,那部分开始有意义了。我一直在尝试确定一旦开关识别出当前页面如何进行。我正在考虑使用隐藏的 css 类并将其更改为显示可能效果最好,但我不确定如何控制 PHP 开关中的 css。

这是我目前所拥有的:

CSS:

.currentPage {
  border-bottom: 2px solid #ffffff;
  border-top: 2px solid #ffffff;
  display: none;
}

PHP 文件:

  print_r(pathinfo("www.domain.com/contact.php"));

  Array ( 
    [dirname] => www.domain.com 
    [basename] => contact.php 
    [extension] => php 
    [filename] => contact 
  );

  switch(filename){
    case "about":
      .currentPage#about {display:inline;}
      break;

    case "portfolio":
      .currentPage#portfolio {display:inline;}
      break;

    case "giving":
      .currentPage#giving {display:inline;}
      break;

    case "services":
      .currentPage#services {display:inline;}
      break;

    case "contact":
      .currentPage#contact {display:inline;}
      break;

    default:
      .currentPage#home {display:inline;}
      break;
  }

echo "<nav class='nav-wrap'>
        <ul class='navLeft'>
           <li><a href='http://www.domain.com' title='Home' class='currentPage' id='home'>HOME</a></li>
           <li><a href='http://www.domain.com/about.php' title='About Us' class='currentPage' id='about'>ABOUT US</a></li>
           <li><a href='http://www.domain.com/portfolio.php' title='View our Portfolio' class='currentPage' id='portfolio'>PORTFOLIO</a></li>
        </ul>
        <ul class='navRight'>
           <li><a href='http://www.domain.com/giving.php' title='Giving Back' class='currentPage' id='giving'>GIVING BACK</a></li>
           <li><a href='http://www.domain.com/services.php' title='Services' class='currentPage' id='services'>SERVICES</a></li>
           <li><a href='http://www.domain.com/contact.php' title='Contact Us' class='currentPage' id='contact'>CONTACT US</a></li>
        </ul>
     </nav>";
?>

【问题讨论】:

  • 创建一个 $_SESSION 变量来存储当前页面,“include”文件将使用这个全局变量来知道要突出显示的选项。举个例子,我们需要更多的代码来理解你这样做的方式。
  • 您可以使用 javascript 从 url 中获取页面名称,然后在同一脚本文件中使用 switch case 将菜单类设置为活动状态。
  • @ShaneSkinner,注意 anuj nehra 的回答,我认为这正是您所需要的。将“文件名”字段放入您的开关中。

标签: php url switch-statement element


【解决方案1】:
    You can use pathinfo for example 
    print_r(pathinfo("www.domain.com/contact.php"));

    Array ( 
    [dirname] => www.domain.com 
    [basename] => contact.php 
    [extension] => php 
    [filename] => contact 
    ) 

Use filename to pull contact 

【讨论】:

  • 或者只是pathinfo($filename, PATHINFO_FILENAME)
  • Anuj Nehra 和@JoseManuelAbarcaRodríguez 谢谢,我想我得到了它的一部分,但我仍然没有找到如何从开关中更改 css。你有没有可以给我看的例子?
【解决方案2】:

@Shane Skinner
您必须创建一个名为 dynamic.css 的新文件并使其为空其余将由代码本身完成

<link rel="stylesheet" type="text/css" href="dynamic.css" />

<?php
$pageName = pathinfo("www.domain.com/contact.php");

$aboutCss = '.currentPage#about {display:inline;}' . "\n";
$portfolioCss = '.currentPage#portfolio {display:inline;color: black;}' . "\n";
$givingCss = '.currentPage#giving {display:inline;}' . "\n";
$servicesCss = '.currentPage#services {display:inline;}' . "\n";
$contactCss = '.currentPage#contact {display:inline;color: red;}' . "\n";
$defaultCss = '.currentPage#home {display:inline;}' . "\n";

switch ($pageName['filename']) {
    case "about":
        createDynamicCss($aboutCss);
        break;

    case "portfolio":
        createDynamicCss($portfolioCss);
        break;

    case "giving":
        createDynamicCss($givingCss);
        break;

    case "services":
        createDynamicCss($servicesCss);
        break;

    case "contact":
        createDynamicCss($contactCss);
        break;

    default:
        createDynamicCss($defaultCss);
        break;
}

function createDynamicCss($cssToWrite) {
    $file = fopen("dynamic.css", "r+");
    fwrite($file, $cssToWrite);
    fclose($file);
}

echo "<nav class='nav-wrap'>
        <ul class='navLeft'>
           <li><a href='http://www.domain.com' title='Home' class='currentPage' id='home'>HOME</a></li>
           <li><a href='http://www.domain.com/about.php' title='About Us' class='currentPage' id='about'>ABOUT US</a></li>
           <li><a href='http://www.domain.com/portfolio.php' title='View our Portfolio' class='currentPage' id='portfolio'>PORTFOLIO</a></li>
        </ul>
        <ul class='navRight'>
           <li><a href='http://www.domain.com/giving.php' title='Giving Back' class='currentPage' id='giving'>GIVING BACK</a></li>
           <li><a href='http://www.domain.com/services.php' title='Services' class='currentPage' id='services'>SERVICES</a></li>
           <li><a href='http://www.domain.com/contact.php' title='Contact Us' class='currentPage' id='contact'>CONTACT US</a></li>
        </ul>
     </nav>";
?>

此代码只是一个示例,尝试让您自己的代码进行更多优化。

【讨论】:

    【解决方案3】:

    尝试使用 JavaScript。你可能根本不需要 PHP。

    我对此进行了测试,它确实有效。

    <html>
    <head>
    <script>
    function highlight (item) {
      switch (item) {
        case "about.php":
          document.getElementById("about").setAttribute('style', 'font-weight: bold; color: red; font-size:150%;');
          break;
        case "contact.php":
          document.getElementById("contact").setAttribute('style', 'font-weight: bold; color: red; font-size:150%;');
        break;
        default:
          document.getElementById("home").setAttribute('style', 'font-weight: bold; color: red; font-size:150%;');
      }
    }
    var index = window.location.pathname.lastIndexOf("/");
    var filename = window.location.pathname.substr(index + 1);
    window.onload = function () {highlight(filename)};
    </script>
    </head>
    <body>
    <nav id="site-navigation" class='nav-wrap'>
        <ul class='navLeft'>
            <li><a id="home" href='http://www.domain.com' title='Home'>HOME</a></li>
            <li><a id="about" href='http://www.domain.com/about.php' title='About Us'>ABOUT US</a></li>
            <li><a id="portfolio" href='http://www.domain.com/portfolio.php' title='View our Portfolio'>PORTFOLIO</a></li>
        </ul>
        <ul class='navRight'>
            <li><a id="giving" href='http://www.domain.com/giving.php' title='Giving Back'>GIVING BACK</a></li>
            <li><a id="services" href='http://www.domain.com/services.php' title='Services'>SERVICES</a></li>
            <li><a id="contact" href='http://www.domain.com/contact.php' title='Contact Us'>CONTACT US</a></li>
        </ul>
    </nav>
    </body>
    </html>
    

    【讨论】:

    • 小心使用 window.onload 它会覆盖你拥有的任何东西。
    【解决方案4】:

    通常的方法是为活动元素设置一个额外的类。如果手动完成,这可能会很乏味,所以我建议定义一个函数来生成菜单项。

    function menuItem($content, $link, $title = null) {
        if ($_SERVER['REQUEST_URI'] == $link) {
            $class = "active";
        } else {
            $class = "";
        }
        if ($title === null) {
            $title = strtoupper($content);
        }
        echo "<li><a href=\"{$content}\" title=\"{$title}\" class=\"{$class}\">{$content}</a></li>";
    }
    

    像这样使用它:

    <ul>
        <?php echo menuItem('About Us', '/about.php'); ?>
        <?php echo menuItem('Portfolio', '/portfolio.php', 'View our portfolio'); ?>
    </ul>
    

    【讨论】:

      猜你喜欢
      • 2019-04-01
      • 2021-04-11
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      相关资源
      最近更新 更多