我在php中创建动态菜单和子菜单....无法知道从哪里开始....我已经编写了所有查询但需要建议使用Ul
和{{迭代结果集1}}带有它的元素....可能没有子菜单的子菜单,菜单可能有也可能没有子菜单......
li
任何建议....
修改
这就是我在做什么,
Menu1
submenu1
submenu11
submenu12
submenu2
submenu21
Menu2
submenu1
submenu11
submenu12
这是我的表,
alt text http://img229.imageshack.us/img229/1174/84296639.jpg
<?php
$con=mysql_connect("localhost","root","");
$db=mysql_select_db("testt");
echo '<ul id="nav">';
$res=mysql_query("SELECT * FROM menu WHERE PARENT_ID=0");
while($row=mysql_fetch_array($res))
{
echo '<li class="top"><a href="#" class="top_link"><span>'.$row['MENU_NAME'].'</span></a></li>';
$res1=mysql_query("SELECT * FROM menu WHERE PARENT_ID=1 AND CHILD_ID=".$row['ID']."");
echo '<ul class="sub">';
while($row1=mysql_fetch_array($res1))
{
echo '<li><a href="#" class="fly">'.$row1['MENU_NAME'].'</li>';
$res2=mysql_query("SELECT * FROM menu WHERE PARENT_ID=2 AND CHILD_ID=".$row1['ID']."");
echo '<ul>';
while($row2=mysql_fetch_array($res2))
{
echo '<li><a href="#">'.$row2['MENU_NAME'].'</li>';
}
echo '</ul>';
}
echo '</ul>';
}
echo '</ul>';
?>
答案 0 :(得分:1)
我是这样做的:
css文件:
ul.makeMenu, ul.makeMenu ul {
background-color:#000000;
cursor:default;
margin:0px 0px 0px 0px;
padding:0px 0px 0px 0px;
width: 180px;
}
ul.makeMenu li {
background-image:url(../images/marker_rosu.gif);
background-position:5px center;
background-repeat:no-repeat;
color:#D4020B;/*#FEBF01;*/
list-style-type:none;
position:relative;
margin:0px 0px 0px 0px;
padding:0px 5px 0px 15px;
line-height:24px;
text-align:left;
}
ul.makeMenu li>ul {
display:none;
position:absolute;
top:0px;
left:180px;
}
ul.makeMenu li:hover {
background-image:url(../images/marker_negru.gif);
background-position:5px center;
background-repeat:no-repeat;
background-color:#D4020B;/*#FEBF01;*/
color:#FFFFFF;
}
ul.makeMenu {
display:block;
}
ul.makeMenu li:hover>ul {
display:block;
}
ul.makeMenu li a {
color:#D4020B;/*#FEBF01;*/
display:block;
width:100%;
text-decoration:none;
}
ul.makeMenu li a:hover {
color:#000000;
}
ul.makeMenu li:hover>a {
color:#000000;
}
产品的sql文件或其他任何类别表:
CREATE TABLE `category` (
`id` int(10) unsigned NOT NULL auto_increment,
`idParrent` int(10) unsigned NOT NULL,
`name` varchar(255) NOT NULL,
`description` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
和创建菜单的功能:
function getAFP ($id)
{
global $conx;
$sql = sprintf("SELECT * FROM category WHERE idParrent = %d;", quote_smart($id));
$rs = mysql_query($sql, $conx);
$arrayResult = array();
if (mysql_num_rows($rs)) {
while ($row = mysql_fetch_array($rs)) {
array_push($arrayResult, $row);
}
return $arrayResult;
} else {
return 0;
}
}
function makeListaCategory ($id)
{
$listaCategorii = array();
$categorii = getAFP($id);
if (! $categorii) {
return 0;
} else {
foreach ($categorii as $categorie) {
array_push($listaCategorii, $categorie[2] . "|" . $categorie[0]);
$subcategorii = makeListaCategory($categorie[0]);
if ($subcategorii) {
foreach ($subcategorii as $subcategorie) {
$valori = explode("|", $subcategorie);
array_push($listaCategorii, $categorie[2] . ">" . $valori[0] . "|" . $valori[1]);
}
}
}
}
return $listaCategorii;
}
我希望这会对你有所帮助!
答案 1 :(得分:0)
编辑:在您澄清问题之前发布。这是一个通用的递归菜单结构。
<?php
function print_menu($menu)
{
echo "<ul>\n";
foreach ($menu as $submenu)
{
echo "<li>\n";
echo "<span>{$submenu['name']}</span>\n";
if (!empty($submenu['children']))
print_menu($submenu['children']);
echo "</li>\n";
}
echo "</ul>\n";
}
print_menu(
array(
array(
'name' => 'Menu 1',
'children' => array(
array(
'name' => 'Submenu 11',
'children' => array(
array('name' => 'Submenu 111'),
array('name' => 'Submenu 112')
)
),
)
),
array(
'name' => 'Menu 2'
)
)
);
?>