Jump to content

Expand Function to Support More Than Two Levels?


ca99uk

Recommended Posts

Hello everyone,

 

I am currently working on the script taken from www.phpwebcommerce.com I am only a beginner and have been using it in a little project for an online game just to help my mates be able to trade items and such.

 

I have managed to get almost everything sorted apart from one thing. The function which gets the categories for a drop down select box:

 

/*
    Generate combo box options containing the categories we have.
    if $catId is set then that category is selected
*/
function buildCategoryOptions($catId = 0)
{
    $sql = "SELECT cat_id, cat_parent_id, cat_name
            FROM tbl_category
            ORDER BY cat_id";
    $result = dbQuery($sql) or die('Cannot get Product. ' . mysql_error());
    
    $categories = array();
    while($row = dbFetchArray($result)) {
        list($id, $parentId, $name) = $row;
        
        if ($parentId == 0) {
            // we create a new array for each top level categories
            $categories[$id] = array('name' => $name, 'children' => array());
        } else {
            // the child categories are put int the parent category's array
            $categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);    
        }
    }    
    
    // build combo box options
    $list = '';
    foreach ($categories as $key => $value) {
        $name     = $value['name'];
        $children = $value['children'];
        
        $list .= "<optgroup label=\"$name\">"; 
        
        foreach ($children as $child) {
            $list .= "<option value=\"{$child['id']}\"";
            if ($child['id'] == $catId) {
                $list.= " selected";
            }
            
            $list .= ">{$child['name']}</option>\r\n";
        }
        
        $list .= "</optgroup>";
    }
    
    return $list;
}


 

Basically the problem is that it only creates a category list of two levels e.g.

 

Parent

 

    Child

 

and I wish to be able to have more sub categories e.g.

 

Parent

 

    Child

 

        Child

 

            Child

 

Is there anyway of adapting this code so it can do something like that? Many thanks for any help.

Link to comment
Share on other sites

  • 2 weeks later...

Hi all,

 

seeing as getting no where with this im guessing it must be kind of impossible to achieve what I want with this function. So if I abandon that idea is there anyway to modify the code so it just gives a list of all the categories in the select box?

 

Any help would be appreciated.

 

Thank you very much  :)

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.