du0531
du0531
2014-10-09 11:59
浏览 24
已采纳

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

I have a menu that needs to be created dynamically from the database. need to have menu and submenu

for example (wht i want):

<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>

below is the code i used for menu using function i want submenu as well

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 "";
    }
}

database is as follows:-

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条回答 默认 最新

  • dougua3705
    dougua3705 2014-10-09 12:16
    已采纳

    as far as menus are concerned storing the submenus as a comma seperated values in the database would come handy.

    ie consider a table with id,menus,submenus(several submenus seperated by a comma).

     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>";
        }
     }
    }
    

    try this

    $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>";
        }
    

    this works fine for me just tested now

    comment if working. comment if not-working.

    点赞 评论
  • duanji1026
    duanji1026 2014-10-09 12:24

    You need to have a column, let we name it parentId in the s_category table.

    Example values in there:

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

    So your structure will be like:

    - Vehicles
       - Cars
       - Bicycles
    - Planes
    

    You'll need to get the data like this (to be considered as pseudocode):

    $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>';
       }
    }
    

    You need to re-write the SQL part to match your case. This will work if you have just one inner level, e.g. no sub-sub-menus. If you plan to use sub-sub-menus you'll need recursive function.

    点赞 评论
  • doudeng8691
    doudeng8691 2017-03-30 11:52

    created your database and write function with code and change parameters

      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>
    
    点赞 评论

相关推荐