动态下拉菜单和子菜单php mysql

时间:2014-10-09 11:59:16

标签: php mysql dynamic menu submenu

我有一个需要从数据库动态创建的菜单。 需要有菜单和子菜单

例如(我想要的):

<ul class="dropdown">
    <li><a href="#">Link 1</a> </li>
    <li><a href="#">Link 2</a> </li>
    <li><a href="#">Link 3</a> </li>
    <li><a href="#">Link 4</a>
    <ul class="sub_menu">
        <li><a href="#">Link 4 - 1</a></li>
        <li><a href="#">Link 4 - 2</a></li>
        <li><a href="#">Link 4 - 3</a></li>
    </ul>
    </li>
    <li><a href="#">Link 5</a></li>
    <li><a href="#">Link 6</a> </li>
</ul>

下面是我用于菜单使用功能的代码我想要子菜单

function listMenu(){
    $ans = $this->select("cat_id,cat_name","s_category","1");
    if(is_array($ans)){
        foreach($ans as $val){
            echo 
            "<li><a href=\"post-summary.php?cid=$val[0]\" >$val[1]</a></li>";
        }
    }
    else{
        echo "";
    }
}

数据库如下: -

CREATE TABLE IF NOT EXISTS `s_category` (
  `cat_id` int(11) NOT NULL AUTO_INCREMENT,
  `cat_name` varchar(15) NOT NULL,
  `cat_uid` int(2) NOT NULL,
  `cat_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `cat_parent` int(11) DEFAULT '0',
  `cat_sort` int(11) DEFAULT NULL,
  `cat_delete` int(1) DEFAULT '0',
  PRIMARY KEY (`cat_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;

3 个答案:

答案 0 :(得分:0)

就菜单而言,将子菜单存储为数据库中的逗号分隔值会很方便。

考虑一个带有id,菜单,子菜单的表(用逗号分隔的几个子菜单)。

 function listMenu(){
$ans = $this->select("menus,submenus");
if(is_array($ans)){
    foreach($ans as $val){
        echo    "<li><a href=\"post-summary.php?cid=$val[0]\" >$val[0]</a>"; //menus

        $submenu = explode(",",$val[1]);

        foreach($submenu as $values){
        echo    "<li><a href=\"post-summary.php?cid=$val[0]&proname=$values\" >$values</a></li>"; // submenus
        }
        echo "</li>";
    }
 }
}

试试这个

$con    =   db_connect();
$str    =   "select menus,submenus from your_tabel  ";
$result =   mysql_query($str,$con);

while($arr = mysql_fetch_array($result))
{
    echo    "<li><a href=\"post-summary.php?cid=$arr[0]\" >$arr [0]</a>"; //menus

        $submenu = explode(",",$arr [1]);

//make sure you have several sub-menus else use a if condition here to avoid foreach error  
        foreach($submenu as $values){
        echo    "<li><a href=\"post-summary.php?cid=$arr[0]&proname=$values\" >$values</a></li>"; // submenus
        }
        echo "</li>";
    }

这对我刚刚测试的效果很好

评论是否有效。 评论如果不工作。

答案 1 :(得分:0)

您需要有一个列,我们在parentId表中将其命名为s_category

那里的示例值:

catId,catName,parentId
1,Vehicles,null
2,Planes,null
3,Cars,1
4,Bicycles,1

所以你的结构就像:

- Vehicles
   - Cars
   - Bicycles
- Planes

您需要获取此类数据(被视为伪代码):

$topLevelItems = (select catId, catName from s_category where parentId is null)
foreach($topLevelItems as $item) {
   echo '<a href="...">$item["catName"]</a>';
   $subItems = (select catId, catName from s_category where parentId=$item['catId'])
   foreach($subItems as $subItem) {
      echo '<a href="...">$subItem["catName"]</a>';
   }
}

您需要重新编写SQL部分以匹配您的案例。如果你只有一个内在级别,例如没有子子菜单。如果您打算使用子子菜单,则需要递归功能。

答案 2 :(得分:0)

使用代码创建数据库并编写函数并更改参数

  function getAllFrom($field, $table,$where = NULL, $and = NULL , $orderfield, $ordering = "DESC") {

global $con;

$getAll = $con->prepare("SELECT $field FROM $table where $where $and  ORDER BY $orderfield $ordering");

$getAll->execute();

$all = $getAll->fetchAll();

return $all;}


         <ul>
                <?php
                $allCats = getAllFrom("*", "categories", "parent = 0", "" , "cat_id", "ASC");
                     foreach ($allCats as $cat) { ?>
                      <li>
                           <?php
                     echo '<a href="categoreis.php?pagename=' . str_replace(' ', '-', $cat['name_cat']) . '&pageid=' . $cat["cat_id"] . '">

                          ' . $cat['name_cat'] . '

                          </a>';
                          ?>
                          <ul class="sub-menu[enter image description here][1]">
                                <?php
                                  $catsub = $cat['cat_id'];
                                  $SubCats = getAllFrom("*", "categories", "parent = {$catsub}", "" , "cat_id", "ASC");
                                  foreach ($SubCats as $sub) { ?>
                                    <li>
                                    <?php
                                         echo '<a href="categoreis.php?pagename=' . $sub['name_cat'] . '&pageid=' . $sub["cat_id"] . '">

                                              ' . $sub['name_cat'] . '

                                              </a>';
                                              ?>
                                       </li>
                                <?php  }
                                  ?>
                          </ul>
                      </li>
                <?php   }
                 ?>
           </ul>