簡體   English   中英

顯示子類別和子/子類別的類別

[英]display category of subcategories and sub/subcategories

我的PHP初學者。我開發我的第一個動態網站- > http://www.afrogfx.com如果你看一下我的側邊欄,你會看到一個類別列出位於這里我的問題,如果我創建子子類別一樣,

    CAT A
    - SUB CAT A-1
    - SUB CAT A-2
      -- SUB CAT A-3 ( problem Here )
    CAT B
    - SUB CAT B-1
    - SUB CAT B-2
    - SUB CAT B-2-a
      -- SUB CAT B-2-b ( problem Here )
       --- SUB CAT B-3 ( problem Here )

類別列表代碼

    <?php 
    mysql_select_db($db_name, $conn); // Change for your database  
    $query_Recordset1 = "SELECT catid,catname,parentid FROM categories ";
    $Recordset1 = mysql_query($query_Recordset1, $conn) or die(mysql_error()); // Change for your database


    while ( $row = mysql_fetch_assoc($Recordset1) )
    {
    $menu_array[$row['catid']] = array('catname' => $row['catname'],'catid' =>         $row['catid'],'parentid' => $row['parentid']);

    }
    //recursive function that prints categories as a nested html unordered list

    function generate_menu($parent)
    {
    $has_childs = false;
    //this prevents printing 'ul' if we don't have subcategories for this category
    global $menu_array;
    //use global array variable instead of a local variable to lower stack memory         requierment
    foreach($menu_array as $key => $value)
    {
    if ($value['parentid'] == $parent) 
    {       
    //if this is the first child print '<ul>'                       
    if ($has_childs === false)
    {
    //don't print '<ul>' multiple times                             
    $has_childs = true;
    //echo '<ul>';
    echo '<ul id="categories">';
    }
    echo '<li><a href="categories?catid=' . $value['catid'] . '&parentid=' .                 $value['parentid'] . '&catname=' . $value['catname'] .'">' .         $value['catname'] . '</a>';
    echo '<input type="hidden" value="' . $value['catname'] . '" />';
    generate_menu($key);

    //call function again to generate nested list for subcategories belonging to this         category

    echo '</li>';
    }
    }

    if ($has_childs === true) echo '</ul>';
    }
    //generate menu starting with parent categories (that have a 0 parent)
    ?>

現在,我需要選擇主類別中所有主題的功能,然后將其選擇為&子類別! 我該怎么做 ?? !!

我將此代碼用於帶有子菜單的菜單

這是功能

// Menu builder function, parentId 0 is the root
function buildMenu($parent, $menu) {
 $html = "";
 if (isset($menu['parents'][$parent]))
 {
  $html .= "
  <ul>\n";
   foreach ($menu['parents'][$parent] as $itemId)
   {
      if(!isset($menu['parents'][$itemId]))
      {     
         $html .= "<li>\n  <a href='".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a>\n</li> \n";

      }
      if(isset($menu['parents'][$itemId]))
      {
         $html .= "
         <li><span>" . $menu['items'][$itemId]['label'] . "<b></b></span>" ;
         $html .= buildMenu($itemId, $menu);
         $html .= "</li> \n";
      }
   }
  $html .= "</ul> \n";

  }
  return $html;
}

這是電話

// Select all entries from the menu table
$sql = "SELECT id, label, link, parent FROM dbo.Menu ORDER BY parent, sort, label";
$result = $database->query($sql);
$menu = array(
'items' => array(),
'parents' => array()
);
// Builds the array lists with data from the menu table
while ($items = sqlsrv_fetch_array( $result )) {
// Creates entry into items array with current menu item id ie. $menu['items'][1]
$menu['items'][$items['id']] = $items;
// Creates entry into parents array. Parents array contains a list of all items with     children
$menu['parents'][$items['parent']][] = $items['id'];
}

我的數據庫中有一個帶有列的SQL表

  • ID
  • 標簽
  • 鏈接
  • 父級
  • 分類

如果您將所有頂級菜單項都設置為父級0,那么您想成為父級下的子菜單的每個項目都將其置於父級ID中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM