Jump to content

[SOLVED] Getting menu and child links with one query


jordanwb

Recommended Posts

I have the following two classes in my database:

 

CREATE TABLE IF NOT EXISTS `scm_menus` (
  `menu_id` int(11) NOT NULL auto_increment,
  `menu_name` varchar(32) NOT NULL,
  PRIMARY KEY  (`menu_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


CREATE TABLE IF NOT EXISTS `scm_menu_links` (
  `link_id` int(11) unsigned NOT NULL auto_increment,
  `link_name` varchar(32) NOT NULL,
  `link_href` text NOT NULL,
  `link_target` varchar(16) NOT NULL default '_self',
  `link_parent` int(10) unsigned NOT NULL,
  `link_order` smallint(5) unsigned NOT NULL,
  PRIMARY KEY  (`link_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

 

Now the field "link_parent" in scm_menu_links refers to "menu_id" in scm_menus. I also want the links to be ordered by "link_order" ascending. I want to store all links in an array like so:

 

$menus['menu_id']['name'] = {Content of 'menu_name'}

$menus['menu_id']['links'][0] = {First Link}

$menus['menu_id']['links'][1] = {First Link}

 

 

I hope what I'm asking makes sense because I don't know how to ask the question.  ::)

Link to comment
Share on other sites

Try a query similar to this:

 

select

          menu_name

        , link_name

        , link_href

        , link_target

 

from  scm_menus

join    scm_menu_links

on      link_parent = menu_id

 

order by  link_order ASC

 

 

Just read each row from the result to build the array however you want it to be.

 

Note: The link_parent should be the same size as the menu_id. One should not be int(10) and the other int(11) (and whether negative allowed or not).

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.