将动态PHP / SQL动态子菜单与CSS集成

时间:2019-02-28 03:18:25

标签: php html css css3 php-mysqlidb

表格的实际图片:“ PHP MySQL Table Image

这是我的“ menu.php”内容,其中,我将“父母和子类别”循环在一起。

<?php
	
function loop_array($array = array(), $parentID = 0){

		if(!empty($array[$parentID])){
			echo '<ul>';
			foreach($array[$parentID] as $items){
			echo '<li>';
			echo $items['categoryName'];
			loop_array($array, $items['categoryID']);
			echo '</li>';
			}
			echo '</ul>';
		}
	}

function display_menus()
	{
		$con = mysqli_connect("localhost", "root", "", "submenu");
		$query = $con->query("SELECT * FROM category");
		$array = array();

		if (mysqli_num_rows($query)) {
			while($rows = mysqli_fetch_array($query)){
				$array[$rows['parentID']][] = $rows;
			}
			loop_array($array);
		}
	}
?>

而且,这是我的“ index.php”,在这里我调用“ display_menus()”函数来显示此 SQL TABLE.

的所有类别和子类别

 <?php require 'menu.php'; ?>
<!DOCTYPE html>
<html>
  <head>
    <title>Sub Menu Test</title>
  </head>
  <body>
    <?php
      display_menus();
    ?> 
  </body>
</html> 

连接这两个PHP页面之后。我得到的结果是准确的结果:" Image result in Web Browser "

最后,我的问题是。如何将其与CSS集成?这看起来像带有子菜单的导航菜单。我在YouTube中尝试了其他教程,但失败了。是否有CSS菜单生成器或其他与此代码兼容的源?非常感谢!

2 个答案:

答案 0 :(得分:0)

要将CSS与HTML(任何元素)集成,您需要在HTML文件中添加CSS文件。

需要向您的元素添加类,id选择器。

并向这些类id添加定义。

例如(内联样式表):

<style>
.left { /* Class for left aligned elements*/
 float: left; /* This is attribute to be modified*/
}
#page-header { /* For element with id page-header*/
 font-weight: bold; /* This is attribute to be modified*/
}
</style>

in HTML,

<div id="page-header">Welcome to StackOverflow</div>
<div class="left">Tips to ask Questions on SO</div>
<ul class="left">
<li>Search on Google for your problem</li>
<li>Don't put why this code doesn't work on Question</li>
</ul>

另外,回到您的问题,对于菜​​单,我们可以使用suprefish库。

答案 1 :(得分:0)

这是我的CSS内容,在我的index.php中以

的形式调用它
<div id="cssmenu">
    <?php
     display_menus();
    ?>

并将我的menu.php函数loop_aray更改为:

function loop_array($array = array(), $parentID = 0){

        if(!empty($array[$parentID])){
            echo '<ul class="cssmenu">';
            foreach($array[$parentID] as $items){
            echo '<li class="has-sub">';
            echo $items['categoryName'];
            loop_array($array, $items['categoryID']);
            echo '</li>';
            }
            echo '</ul>';
        }
    }

这就是我的GETTING,也是我想要的OUTPUT

@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,600,300);
#cssmenu {
  font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, Sans-Serif;
  font-size: 10px;
  line-height: 15px;
  text-transform: uppercase;
  text-align: left;
}
#cssmenu > ul {
  width: auto;
  list-style-type: none;
  padding: 0;
  margin: 0;
  background: #ffffff;
  border: 1px solid #ece6e8;
  border-bottom: 3px solid #d9ced2;
  -webkit-border-radius: 2px;
  -moz-border-radius: 2px;
  -o-border-radius: 2px;
  border-radius: 2px;
}
#cssmenu > ul li#responsive-tab {
  display: none;
  /* Hide for large screens */

}
#cssmenu > ul li {
  display: inline-block;
  *display: inline;
  zoom: 1;
}
#cssmenu > ul li.right {
  float: right;
}
#cssmenu > ul li.has-sub {
  position: relative;
}
#cssmenu > ul li.has-sub:hover ul {
  display: block;
}
#cssmenu > ul li.has-sub ul {
  display: none;
  width: 250px;
  position: absolute;
  margin: 0;
  padding: 0;
  list-style-type: none;
  background: #ffffff;
  border: 1px solid #ece6e8;
  border-bottom: 3px solid #d9ced2;
  border-top: 0 none;
}
#cssmenu > ul li.has-sub ul li {
  display: block;
}
#cssmenu > ul li.has-sub > a {
  background-image: url('images/caret.png');
  background-repeat: no-repeat;
  background-position: 90% -95%;
}
#cssmenu > ul li.has-sub > a.active,
#cssmenu > ul li.has-sub > a:hover {
  background: #d80041 url('images/caret.png') no-repeat;
  background-position: 90% 195%;
}
#cssmenu > ul li a {
  display: block;
  padding: 12px 24px 11px 24px;
  text-decoration: none;
  color: #747474;
  text-shadow: 0px 1px 0px #fff;
}
#cssmenu > ul li a.active,
#cssmenu > ul li a:hover {
  background: #d80041;
  color: #fff;
  text-shadow: 0px 1px 0px #000;
}
@media (max-width: 600px) {
  #cssmenu > ul {
    width: 100%;
  }
  #cssmenu > ul li#responsive-tab {
    display: block;
  }
  #cssmenu > ul li#responsive-tab a {
    background: url('images/menu.png') no-repeat;
    background-position: 95% -35%;
  }
  #cssmenu > ul li#responsive-tab a:hover {
    background-color: #d80041;
    background-position: 95% 135%;
  }
  #cssmenu > ul li {
    display: none;
  }
  #cssmenu > ul li.right {
    float: none;
  }
  #cssmenu > ul li.has-sub {
    position: relative;
  }
  #cssmenu > ul li.has-sub ul {
    display: block;
    position: static;
    width: 100%;
    background: #ffffff;
    border: 0 none;
  }
  #cssmenu > ul li.has-sub ul li {
    display: block !important;
  }
  #cssmenu > ul li.has-sub ul li a span {
    display: block;
    padding-left: 24px;
  }
  #cssmenu > ul li.has-sub > a {
    background-image: none;
  }
}
/* Make sure they show even if hidden in mobile view by JS */
@media (min-width: 600px) {
  #cssmenu > ul > li.collapsed {
    display: inline-block !important;
    *display: inline;
    zoom: 1;
  }
  #cssmenu > ul ul li.collapsed {
    display: block !important;
  }
}