2011-07-24 44 views
0

我有一個名爲「當前菜單」的類,我想找到它的前一個兄弟並添加一個名爲「前一個菜單」的類,我怎麼能只用php實現它?如何查找以前的項目,並使用php添加一個類

我可以用jQuery做到這一點,

$("#menu-header .current-menu").parent().prev().find('a').addClass('current-previous'); 

,但它是緩慢增加客戶端上的,所以我現在用的是服務器端存檔想着以前的類。可能嗎?

這是我的HTML和PHP代碼生成,「第一菜單」,「目前的菜單」和「最後的菜單」,

<?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;?> 

所以產生這樣的事情,

<!--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--> 

所以我想補充'以前的菜單'到這個節點,

<li><a href="#">How to visit</a></li> 

成爲這個,

<li><a href="#" class="previous-menu">How to visit</a></li> 
下面

是我從數據庫中獲取數據,

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 
     ) 

) 

回答

1

這裏是解決方案。我假設一些項目可能有2個類。

<?php 
$items[0]['class'] = "first_menu "; 
$items[$total_items - 1]['class'] = "last-menu "; 

foreach ($items as $index => &$item) { 
    if (!$item['class']) { 
     $item['class'] = ''; 
    } 
    if ($parent->pg_url == $item['pg_url']) { 
     $item['class'] .= "current-menu "; 
     if ($index > 0) { 
      $items[$index - 1]['class'] .= "previous-menu "; 
     } 
    } 
} 

foreach($items as $index => $item): 
?> 

<li><a href="#"<?php 
if ($item['class']) echo ' class="' . trim($item['class']) . '"'; 
?>><?php echo $item['mnu_name'];?></a></li> 

<?php endforeach;?> 
+0

THanks爲此答案! – laukok

相關問題