2011-08-19 70 views
0

我已經在下面展示了一個css menu.php,其中包含ul li的不同級別,當我執行include時會調用它。 enter image description here用於動態菜單css的foreach php

,並在這裏你的代碼:

<div class="cont_menu"> 
     <div class="menu"> 

     <ul> 
     <li><a class="hide" href="#" style="font-weight:bold;">CATALOGO</a> 

     <!--[if lte IE 6]> 
     <a href="../menu/index.html">CATALOGO 
     <table><tr><td> 
     <![endif]--> 

      <ul> 
      <li><a href="udi_categoria.php?listar=cat_nivel&par=1&op=insert&nivel=1" title="Categorias de Produtos">CATEG. E PROD.</a></li> 
      <li><a href="prod_sem_cat.php" title="PRODUTOS SEM CATEGORIA">PROD. S/ CAT.</a></li> 
      <li><a href="udi_caracteristica.php?op=insert" title="Removing active/focus borders">CARACTERISTICA</a></li> 

      <li><a class="hide" href="../menu/hover_click.html" title="Hover/click with no active/focus borders">HOVER/CLICK &gt;</a> 

      <!--[if lte IE 6]> 
      <a class="sub" href="../menu/hover_click.html" title="Hover/click with no active/focus borders">HOVER/CLICK &gt; 
      <table><tr><td> 
      <![endif]--> 

       <ul> 
        <li><a href="../menu/form.html" title="Styling forms">styled form</a></li> 
        <li><a href="../menu/nodots.html" title="Removing active/focus borders">active focus</a></li> 
        <li><a href="../menu/hover_click.html" title="Hover/click with no active/focus borders">hover/click</a></li> 
       </ul> 

      <!--[if lte IE 6]> 
      </td></tr></table> 
      </a> 
      <![endif]--> 

      </li> 

      <li><a href="udi_menu.php" title="Cadastro de Menu">MENU</a></li> 
      <li><a href="udi_usuario.php?op=insert" title="Image Map for detailed information">USUARIOS</a></li> 
      <li><a href="../menu/bodies.html" title="fun with background images">fun backgrounds</a></li> 
      <li><a href="../menu/fade_scroll.html" title="fade-out scrolling">fade scrolling</a></li> 
      <li><a href="../menu/em_images.html" title="em size images compared">em sized images</a></li> 
      </ul> 

     <!--[if lte IE 6]> 
     </td></tr></table> 
     </a> 
     <![endif]--> 

     </li> 

     <li><a class="hide" style="font-weight:bold;" href="index.html">MENUS</a> 

     <!--[if lte IE 6]> 
     <a href="index.html">MENUS 
     <table><tr><td> 
     <![endif]--> 

      <ul> 
      <li><a href="spies.html" title="a coded list of spies">spies menu</a></li> 
      <li><a href="vertical.html" title="a horizontal vertical menu">vertical menu</a></li> 
      <li><a href="expand.html" title="an enlarging unordered list">enlarging list</a></li> 
      <li><a href="enlarge.html" title="an unordered list with link images">link images</a></li> 
      <li><a href="cross.html" title="non-rectangular links">non-rectangular</a></li> 
      <li><a href="jigsaw.html" title="jigsaw links">jigsaw links</a></li> 
      <li><a href="circles.html" title="circular links">circular links</a></li> 
      </ul> 

     <!--[if lte IE 6]> 
     </td></tr></table> 
     </a> 
     <![endif]--> 

     </li> 

     <li><a style="font-weight:bold;" class="hide" href="../layouts/index.html">LAYOUTS</a> 

     <!--[if lte IE 6]> 
     <a href="../layouts/index.html">LAYOUTS 
     <table><tr><td> 
     <![endif]--> 

      <ul> 
      <li><a href="../layouts/bodyfix.html" title="Cross browser fixed layout">Fixed 1</a></li> 
      <li><a href="../layouts/body2.html" title="Cross browser fixed layout">Fixed 2</a></li> 
      <li><a href="../layouts/body4.html" title="Cross browser fixed layout">Fixed 3</a></li> 
      <li><a href="../layouts/body5.html" title="Cross browser fixed layout">Fixed 4</a></li> 
      <li><a href="../layouts/minimum.html" title="A simple minimum width layout">minimum width</a></li> 
      </ul> 

     <!--[if lte IE 6]> 
     </td></tr></table> 
     </a> 
     <![endif]--> 

     </li> 

     <li><a style="font-weight:bold;" class="hide" href="../boxes/index.html">BOXES</a> 

     <!--[if lte IE 6]> 
     <a href="../boxes/index.html">BOXES 
     <table><tr><td> 
     <![endif]--> 

      <ul> 
      <li><a href="spies.html" title="a coded list of spies">spies menu</a></li> 
      <li><a href="vertical.html" title="a horizontal vertical menu">vertical menu</a></li> 
      <li><a href="expand.html" title="an enlarging unordered list">enlarging list</a></li> 
      <li><a href="enlarge.html" title="an unordered list with link images">link images</a></li> 
      <li><a href="cross.html" title="non-rectangular links">non-rectangular</a></li> 
      <li><a href="jigsaw.html" title="jigsaw links">jigsaw links</a></li> 
      <li><a href="circles.html" title="circular links">circular links</a></li> 
      </ul> 

     <!--[if lte IE 6]> 
     </td></tr></table> 
     </a> 
     <![endif]--> 

     </li> 

     <li><a style="font-weight:bold;" class="hide" href="../mozilla/index.html">MOZILLA</a> 

     <!--[if lte IE 6]> 
     <a href="../mozilla/index.html">MOZILLA 
     <table><tr><td> 
     <![endif]--> 

      <ul> 
      <li><a href="../mozilla/dropdown.html" title="A drop down menu">drop down menu</a></li> 
      <li><a href="../mozilla/cascade.html" title="A cascading menu">cascading menu</a></li> 
      <li><a href="../mozilla/content.html" title="Using content:">content:</a></li> 
      <li><a href="../mozilla/moxbox.html" title=":hover applied to a div">mozzie box</a></li> 
      <li><a href="../mozilla/rainbow.html" title="I can build a rainbow">rainbow box</a></li> 
      <li><a href="../mozilla/snooker.html" title="Snooker cue">snooker cue</a></li> 
      <li><a href="../mozilla/target.html" title="Target Practise">target practise</a></li> 
      <li><a href="../mozilla/splittext.html" title="Two tone headings">two tone headings</a></li> 
      <li><a href="../mozilla/shadow_text.html" title="Shadow text">shadow text</a></li> 
      </ul> 

     <!--[if lte IE 6]> 
     </td></tr></table> 
     </a> 
     <![endif]--> 

     </li> 

     <li><a style="font-weight:bold;" class="hide" href="../ie/index.html">EXPLORER</a> 

     <!--[if lte IE 6]> 
     <a href="../ie/index.html">EXPLORER 
     <table><tr><td> 
     <![endif]--> 

      <ul> 
      <li><a href="../ie/exampleone.html" title="Example one">example one</a></li> 
      <li><a href="../ie/weft.html" title="Weft fonts">weft fonts</a></li> 
      <li><a href="../ie/exampletwo.html" title="Vertical align">vertical align</a></li> 
      </ul> 

     <!--[if lte IE 6]> 
     </td></tr></table> 
     </a> 
     <![endif]--> 

     </li> 

     <li><a style="font-weight:bold;" class="hide" href="../opacity/index.html">OPACITY</a> 

     <!--[if lte IE 6]> 
     <a href="../opacity/index.html">OPACITY 
     <table><tr><td> 
     <![endif]--> 

      <ul> 
      <li><a href="../opacity/colours.html" title="colour wheel">opaque colours</a></li> 
      <li><a href="../opacity/picturemenu.html" title="a menu using opacity">opaque menu</a></li> 
      <li><a href="../opacity/png.html" title="partial opacity">partial opacity</a></li> 
      <li><a href="../opacity/png2.html" title="partial opacity II">partial opacity II</a></li> 
      <li><a class="hide" href="../menu/hover_click.html" title="Hover/click with no active/focus borders">&lt; HOVER/CLICK</a> 

      <!--[if lte IE 6]> 
      <a class="sub" href="../menu/hover_click.html" title="Hover/click with no active/focus borders">&lt; HOVER/CLICK 
      <table><tr><td> 
      <![endif]--> 

       <ul class="left"> 
        <li><a href="../menu/form.html" title="Styling forms">styled form</a></li> 
        <li><a href="../menu/nodots.html" title="Removing active/focus borders">active focus</a></li> 
        <li><a href="../menu/hover_click.html" title="Hover/click with no active/focus borders">hover/click</a></li> 
       </ul> 

      <!--[if lte IE 6]> 
      </td></tr></table> 
      </a> 
      <![endif]--> 

      </li> 
      </ul> 

     <!--[if lte IE 6]> 
     </td></tr></table> 
     </a> 
     <![endif]--> 

     </li> 
     </ul> 

     </div> 


    </div> 

菜單必須是動態的,每個用戶都有委託頁對我來說,我在數據庫中進行這些表來存儲菜單:

enter image description here

enter image description here

,所以我做這個選擇:

$p = new DB(); 
$p->setSQLcommand("SELECT menu_descr,sup_cod,level,menu_link,menu_title 
        FROM acesso 
        WHERE access_id in (SELECT access_id as id 
          FROM cpd_colaborador_acesso 
          WHERE col_cod = ".$_SESSION["colaborador"].")");      
$t = $p->ExecSelect(); 

在這裏,我告訴你一個的var_dump的結果

array 
0 => 
    array 
    'menu_descr' => string 'CATALOGO' (length=8) 
    0 => string 'CATALOGO' (length=8) 
    'sup_cod' => null 
    1 => null 
    'level' => string '1' (length=1) 
    2 => string '1' (length=1) 
    'menu_link' => string '#' (length=1) 
    3 => string '#' (length=1) 
    'menu_title' => string 'CATALOGO' (length=8) 
    4 => string 'CATALOGO' (length=8) 
1 => 
    array 
    'menu_descr' => string 'CATEG. E PROD.' (length=14) 
    0 => string 'CATEG. E PROD.' (length=14) 
    'sup_cod' => string '15' (length=2) 
    1 => string '15' (length=2) 
    'level' => string '2' (length=1) 
    2 => string '2' (length=1) 
    'menu_link' => string 'http://localhost/compararprecosde/admin 
    /udi_categoria.php?listar=cat_nivel&par=1&op=insert&nivel=1' (length=98) 
    3 => string 'http://localhost/compararprecosde/admin 
    /udi_categoria.php?listar=cat_nivel&par=1&op=insert&nivel=1' (length=98) 
    'menu_title' => string 'Categoria e Produtos' (length=20) 
    4 => string 'Categoria e Produtos' (length=20) 
2 => 
    array 
    'menu_descr' => string 'PROD. S/ CAT.' (length=13) 
    0 => string 'PROD. S/ CAT.' (length=13) 
    'sup_cod' => string '15' (length=2) 
    1 => string '15' (length=2) 
    'level' => string '2' (length=1) 
    2 => string '2' (length=1) 
    'menu_link' => string 'http://localhost/compararprecosde/admin/prod_sem_cat.php' 
    (length=56) 
    3 => string 'http://localhost/compararprecosde/admin/prod_sem_cat.php' (length=56) 
    'menu_title' => string 'Produtos sem Categoria' (length=22) 
    4 => string 'Produtos sem Categoria' (length=22) 

現在是我想與UL和李成果的陣列集成在foreach()PHP的問題,考慮到它在標題首先,這是1級,和它的子類,我不知道如何能夠在邏輯編程表示...

謝謝大家..

+0

不知道爲什麼這個提問者得到了一個downvote ...似乎是一個明確和有效的問題。 – CashIsClay

回答

0

這可能需要一些TW因爲我目前沒有可用的PHP服務器。我希望這至少接近你所需要的。

<?php 
$lastLevel = 0; 

foreach($t as $entry) 
{ 
    if($entry['level'] > $lastLevel) 
    { 
     $lastLevel = $entry['level']; 
     echo "<ul>"; 
    } 
    else if($entry['level'] < $lastLevel) 
    { 
     $lastLevel = $entry['level']; 
     echo "</li></ul>"; 
    } 
    else 
    { 
     echo "</li>"; 
    } 

    echo "<li><a href='" . $entry['menu_link'] . "'>" . $entry['menu_title'] . "</a>"; 
} 

for($x = $lastLevel; $x > 0; $x--) 
{ 
    echo "</li></ul>"; 
} 
?> 
+1

請嘗試http://codepad.org –