如何在foreach循环中使用.active

时间:2016-10-19 14:35:41

标签: php css foreach

我正在尝试为活动菜单项提供不同的颜色,但颜色保留在第一个菜单项(Home)上。我已经在我的CSS中放了一个活跃的课程。

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');

$menu = array(
    array('text' => 'Home', 'link' => 'index.php', 'target' => NULL),
    array('text' => 'Contact', 'link' => 'contact.php', 'target' => NULL),
    array('text' => 'Video', 'link' => 'video.php', 'target' => NULL), 
    array('text' => 'Thema', 'link' => 'thema.php', 'target' => NULL),
    array('text' => 'Numbers', 'link' => 'numbers.php', 'target' => NULL),
    array('text' => 'Login', 'link' => 'login.php', 'target' => NULL),
    array('text' => 'Logout', 'link' => 'indexlogout.php', 'target' => NULL),
    array('text' => 'Links', 'link' => 'links.php', 'target' => NULL),
    array('text' => 'Image Maps', 'link' => 'imagemap.php', 'target' => NULL)
);

function makeMenu($menu){
    $current_page = substr($_SERVER['REQUEST_URI'], 1);
    $current_page = str_replace('.php', '', $current_page);

    echo '<ul>';
    foreach ($menu as $key => $value){
        if($current_page == $key){
            echo '<li><a class="active" href="'.$value['link'].'" target="'.$value['target'].'">'.$value['text'].'</a></li>';
        }
        else {
            echo '<li><a href="'.$value['link'].'" target="'.$value['target'].'">'.$value['text'].'</a></li>';
        }
    }
    echo '</ul>';
}

makeMenu($menu);

&GT;

1 个答案:

答案 0 :(得分:0)

foreach中的$key将是数字的link。例如,第一项将为0,我认为这不是您所需要的。

您需要更改它并使用$value

中的function makeMenu($menu){ $current_page = substr($_SERVER['REQUEST_URI'], 1); echo '<ul>'; foreach ($menu as $value){ if($current_page == $value['link']){ echo '<li><a class="active" href="'.$value['link'].'" target="'.$value['target'].'">'.$value['text'].'</a></li>'; } else { echo '<li><a href="'.$value['link'].'" target="'.$value['target'].'">'.$value['text'].'</a></li>'; } } echo '</ul>'; } 值进行检查
  %% Load image %%
close all; clear all; clc;
img = imread('https://i.stack.imgur.com/yO8Nd.jpg');  %// read image
bw = img(:,:,1) > 128;  %// convert to binary mask
lb = bwlabel(bw,4);  %// extract distinct regions

%%

%% Create as many colors as needed
% example: 2

cmap=rand(2,3);  % make this yellow if needed

% lets convert to HSV, we can tune the intesity of the image better here
cmap=rgb2hsv(cmap);

% for each color, lets crate a set of colors.

for ii=1:size(cmap,1);


       colors{ii}= [cmap(ii,1)*ones(1,size(img,2)); cmap(ii,2)*ones(1,size(img,2)); linspace(0.3,1,size(img,2))].';
       %  Modify the limits of linspace
       % to achieve control over the limits
end

% Now we have the colors, lets create an image of vertical colors and mask
% it
cimage=zeros(size(img,1),size(img,2),3); % empthy color image
finalimage=cimage;
for ii=1:size(colors,2)
    colors{ii}=hsv2rgb(colors{ii});
    cimage=permute(reshape(repmat(colors{ii},[size(img,1),1,1]),[size(img,2),size(img,1),3]),[2,1,3]); % there is probably a simpler way 
    finalimage=finalimage+cimage.*repmat((lb==ii),[1 1 3]);
end


figure; imshow(finalimage, [], 'border', 'tight');