Solved

Unlimited depth of menu

Posted on 2006-07-02
1
345 Views
Last Modified: 2013-12-12
First of all, sry for my bad english, i hope you understand

I have made a menu width 2 depth of submenu.

But i want to make menu what has 3 or more depth submenu.

Example:
http://gen.kostrika.com/screen.jpg
There tehe menu item has writed by hand.

But i want that, PHP makes automaticly the submenu items. And tehe menu items are in MySql database

Thanks

 
0
Comment
Question by:GenTaliaru
1 Comment
 
LVL 10

Accepted Solution

by:
Khanh Doan earned 125 total points
ID: 17026883
From my code :

<?php
//########################### Cat List ###########################//
function catlist()
{
      $cat = array();
      $totalcats = '1';
      $maxid = '1';

      $cat_query = mysql_query("SELECT * FROM songcat ORDER BY displayorder, cat_name ASC");

      while ($cattemp = mysql_fetch_array($cat_query))
      {
            $id = $cattemp['catid'];
            if ($maxid < $id) $maxid = $id;

            $cat[$totalcats]['id'] = $totalcats;
            $cat[$totalcats]['cat_id'] = $cattemp['catid'];
            $cat[$totalcats]['parent_id'] = $cattemp['parent_catid'];
            $cat[$totalcats]['name'] = $cattemp['cat_name'];

            $totalcats++;
      }

      if ($totalcats == '1')
      {
            $cat_list_bit = $vbphrase['music_nocat'];
      }
      else
      {
            for ($cid = '1'; $cid <= $maxid; $cid++)
            {
                  if ($cat[$cid]['parent_id'] == '0')
                  {
                        $cat_id = $cat[$cid]['cat_id'];
                        $cat_name = $cat[$cid]['name'];
                        $par_num = '0';
                        $cat_list_sub = subcat($cat_id, $cat, $maxid, $par_num);

                        $cat_list_bit .= "<b>" . $cat_name . "</b><br><span>" . $cat_list_sub . "</span>";
                  }
            }
      }

      $cat_list = $cat_list_bit;

      return $cat_list;
}

//########################### Sub Categories ###########################//
function subcat($parent_id, $cat, $maxid, $par_num)
{
      for ($sub_id = 0; $sub_id <= $maxid; $sub_id++)
      {
            if ($cat[$sub_id]['parent_id'] == $parent_id)
            {
                  $cat_id = $cat[$sub_id]['cat_id'];
                  $cat_name = $cat[$sub_id]['name'];

                  $sub_cats_ = '';
                  for ($n = '0'; $n <= $par_num; $n++) $sub_cats_ .= '| - ';

                  $sub_cats_sub = subcat($cat_id, $cat, $maxid, $par_num + 1);

                  $sub_cats .= $sub_cats_ . $cat_name . "<br><span>" . $sub_cats_sub . "</span>";
            }
      }

      return $sub_cats;
}

echo catlist();
?>

My table :

CREATE TABLE IF NOT EXISTS songcat (
  catid int(11) NOT NULL auto_increment,
  parent_catid int(11) NOT NULL default '0',
  cat_name varchar(250) NOT NULL default '',
  displayorder INT NOT NULL DEFAULT '0';
  PRIMARY KEY  (catid)
) TYPE=MyISAM;

Goodluck.
Bonmat86.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question