2016-03-31 5 views
0

データベースに作成された動的メニューを作成するにはどうすればよいですか?PHP:SQL Serverデータベースからメニューを作成

メニュー表のサンプル:

ID NAME  URL   IDPARENT 
---------------------------------- 
1 Accueil  #Accueil 0   
2 Parcs  #Parcs  0   
3 Allemagne #Allemagne 2   
4 Berlin  #Berlin  3   
5 France  #France  2   
6 Contact  #Contact 0  

結果は次のようになります。

<ul> 
<li>Accueil</li> 
<li>Parcs</li> 
    <ul> 
    <li>Allemagne</li> 
     <ul> 
      <li>Berlin</li> 
     </ul> 
    <li>France</li> 
    </ul> 
<li>Contact</li> 
</ul> 

を解決しよう:私のコード:

 <?php 
     //connection to the database 
     $dbhandle = mssql_connect('*****', '*****', '*****') 
      or die("Couldn't connect to Server");  
     //select a database to work with 
     $selected = mssql_select_db("*****", $dbhandle) 
      or die("Couldn't open database"); 
      //declare the SQL statement that will query the database 
     $query = "SELECT * FROM CATEGORIES "; 
     //execute the SQL query and return records 
     $result = mssql_query($query); 
     //display the results 
     while($row = mssql_fetch_array($result)) 
       { 
       // Assign by reference 
       $thisref = &$refs[ $row['ID'] ]; 
       // add the the menu parent 
       $thisref['IDCategoriePere'] = $row['IDCategoriePere']; 
       $thisref['NOM'] = $row['NOM']; 
       $thisref['URL'] = $row['URL']; 
       // if there is no parent id 
       if ($row['IDCategoriePere'] == 0) 
       { 
        $list[ $row['ID'] ] = &$thisref; 
       } 
       else 
       { 
        $refs[ $row['IDCategoriePere'] ]['children'][ $row['ID'] ] = &$thisref; 
       } 
     } 
     function create_menu($arr) 
      { 
       $html = "\n<ul>\n"; 
       foreach ($arr as $key=>$val) 
       { 
        $html .= '<li><a href="'.$val['URL'].'">'.$val['NOM']."</a></li>\n"; 
        if (array_key_exists('children', $val)) 
        { 
         $html .= create_menu($val['children']); 
        } 
       } 
       $html .= "</ul>\n"; 
       return $html; 
      } 
      echo create_menu($list); 
      //close the connection 
     mssql_close($dbhandle); 
     ?> 

は、それが正常に動作します!私は、CSS(http://cssmenumaker.com/menu/flat-jquery-responsive-menu)を入れてみましたときには、ドロップダウンは、CSSとし、witout :(

結果は表示されません:

<style> 
/* CSS Document */ 
@import url(http://fonts.googleapis.com/css?family=Open+Sans); 
@import url(http://fonts.googleapis.com/css?family=Bree+Serif); 
#container { 
margin: 0 auto; 
} 
nav { 
margin: 50px 0; 
background-color: #E64A19; 
} 
nav ul { 
padding: 0; 
margin: 0; 
list-style: none; 
position: relative; 
} 
nav ul li { 
display:inline-block; 
background-color: #E64A19; 
} 
nav a { 
display:block; 
padding:0 10px; 
color:#FFF; 
font-size:20px; 
line-height: 60px; 
text-decoration:none; 
} 
nav a:hover { 
background-color: #000000; 
} 
/* Hide Dropdowns by Default */ 
nav ul ul { 
display: none; 
position: absolute; 
top: 60px; /* the height of the main nav */ 
} 
/* Display Dropdowns on Hover */ 
nav ul li:hover > ul { 
display:inherit; 
} 
/* Fisrt Tier Dropdown */ 
nav ul ul li { 
width:170px; 
float:none; 
display:list-item; 
position: relative; 
} 
/* Second, Third and more Tiers */ 
nav ul ul ul li { 
position: relative; 
top:-60px; 
left:170px; 
} 
/* Change this in order to change the Dropdown symbol */ 
li > a:after { content: ' +'; } 
li > a:only-child:after { content: ''; } 
</style> 

<!-- WITH PHP --> 
<div id="container"> 
<nav> 
<?php 
     //connection to the database 
     $dbhandle = mssql_connect('*****', '*****', '*****') 
      or die("Couldn't connect to Server");  
     //select a database to work with 
     $selected = mssql_select_db("*****", $dbhandle) 
      or die("Couldn't open database"); 
      //declare the SQL statement that will query the database 
     $query = "SELECT * FROM CATEGORIES "; 
     //execute the SQL query and return records 
     $result = mssql_query($query); 
     //display the results 
     while($row = mssql_fetch_array($result)) 
       { 
       // Assign by reference 
       $thisref = &$refs[ $row['ID'] ]; 
       // add the the menu parent 
       $thisref['IDCategoriePere'] = $row['IDCategoriePere']; 
       $thisref['NOM'] = $row['NOM']; 
       $thisref['URL'] = $row['URL']; 
       // if there is no parent id 
       if ($row['IDCategoriePere'] == 0) 
       { 
        $list[ $row['ID'] ] = &$thisref; 
       } 
       else 
       { 
        $refs[ $row['IDCategoriePere'] ]['children'][ $row['ID'] ] = &$thisref; 
       } 
     } 
     function create_menu($arr) 
      { 
       $html = "\n<ul>\n"; 
       foreach ($arr as $key=>$val) 
       { 
        $html .= '<li><a href="'.$val['URL'].'">'.$val['NOM']."</a></li>\n"; 
        if (array_key_exists('children', $val)) 
        { 
         $html .= create_menu($val['children']); 
        } 
       } 
       $html .= "</ul>\n"; 
       return $html; 
      } 
      echo create_menu($list); 
      //close the connection 
     mssql_close($dbhandle); 
     ?> 
     </nav> 
</div> 

<!-- WITHOUT PHP --> 
<div id="container"> 
<nav> 
    <ul> 
     <li><a href="#">ACCUEIL</a></li> 

     <li><a href="#">PARCS</a> 
     <!-- First Tier Drop Down --> 
     <ul> 
      <li><a href="#">ALLEMAGNE</a> 
      <!-- Second Tier Drop Down --> 
      <ul> 
       <li><a href="#">BERLIN</a></li> 
      </ul> 
      </li> 
      <li><a href="#">FRANCE</a> 
      </li> 
     </ul> 
     </li> 
     <li><a href="#">CONTACT</a></li> 
    </ul> 
</nav> 
</div> 

enter image description here

+0

質問を更新し、メニューを作成するために使用している言語でタグ付けしてください。 –

+0

done、申し訳ありません^^ ' – Senneville

+0

まず、あなたのテーブルを照会するときに、階層を作成し、メニューを作成するための配列を適切に抽出するための除外をするのが大変です。 – cpugourou

答えて

1

を詳細な回答のためのようなすべての情報を教えてくださいあなたのデータベースのタイプ、さらにはあなたのコードのクエリやすべての名前。列名NAMEは予約済みのSQLキーワードですので、通常は良い考えではありませんが、mssqlはほとんどANSIではないので、面白くないかもしれません:)

多次元メニューでは、テーブルを印刷するだけでは不十分です。最初にデータを注文し(すべての子を親に渡す)、次にメニューを作成することができます。そのためには、ここではcreate_menu関数のような再帰関数やメソッドで通常使用しています。

<?php 

    $serverName = "serverName\instanceName"; 
    $connectionInfo = array("Database"=>"dbName", "UID"=>"username", "PWD"=>"password"); 
    // connect to sql server 
    $conn = sqlsrv_connect($serverName, $connectionInfo); 
    if($conn === false) { 
     die(print_r(sqlsrv_errors(), true)); 
    } 

    // create an array to hold the references 
    $refs = array(); 

    // create and array to hold the list 
    $list = array(); 

    $tsql = "SELECT ID, IDPARENT, NAME, URL FROM menu_items ORDER BY NAME;" 

    $stmt = sqlsrv_query($conn, $tsql); 
    if($stmt === false) { 
     die(print_r(sqlsrv_errors(), true)); 
    } 
    while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) 
    { 
     // Assign by reference 
     $thisref = &$refs[ $row['ID'] ]; 

     // add the the menu parent 
     $thisref['IDPARENT'] = $row['IDPARENT']; 
     $thisref['NAME'] = $row['NAME']; 
     $thisref['URL'] = $row['URL']; 

     // if there is no parent id 
     if ($row['IDPARENT'] == 0) 
     { 
      $list[ $row['ID'] ] = &$thisref; 
     } 
     else 
     { 
      $refs[ $row['IDPARENT'] ]['children'][ $row['ID'] ] = &$thisref; 
     } 
    } 
    mssql_close($conn); 

    /** 
    * 
    * Create a HTML menu from an array 
    * 
    * @param array $arr 
    * @param string $list_type 
    * @return string 
    * 
    */ 
    function create_menu($arr) 
    { 
     $html = "\n<ul>\n"; 
     foreach ($arr as $key=>$val) 
     { 
      $html .= '<li><a href="'.$val['URL'].'">'.$val['NAME']."</a>"; 
      if (array_key_exists('children', $val)) 
      { 
       $html .= create_menu($val['children']); 
      } 
      $html .= "</li>\n"; 
     } 
     $html .= "</ul>\n"; 
     return $html; 
    } 

    echo create_menu($list); 
    ?> 
+0

ありがとう!出来た ! (小さな最終的な問題.. cf質問) – Senneville

+0

聞いてよかった。それは通常は新しい質問であり、コードを見ずにメニューを呼び出す方法はなぜ機能していないのかは簡単ではありません。 – Pintus

+0

質問CSSで問題を表示するためにudpatedしてください。 – Senneville

関連する問題