I'm trying to color a menu of items on an webpage, based on the item currently selected. I know the item chosen, based on $cgi->param('a'), so that part isn't a problem.
I've abstracted the logic into a small, standalone script that looks like this:
my @menu_options = ('home', 'donate', 'news', 'docs', 'download', 'gal
+lery', 'samples', 'users', 'developers', 'about');
my $items = @menu_options;
my $count = 1;
print qq{
<div class="sh">
<span class="doNotDisplay">Navigation:</span>
};
my $style = '';
foreach my $menu_item (@menu_options) {
$style = qq{style="background-color: #9943ac;"}
if ($action eq $menu_item && $action =~ /docs/);
$style = qq{style="background-color: #d8402b;"}
if ($action eq $menu_item && $action =~ /download/);
$style = qq{style="background-color: #ab0;"}
if ($action eq $menu_item && $action =~ /users/);
$style = qq{style="background-color: #6fab87;"}
if ($action eq $menu_item && $action =~ /developers/);
print qq{\t\t<a href="/$menu_item" $style>$menu_item</a>};
print " |" if ($count < $items);
print "\n";
$count++;
}
print "\t</div>\n";
In my actual code, I have a dispatch table that looks like this:
my %dispatch = (
(map { $_ => \&default } qw(
home donate news docs download gallery
docs users developers about
)),
samples => sub { \&samples($dbh) },
);
I'm trying to selectively color the background of the item selected. What am I missing in my sample code above?
I'm only trying to colorize 4 of the 10 menu options, and only one should be colorized at a time. They're in my dispatch table in the production code (not an array like the sample test above; I created that to try to isolate the problem).
Is there a better way to do this?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.