I have a class called ‘current-menu’, and I want to find its previous sibling and add a class called ‘previous-menu’, how can I achieve it with php only?
I can do this with jquery,
$("#menu-header .current-menu").parent().prev().find('a').addClass('current-previous');
but it is slow to add the previous class on the client side so I am thinking using the server side to archive it. Is it possible?
this is my html and php code that generate, ‘first-menu’, ‘current-menu’ and the ‘last-menu’,
<?php
foreach($items as $index => $item):
?>
<li><a href="#"<?php
if ($parent->pg_url == $item['pg_url']) echo ' class="current-menu"';
elseif($index == ($total_items - 1)) echo ' class="last-menu"';
elseif($index == '0') echo ' class="first-menu"';
?>><?php echo $item['mnu_name'];?></a></li>
<?php endforeach;?>
so it generate something like this,
<!--menu-->
<ul id="menu-header">
<li><a href="#" class="first-menu">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Venues</a></li>
<li><a href="#">How to visit</a></li>
<li><a href="#" class="current-menu">Active Space</a></li>
<li><a href="#" class="last-menu">Resources</a></li>
</ul>
<!--menu-->
So I want to add ‘previous-menu’ to this node,
<li><a href="#">How to visit</a></li>
to become this,
<li><a href="#" class="previous-menu">How to visit</a></li>
below is the data I get from the database,
Array
(
[0] => Array
(
[mnu_id] => 1
[mnu_name] => Home
[mnu_url] =>
[mnu_additional] =>
[mnu_order] => 1
[pg_url] => home
)
[1] => Array
(
[mnu_id] => 5
[mnu_name] => About
[mnu_url] =>
[mnu_additional] =>
[mnu_order] => 2
[pg_url] => about
)
[2] => Array
(
[mnu_id] => 6
[mnu_name] => Venues
[mnu_url] =>
[mnu_additional] =>
[mnu_order] => 3
[pg_url] => venues
)
[3] => Array
(
[mnu_id] => 3
[mnu_name] => How to visit
[mnu_url] =>
[mnu_additional] =>
[mnu_order] => 4
[pg_url] => how to visit
)
[4] => Array
(
[mnu_id] => 2
[mnu_name] => Project Archive
[mnu_url] =>
[mnu_additional] =>
[mnu_order] => 5
[pg_url] => projects
)
[5] => Array
(
[mnu_id] => 8
[mnu_name] => Active Space
[mnu_url] =>
[mnu_additional] =>
[mnu_order] => 6
[pg_url] => active space
)
[6] => Array
(
[mnu_id] => 4
[mnu_name] => Contact
[mnu_url] =>
[mnu_additional] =>
[mnu_order] => 7
[pg_url] => contact
)
[7] => Array
(
[mnu_id] => 7
[mnu_name] => News
[mnu_url] =>
[mnu_additional] =>
[mnu_order] => 8
[pg_url] =>
)
[8] => Array
(
[mnu_id] => 9
[mnu_name] => Resources
[mnu_url] =>
[mnu_additional] =>
[mnu_order] => 9
[pg_url] => resources
)
)
Here is solution. I assume that some items may have 2 classes.