简体   繁体   English

PHP动态导航菜单的主动样式

[英]PHP dynamic navigation menu active styling

I want to create a dynamic menu in PHP and based on what page they are on the menu will have different styling. 我想在PHP中创建一个动态菜单,根据它们在菜单上的哪个页面,它们将具有不同的样式。 I have this but it's not how I want it to be :( 我有这个,但这不是我想要的:(

This is the array I have, containing info from the database 这是我拥有的数组,包含来自数据库的信息

Array(

    [Home] => Array
        (
            [url] => Home
            [name] => Home
            [is_home] => 1
        )

    [About] => Array
        (
            [url] => About
            [name] => About
            [is_home] => 0
        )

    [Contact] => Array
        (
            [url] => Contact.php
            [name] => Contact
            [is_home] => 0
        )

)

This is what I currently have, 这就是我目前拥有的

    if(isset($_GET["p"])) {
     if(in_array($page_name, $navigation[$page_name])) {
          $navigation[$page_name]["name"] = "<span>{$navigation[$page_name]["name"]}</span>";
     }
}
foreach ($navigation as $nav) {
     echo "<li><a href=\"" . strtolower($nav["url"]) . "\">{$nav["name"]}</a></li>";
}

This is how the page_name variable looks 这是page_name变量的外观

$page_name = current(explode(".", ucfirst(strtolower($_GET["p"]))));

As you can see this inserts the span tags in the navigation menu name so this works but that's not how I want it to be. 如您所见,这会在导航菜单名称中插入span标签,因此可以正常工作,但这不是我想要的那样。 I want to add class="active" the the list item that is the current page. 我想在当前页面的列表项中添加class =“ active”。 I just don't know how to do it 我只是不知道该怎么做

I hope you understand what I mean and sorry for any messy indentation that occurred when pasting the code in here. 希望您理解我的意思,对于在此处粘贴代码时出现的任何混乱缩进感到抱歉。

//Edit //编辑

The fetch and array code 提取和数组代码

$mysql->query("SELECT `page_name`, `is_home` FROM `pages` ORDER BY `order` ASC");

        $navigation_items = array();
        while ($mysql->fetch($row)){
            $navigation_items[] = array(
                "url"   => $row["page_name"],
                "name"  => current(explode(".", $row["page_name"])),
                "is_home"  => $row["is_home"]
            );
        }

        return $navigation_items;

First of all the array you provided is reformatted which means you changed the indexes into page names which isn't necessary. 首先,您提供的数组将重新格式化,这意味着您将索引更改为不必要的页面名称。 This is how you can achieve what you want: 这样可以实现您想要的:

<?php
$menu = $Db->fetchAll("SELECT * FROM `menu`"); //or whatever method you're using to get data from the database.
$current = null;
if(isset($_GET['p'])) {
  $current = current(explode(".", ucfirst(strtolower($_GET["p"]))));
}
$navigation = '';
for($i=0;$i<count($menu);$i++) {
  $url = ucfirst(strtolower($menu[$i]['url']));
  if($current == $url)
    $class = ' class="active"';
  else
    $class = '';
  $name = '<span'.$class.'>'.$menu[$i]['name'].'</span>';
  $navigation .= "<li><a href='{$url}'>{$name}</a></li>";
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM