你好今天我问非常愚蠢的问题,我只是做数据库相关的导航菜单并且它正常工作,但我的问题是当我要设置我的菜单或子菜单然后我做的很很复杂的想法,比如,发布菜单名称,发布菜单链接,发布子菜单位置,发布子菜单扩展等。 但在wordpress中,对任何人来说似乎都很容易,
这是我的导航菜单代码:
<?php
$dbcon = new MySQLi("localhost","root","","myweb");
?>
<div id='cssmenu'>
<ul>
<li><a href='#'><span>Home</span></a></li>
<?php
$res=$dbcon->query("SELECT * FROM main_menu");
while($row=$res->fetch_array())
{
?>
<li class='active has-sub'><a href='<?php echo $row['m_menu_link']; ?>'><span><?php echo $row['m_menu_name']; ?></span></a>
<?php
$res_pro=$dbcon->query("SELECT * FROM sub_menu WHERE m_menu_id=".$row['m_menu_id']);
?>
<ul>
<?php
while($pro_row=$res_pro->fetch_array())
{
?><li class='has-sub'><a href="<?php echo $pro_row['s_menu_link']; ?>"><?php echo $pro_row['s_menu_name']; ?></a>
<?php
$res_pror=$dbcon->query("SELECT * FROM sube_menu WHERE s_menu_id=".$pro_row['s_menu_id']);
?>
<ul>
<?php
while($pro_rowe=$res_pror->fetch_array())
{ ?>
<li><a href='<?php echo $pro_rowe['s_menu_link2']; ?>'><span><?php echo $pro_rowe['s_menu_name2']; ?></span></a></li>
<?php } ?>
</ul>
</li>
<?php
}
?>
</ul>
</li>
<?php
}
?>
</ul>
</div>
这是我的菜单设置代码,通过这些代码,我可以将菜单设置为数据库。
<?php
$dbcon = new MySQLi("localhost","root","","myweb");
if(isset($_POST['add_main_menu']))
{
$menu_name = $_POST['menu_name'];
$menu_link = $_POST['mn_link'];
$sql=$dbcon->query("INSERT INTO main_menu(m_menu_name,m_menu_link) VALUES('$menu_name','$menu_link')");
}
if(isset($_POST['add_sub_menu']))
{
$parent = $_POST['parent'];
$proname = $_POST['sub_menu_name'];
$menu_link = $_POST['sub_menu_link'];
$sql=$dbcon->query("INSERT INTO sub_menu(m_menu_id,s_menu_name,s_menu_link) VALUES('$parent','$proname','$menu_link')");
}
if(isset($_POST['add_sub_menu2']))
{
$parent2 = $_POST['parent2'];
$proname2 = $_POST['sub_menu_name2'];
$menu_link2 = $_POST['sub_menu_link2'];
$sql=$dbcon->query("INSERT INTO sube_menu(s_menu_id,s_menu_name2,s_menu_link2) VALUES('$parent2','$proname2','$menu_link2')");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Setup menus</title>
<link rel="stylesheet" type="text/css" href="style.css" media="all" />
</head>
<body>
<div id="head">
</div>
<center>
<pre>
<form method="post">
<input type="text" placeholder="menu name :" name="menu name" /><br />
<input type="text" placeholder="menu link :" name="mn_link" /><br />
<button type="submit" name="add_main_menu">Add main menu</button>
</form>
</pre>
<br />
<pre>
<form method="post">
<select name="parent">
<option selected="selected">select parent menu</option>
<?php
$res=$dbcon->query("SELECT * FROM main_menu");
while($row=$res->fetch_array())
{
?>
<option value="<?php echo $row['m_menu_id']; ?>"><?php echo $row['m_menu_name']; ?></option>
<?php
}
?>
</select><br />
<input type="text" placeholder="menu name :" name="sub_menu_name" /><br />
<input type="text" placeholder="menu link :" name="sub_menu_link" /><br />
<button type="submit" name="add_sub_menu">Add sub menu</button>
</form>
</pre>
<pre>
<form method="post">
<select name="parent2">
<option selected="selected">select parent menu</option>
<?php
$res=$dbcon->query("SELECT * FROM sub_menu");
while($row=$res->fetch_array())
{
?>
<option value="<?php echo $row['s_menu_id']; ?>"><?php echo $row['s_menu_name']; ?></option>
<?php
}
?>
</select><br />
<input type="text" placeholder="menu name :" name="sub_menu_name2" /><br />
<input type="text" placeholder="menu link :" name="sub_menu_link2" /><br />
<button type="submit" name="add_sub_menu2">Add sub menu2</button>
</form>
</pre>
<a href="index.php">back to main page</a>
</center>
</body>
</html>
但我的观点是我的代码可以像wordpress一样。谢谢。