Jump to content

Help with understanding a function


evanct

Recommended Posts

I guess I don't understand arrays well enough, I can't wrap my head around this thing.

 

say for an example $categories is

(0 => ('cat_id' => 8, 'cat_parent_id' => 2),
1 => ('cat_id' => 2, 'cat_parent_id' => 5));

 

and $parentId is 8.

 

the code comments aren't helping me very much. the function:

/*
    Return the current category list which only shows
    the currently selected category and it's children.
    This function is made so it can also handle deep
    category levels ( more than two levels )
*/
function formatCategories($categories, $parentId)
{
    // $navCat stores all children categories
    // of $parentId
    $navCat = array();
    
    // expand only the categories with the same parent id
    // all other remain compact
    $ids = array();
    foreach ($categories as $category) {
        if ($category['cat_parent_id'] == $parentId) {
            $navCat[] = $category;
        }
        
        // save the ids for later use
        $ids[$category['cat_id']] = $category;
    }    

    $tempParentId = $parentId;
    
    // keep looping until we found the
    // category where the parent id is 0
    while ($tempParentId != 0) {
        $parent    = array($ids[$tempParentId]);
        $currentId = $parent[0]['cat_id'];

        // get all categories on the same level as the parent
        $tempParentId = $ids[$tempParentId]['cat_parent_id'];
        foreach ($categories as $category) {
            // found one category on the same level as parent
            // put in $parent if it's not already in it
            if ($category['cat_parent_id'] == $tempParentId && !in_array($category, $parent)) {
                $parent[] = $category;
            }
        }
        
        // sort the category alphabetically
        array_multisort($parent);
    
        // merge parent and child
        $n = count($parent);
        $navCat2 = array();
        for ($i = 0; $i < $n; $i++) {
            $navCat2[] = $parent[$i];
            if ($parent[$i]['cat_id'] == $currentId) {
                $navCat2 = array_merge($navCat2, $navCat);
            }
        }
        
        $navCat = $navCat2;
    }


    return $navCat;
}

function fetchCategories()
{
    $sql = "SELECT cat_id, cat_parent_id, cat_name, cat_image, cat_description
            FROM tbl_category
            ORDER BY cat_id, cat_parent_id ";
    $result = dbQuery($sql);
    
    $cat = array();
    while ($row = dbFetchAssoc($result)) {
        $cat[] = $row;
    }
    
    return $cat;
} 

 

In particular:

 

$ids[$category['cat_id']] = $category;

 

and the entire

while ($tempParentId != 0)

loop. Essentially I'm having trouble understanding the structure of all these arrays and how they relate to one another.

Link to comment
https://forums.phpfreaks.com/topic/157286-help-with-understanding-a-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.