Constructing menu array

Discussion in 'PHP' started by LOOM, May 24, 2014.

  1. #1
    Hi everyone.

    I have to make an array for a category system, first off i don't have a problem getting the parent and first child categories (That's easy :)) but where i'm having trouble with is when there are sub categories in side sub categories, let me explain may setup and all the other things.

    My table structure:
    id,name,order,parent_id

    The basic php so far:
    
    function productCategories(){
    
        $link = dbconnect();
        $fullArry = array();
       
    
        $query = mysqli_query($link, TheQuerySting);
        $queryNumRows = mysqli_num_rows($query);
    
        if($queryNumRows > 0){
            while($queryResults = mysqli_fetch_assoc($query)){
                $id = $queryResults['category_id'];
                $name = $queryResults['category_name'];
                $parentId = $queryResults['parent_id'];
                $order = $queryResults['order'];
    
                if($parentId == '0'){
                    $fullArry[$id]['name'] = $name;
                    $fullArry[$id]['order'] = $order;
                }else{
                    $fullArry[$parentId]['children'][$id]['name'] = $name;
                    $fullArry[$parentId]['children'][$id]['order'] = $order;
                }
            }
        }
    
        return $fullArry;
    }
    
    PHP:
    The Array structure i have in mind:
    
    parent.id#1 =>
        name => parent.name
        order => parent.order
        children =>
            child.id =>
                name => child.name
                order => child.order
            child.id =>
                name => child.name
                order => child.order
                All the child categories will be under the children key even the sub sub categories
    parent.id#2 =>
        name => parent.name
        order => parent.order
        children =>
            child.id =>
                name => child.name
                order => child.order
            child.id =>
                name => child.name
                order => child.order
    ...etc
    
    Code (markup):
    I am having trouble with going passed this point, if i was not using MVC i would have build the table structure inside the function but i can't or the purpose of MVC would not matter, anyway if i do the same as i would without MVC calling the function in itself and building the array that way will cause the server memory to run out.

    Does anyone have an idea of how to do this?
    side note as you can see in the code snippet i am not using OOP.
    Thanks for reading.
     
    LOOM, May 24, 2014 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    Does the array need to have exactly that structure? If not then I would propose a different structure.
     
    stephan2307, May 27, 2014 IP
  3. LOOM

    LOOM Greenhorn

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    HI there i have found a way to build the array and that's with Nested statements in my query (mysql) but thanks for the reply.
     
    LOOM, May 27, 2014 IP