SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multidimensional array generated from database

    Not sure that's even the right way to describe what I'm trying to do, but it's the best I can think of.

    I've got a database containing a list of categories for example -

    Code:
    cat_id | cat_sub_id | name
    1      | null       | Wheel
    2      | 1          | Hub
    3      | null       | Gearbox
    4      | 2          | Bearing
    5      | 3          | Gear
    6      | null       | Engine
    What I want to do, is then generate an array containing the structure for then generating an ordered menu.
    For example,
    $cat[1][2][4]
    $cat[3][5]
    $cat[6]
    The purpose of the array is because the actual menu type/layout will vary depending on where it's displayed

    At the moment, I'm fetching a resultset sorted by cat_sub_id, then passing that to a while loop containing an if statement, and another array for storing the category names -
    Code PHP:
    		while ($row = db_fetch_assoc($cat) {
    		// first of, generate array for menu structure
    		if($row['cat_sub_id'] == null){		// if part_category_sub is null, then we have a parent item
    			$cat['cat_id'][$row['cat_id']] = null;
    		} else {								// else we a child item
    			$cat['cat_id'][$row['cat_sub_id']][$row['cat_id']] = null;
    		}
    		$cat['name'][$row['cat_id']] = $row['name'];
    	}

    The problem with that, is it only allows for an array depth of two, because if the parent category for a category, is also a child category, then the logic in the loop, creates a new parent array, so I end up with -
    $cat[1][2]
    $cat[2][4]
    $cat[3][5]
    $cat[6]

    I'm using the above code just now, as it's functional enough so I can get on with other coding (and I needed a break from this particular problem!), but I know it needs redone.
    I'm currently failing to find a solution to handle generating a variable depth array. I'm thinking I probably need some kind of recursive function call, but I'm having a complete mental block about how to acheive it!

    Any suggestions are welcome.

  2. #2
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why does it need to be that way? I'm not sure I see the point.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,139
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Expanding on the link crmalibu provided to your exact schema requirements:

    PHP Code:
    <?php
    $arrRows 
    = array();
    $arrRows[] = array('cat_id'=>1,'cat_sub_id'=>null,'name'=>'Wheel');
    $arrRows[] = array('cat_id'=>2,'cat_sub_id'=>1,'name'=>'Hub');
    $arrRows[] = array('cat_id'=>3,'cat_sub_id'=>null,'name'=>'Gearbox');
    $arrRows[] = array('cat_id'=>4,'cat_sub_id'=>2,'name'=>'Bearing');
    $arrRows[] = array('cat_id'=>5,'cat_sub_id'=>3,'name'=>'Gear');
    $arrRows[] = array('cat_id'=>6,'cat_sub_id'=>null,'name'=>'Engine');

    parse_into_menu(parse_into_tree(null,$arrRows,'cat_id','cat_sub_id','categories'),'categories','name');

    function 
    parse_into_tree($intParentId,&$arrRows,$strIdField,$strParentsIdField,$strNameResolution) {
        
        
    $arrChildren = array();

        for(
    $i=0;$i<count($arrRows);$i++) {
            if(
    $intParentId === $arrRows[$i][$strParentsIdField]) {
                
    $arrChildren array_merge($arrChildren,array_splice($arrRows,$i--,1));
            }
        }
        
        
    $intChildren count($arrChildren);
        if(
    $intChildren != 0) {
            for(
    $i=0;$i<$intChildren;$i++) {
                
    $arrChildren[$i][$strNameResolution] = parse_into_tree($arrChildren[$i][$strIdField],$arrRows,$strIdField,$strParentsIdField,$strNameResolution);
            }        
        }
        
        return 
    $arrChildren;

    }


    function 
    parse_into_menu($arrMenu,$strChildKey,$strNameKey,$intRunner=0) {
        
        
    $strNL "\n";
        
    $intMenu count($arrMenu);
        
        for(
    $i=0;$i<$intMenu;$i++) {
            if(
    $i==0) { 
                echo 
    str_repeat("\t",$intRunner),'<ul>',$strNL
            }
            
            if(!empty(
    $arrMenu[$i][$strChildKey])) {
                echo 
    str_repeat("\t",$intRunner+1),'<li>',$arrMenu[$i][$strNameKey],$strNL;
                
    parse_into_menu($arrMenu[$i][$strChildKey],$strChildKey,$strNameKey,$intRunner+2);
                echo 
    str_repeat("\t",$intRunner+1),'</li>',$strNL;
            } else {
                echo 
    str_repeat("\t",$intRunner+1),'<li>',$arrMenu[$i][$strNameKey],'</li>',$strNL;
            }
            
            if(
    $i==($intMenu-1)) { 
                echo 
    str_repeat("\t",$intRunner),'</ul>',$strNL
            }
        }
        
    }
    parse_into_tree() will create the tree using nested arrays.
    parse_into_menu() will build the menu once it is in a tree format.

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent!
    Thanks for that.

    Think I can just about understand how it works (arrays aren't my strongest point), and how to implement it to do what I want it to.

  7. #7
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by m_c View Post
    Excellent!
    Thanks for that.

    Think I can just about understand how it works (arrays aren't my strongest point), and how to implement it to do what I want it to.
    To be pro in PHP programming, you have to master in Arrays.
    What you think guys?

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In any kind of programming, arrays and recursive thinking are usually pretty important.

  9. #9
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHPycho View Post
    To be pro in PHP programming, you have to master in Arrays.
    What you think guys?
    If there is one thing you absolutely need to master in PHP then that would be arrays. No ifs, ands or buts about it. They are to PHP what the Struct is to C. Also, for mysql sites having the ability to serialize and unserialize arrays is essential.

  10. #10
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do understand the basics of arrays, and can handle the basics of manipulating them (creating them, iterating through them, getting the values I want), but when it comes to more advanced manipulation (like above), that's where I struggle.

    But I'm gradually learning more.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •