Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7573145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:06:27+00:00 2026-05-30T16:06:27+00:00

I am planning to create a multi select drop down or multi select check

  • 0

I am planning to create a multi select drop down or multi select check box form element in php. But with a parent child menu item relation ship.

Ex: So I have got three separate tables tbl_branch, tbl_area, tbl_region and 2 more tables for the mapping tbl_regiontoarea and tbl_areatobranch. Now I need a multi select or multi check box which looks some thing like this:

Branch 1
 Area 1
   Region 1
   Region 2
 Area 2
   Region 3
   Region 4
   Region 7
   Region 9
 Area 5
 Area 7
   Region 8

Branch 2 
 Area 3

Branch 3
Branch 4
Branch 5
 Area 6
   Region 10
 Area 2

So as you would have guessed it, Branch is the top most in the hierarchy and region is the lowermost i.e. Branch is the parent. I would like it to loop until it finds children for it. Once it stops finding any children it would move on to next parent item in the array.

I am very bad with arrays, and forming complex sql queries and hence was looking forward to have some starting direction on how to do this. I would really appreciate if some one could help me with the same.

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:06:28+00:00Added an answer on May 30, 2026 at 4:06 pm

    I suggest you give this a read

    http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

    I’ve set up a tree of near identical data and using the methods suggested in the above document make the issue significantly simpler especially when it comes to determining parents and siblings down to n levels.

    The short version is that you assign a number on ‘left’ and ‘right’ of each node, and using that you can then select segments of the tree very efficiently. The more long winded version is in that article, and explains it far more thoroughly than I can and includes useful MySQL code.

    Here’s a potential outline for your main table

    treeTable (
        // ID field
        id INT UNSIGNED PK AI,
        // Fields for branching
        lft INT UNSIGNED,
        rgt INT UNSIGNED,
        // ID fields for the specific types
        leafType ENUM('Branch','Area','Region'),
        leafID INT UNSIGNED,
        leafName VARCHAR
    )
    
    UNIQUE INDEX (leafType,leafID)
    

    If you need specific data for each type then you could create seperate tables for them but the above would let you create your tree, map out the id’s and allow relationships to be stored along with a display name.

    Of course the more data you can normalise between each leafType the better, ideally they are all effectively the same type of data after all as they are all a geographic subsection

    Taking a sample of your data as example

    Branch 3
    Branch 4
    Branch 5
     Area 6
       Region 10
     Area 2
    

    you might end up with table such as

    id  lft rgt leafType    leafID  
    1   1   2   'Branch'    3   
    2   3   4   'Branch'    4
    3   5   12  'Branch'    5
    4   6   9   'Area'      6
    5   7   8   'Region'    10
    6   10  11  'Area'      2
    

    Queries for how to insert etc can be found in the linked article, i.e. (quoted verbatim from http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/)

    LOCK TABLE nested_category WRITE;
    
    SELECT @myLeft := lft FROM nested_category
    
    WHERE name = '2 WAY RADIOS';
    
    UPDATE nested_category SET rgt = rgt + 2 WHERE rgt > @myLeft;
    UPDATE nested_category SET lft = lft + 2 WHERE lft > @myLeft;
    
    INSERT INTO nested_category(name, lft, rgt) VALUES('FRS', @myLeft + 1, @myLeft + 2);
    
    UNLOCK TABLES;
    

    And then a SELECT can illustrate the tree (again verbatim from the source)

    SELECT CONCAT( REPEAT( ' ', (COUNT(parent.name) - 1) ), node.name) AS name
    FROM nested_category AS node,
            nested_category AS parent
    WHERE node.lft BETWEEN parent.lft AND parent.rgt
    GROUP BY node.name
    ORDER BY node.lft;
    

    Field/table names in the 2 above are of course unchanged, but you can update the table/field names as necessary

    Following my latest comment, below is the definition for my stored procedure I use for inserting new rows

    CREATE PROCEDURE `addOptionalExtraToTree`(inParentOptionID INT,inOptionName VARCHAR(40),inLeafType VARCHAR(20))
    BEGIN
        DECLARE myRight INT UNSIGNED DEFAULT 0;
    
        SELECT optionalExtra_tree_right-1
        INTO myRight
        FROM optionalExtras
        WHERE optionalExtraID = inParentOptionID;
    
        IF myRight = 0 THEN
            SELECT COALESCE(MAX(optionalExtra_tree_right),0)
            INTO myRight
            FROM optionalExtras;
        END IF;
    
        UPDATE optionalExtras 
        SET optionalExtra_tree_right = optionalExtra_tree_right + 2 
        WHERE optionalExtra_tree_right > myRight;
    
        UPDATE optionalExtras 
        SET optionalExtra_tree_left = optionalExtra_tree_left + 2 
        WHERE optionalExtra_tree_left > myRight;
    
        INSERT INTO optionalExtras (
            optionalExtra_name,
            optionalExtra_leaf_type,
            optionalExtra_tree_left,
            optionalExtra_tree_right
        ) VALUES (
            inOptionName,
            inLeafType,
            myRight + 1,
            myRight + 2
        );
    
        SELECT LAST_INSERT_ID() AS optionalExtraID;
    
    END;
    

    This is then called using calls such as the following

    CALL addOptionalExtraToTree(0,'New root option','Region');
    CALL addOptionalExtraToTree(4,'New child option','Area');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm planning to create a program for manipulating multi-track OGG files, but I don't
We are planning to create a multi level repository with the following structure RepositoryRoot
I'am planning to create a git in my own server, but I need to
I'm planning to create Yahoo Messenger bot using C#, but until now, I cant
I am planning to create a facebook canvas app using php sdk. Google app
I'm planning to create a blog using php/mysql on apache (probably on a linux
I am planning to create a component that will be able to check user's
I am planning to create an app in rails but first I want to
I am trying to build a multi-page form with Yii, but am quite new
I am planning to create a restful web service (PHP). api/user api/user/[id] api/ad api/ad/[id];

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.