如何创建在数据库中创建的动态菜单?
菜单表的一个示例:
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的结果:
<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>
答案 0 :(得分:1)
请详细解答所有信息,例如数据库类型以及查询或所有代码。列名NAME也是一个保留的SQL关键字,所以通常它不是一个好主意,但mssql主要不是ANSI,所以它可能不是很有趣:)
如果您想要一个多维菜单,那么您不仅可以打印出表格。您首先要订购数据(将所有子项传递给父项),然后您可以创建菜单。为此,您通常使用递归函数或类似create_menu函数的方法。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Kviz {
private JFrame okvir;
private JPanel p1,p2,p3;
private JLabel [] nizLabela;
private JRadioButton [] nizRButtona;
private ButtonGroup[] nizGrupa;
private JPanel [] nizPanela;
private CardLayout cl;
private JLabel lb1,lb2;
private JButton btn1,btn2;
private Timer tim;
private int t,s;
public Kviz(){
pripremiGUI();
}
private void pripremiGUI(){
okvir=new JFrame();
okvir.setSize(400, 400);
okvir.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okvir.setLocationRelativeTo(null);
okvir.setLayout(new BorderLayout());
p1=new JPanel();
p1.setLayout(new FlowLayout());
lb1=new JLabel("Dobrodošli u kviz");
cl=new CardLayout();
p2=new JPanel();
p2.setLayout(cl);
nizPanela = new JPanel[10];
//Niz panela na kome će se smenjivati pitanja i odgovori
for(int i=0;i<nizPanela.length;i++){
nizPanela[i] = new JPanel();
p2.add(nizPanela[i], String.valueOf(i));
nizPanela[i].setLayout(new GridLayout(0,1));
}
//Niz odgovora
nizRButtona = new JRadioButton[40];
for(int i=0;i<nizRButtona.length;i++){
nizRButtona[i] = new JRadioButton();
switch(i){
case 0:nizRButtona[i].setText("Mesopotamiji");
break;
case 1:nizRButtona[i].setText("Grčkoj");
break;
case 2:nizRButtona[i].setText("Kini");
break;
case 3:nizRButtona[i].setText("Egiptu");
break;
case 4:nizRButtona[i].setText("Kinšasa");
break;
case 5:nizRButtona[i].setText("Lagos");
break;
case 6:nizRButtona[i].setText("Ulan Bator");
break;
case 7:nizRButtona[i].setText("Abu Dabi");
break;
case 8:nizRButtona[i].setText("Velikoj Britaniji");
break;
case 9:nizRButtona[i].setText("Norveškoj");
break;
case 10:nizRButtona[i].setText("SAD");
break;
case 11:nizRButtona[i].setText("Danskoj");
break;
case 12:nizRButtona[i].setText("Step mom");
break;
case 13:nizRButtona[i].setText("Runaway Bride");
break;
case 14:nizRButtona[i].setText("Grease");
break;
case 15:nizRButtona[i].setText("Nothing Hill");
break;
case 16:nizRButtona[i].setText("Neil Armstrong");
break;
case 17:nizRButtona[i].setText("John Lenon");
break;
case 18:nizRButtona[i].setText("Martin Luter King");
break;
case 19:nizRButtona[i].setText("Winston Churchill");
break;
case 20:nizRButtona[i].setText("Meka");
break;
case 21:nizRButtona[i].setText("Medina");
break;
case 22:nizRButtona[i].setText("Dubai");
break;
case 23:nizRButtona[i].setText("Rijad");
break;
case 24:nizRButtona[i].setText("Španije");
break;
case 25:nizRButtona[i].setText("Venecije");
break;
case 26:nizRButtona[i].setText("Portugala");
break;
case 27:nizRButtona[i].setText("Đenove");
break;
case 28:nizRButtona[i].setText("Holokaust");
break;
case 29:nizRButtona[i].setText("Perl Harbour");
break;
case 30:nizRButtona[i].setText("Napad na Francusku");
break;
case 31:nizRButtona[i].setText("Blitzkrieg na Poljsku");
break;
case 32:nizRButtona[i].setText("Bon");
break;
case 33:nizRButtona[i].setText("Minhen");
break;
case 34:nizRButtona[i].setText("Lajpcig");
break;
case 35:nizRButtona[i].setText("Hamburg");
break;
case 36:nizRButtona[i].setText("Čile");
break;
case 37:nizRButtona[i].setText("Istočni Timor");
break;
case 38:nizRButtona[i].setText("Portugal");
break;
case 39:nizRButtona[i].setText("Brazil");
break;
}
}
//Niz button grupa i dodavanje komponenti u njega
nizGrupa=new ButtonGroup[10];
int j=0;
for(int i=0;i<nizGrupa.length;i++){
nizGrupa[i] = new ButtonGroup();
nizGrupa[i].add(nizRButtona[j]);
nizGrupa[i].add(nizRButtona[j+1]);
nizGrupa[i].add(nizRButtona[j+2]);
nizGrupa[i].add(nizRButtona[j+3]);
j+=4;
}
//Niz pitanja
nizLabela = new JLabel[10];
for(int i=0;i<nizLabela.length;i++){
nizLabela[i] = new JLabel();
switch(i){
case 0:nizLabela[i].setText("U kojoj zemlji su rođene Olimpijske igre");
break;
case 1:nizLabela[i].setText("Koji je glavni grad Nigerije");
break;
case 2:nizLabela[i].setText("Kojoj državi pripada Grenland");
break;
case 3:nizLabela[i].setText("Osim filma \"Pretty Women\" u kojem filmu zajedno glume Julia Roberts i Richard Gere");
break;
case 4:nizLabela[i].setText("Kako se zove prvi čovek koji je sleteo na mesec");
break;
case 5:nizLabela[i].setText("Glavni grad Saudijske Arabije");
break;
case 6:nizLabela[i].setText("Kristofer Kolumob je iz");
break;
case 7:nizLabela[i].setText("Koji događaj je naveo SAD da se uključe u rat");
break;
case 8:nizLabela[i].setText("Koji je grad bio glavni pri ujedinjenju Nemačke");
break;
case 9:nizLabela[i].setText("U kojoj zemlji je bio na vlasti diktator Salazar");
break;
}
}
p3=new JPanel();
p3.setLayout(new FlowLayout());
//Menja panele sa pitanjima i odgovorima
btn1=new JButton("Sledeće pitanje");
btn1.setEnabled(false);
btn1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
cl.next(p2);
btn1.setEnabled(false);
btn2.setEnabled(true);
t=10;
lb2.setText(String.valueOf(t));
tim.start();
btn1.setText("Sledeće pitanje");
}
});
//Proverava da li su odgovori tačni
btn2=new JButton("Odgovori");
btn2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
tim.stop();
btn1.setEnabled(true);
if(nizRButtona[1].isSelected()
||nizRButtona[5].isSelected()
||nizRButtona[11].isSelected()
||nizRButtona[13].isSelected()
||nizRButtona[16].isSelected()
||nizRButtona[23].isSelected()
||nizRButtona[27].isSelected()
||nizRButtona[29].isSelected()
||nizRButtona[32].isSelected()){
s+=10;
JOptionPane.showMessageDialog(null, "Correct");
} else if(nizRButtona[36].isSelected()){
s+=10;
JOptionPane.showMessageDialog(null, "Correct, points earned: " + s);
btn1.setText("Započni opet igru");
}else if(nizRButtona[33].isSelected()
||nizRButtona[34].isSelected()
||nizRButtona[35].isSelected()){
JOptionPane.showMessageDialog(null, "Incorrect, points earned " + s);
btn1.setText("Započni opet igru");
}else{
JOptionPane.showMessageDialog(null, "Incorrect");
}
for (int i = 0; i < 10; i++) {
nizGrupa[i].clearSelection();
}
}
});
t=10;
//Tajmer
lb2=new JLabel("10");
tim=new Timer(1000,new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
if(t>0){
t--;
lb2.setText(String.valueOf(t));
}else{
tim.stop();
JOptionPane.showMessageDialog(null, "Vreme je isteklo");
btn2.setEnabled(false);
btn1.setEnabled(true);
}
}
});
}
private void pokreniGUI(){
p1.add(lb1);
p3.add(btn1);
p3.add(btn2);
p3.add(lb2);
int q=0;
for(int w=0;w<10;w++){
//nizGrupa[w] = new ButtonGroup();
nizPanela[w].add(nizLabela[w]);
nizPanela[w].add(nizRButtona[q]);
nizPanela[w].add(nizRButtona[q+1]);
nizPanela[w].add(nizRButtona[q+2]);
nizPanela[w].add(nizRButtona[q+3]);
q+=4;
}
okvir.add(p1,BorderLayout.NORTH);
okvir.add(p2,BorderLayout.CENTER);
okvir.add(p3,BorderLayout.SOUTH);
cl.show(p2, "0");
tim.start();
okvir.setVisible(true);
}
public static void main(String[] args) {
Kviz pokreni=new Kviz();
pokreni.pokreniGUI();
}
}