From c54b6371e075e630e57a616b36bf855617a802cb Mon Sep 17 00:00:00 2001 From: acidvertigo Date: Tue, 26 Aug 2014 12:29:19 +0200 Subject: [PATCH 1/3] Build the tree hierachy in one pass With this function can be easy to acess category data in one query and build category menu, count sub-categories etc.. for example the array resulting from this is: ``` Array ( [3] => Array ( [children] => Array ( [10] => Array ( [id] => 10 [parent_id] => 3 [name] => Action [image] => subcategory_action.gif ) [13] => Array ( [id] => 13 [parent_id] => 3 [name] => Cartoons [image] => subcategory_cartoons.gif ) [12] => Array ( [id] => 12 [parent_id] => 3 [name] => Comedy [image] => subcategory_comedy.gif ) [15] => Array ( [id] => 15 [parent_id] => 3 [name] => Drama [image] => subcategory_drama.gif ) [11] => Array ( [id] => 11 [parent_id] => 3 [name] => Science Fiction [image] => subcategory_science_fiction.gif ) [14] => Array ( [id] => 14 [parent_id] => 3 [name] => Thriller [image] => subcategory_thriller.gif ) ) [id] => 3 [parent_id] => 0 [name] => DVD Movies [image] => category_dvd_movies.gif ) [21] => Array ( [id] => 21 [parent_id] => 0 [name] => Gadgets [image] => category_gadgets.png ) [1] => Array ( [children] => Array ( [17] => Array ( [id] => 17 [parent_id] => 1 [name] => CDROM Drives [image] => subcategory_cdrom_drives.gif ) [4] => Array ( [id] => 4 [parent_id] => 1 [name] => Graphics Cards [image] => subcategory_graphic_cards.gif ) [8] => Array ( [id] => 8 [parent_id] => 1 [name] => Keyboards [image] => subcategory_keyboards.gif ) [16] => Array ( [id] => 16 [parent_id] => 1 [name] => Memory [image] => subcategory_memory.gif ) [9] => Array ( [id] => 9 [parent_id] => 1 [name] => Mice [image] => subcategory_mice.gif ) [6] => Array ( [id] => 6 [parent_id] => 1 [name] => Monitors [image] => subcategory_monitors.gif ) [5] => Array ( [id] => 5 [parent_id] => 1 [name] => Printers [image] => subcategory_printers.gif ) [7] => Array ( [id] => 7 [parent_id] => 1 [name] => Speakers [image] => subcategory_speakers.gif ) ) [id] => 1 [parent_id] => 0 [name] => Hardware [image] => category_hardware.gif ) [2] => Array ( [children] => Array ( [19] => Array ( [id] => 19 [parent_id] => 2 [name] => Action [image] => subcategory_action_games.gif ) [18] => Array ( [id] => 18 [parent_id] => 2 [name] => Simulation [image] => subcategory_simulation.gif ) [20] => Array ( [id] => 20 [parent_id] => 2 [name] => Strategy [image] => subcategory_strategy.gif ) ) [id] => 2 [parent_id] => 0 [name] => Software [image] => category_software.gif ) ) ``` Based on this article found on web http://blog.ideashower.com/post/15147134343/create-a-parent-child-array-structure-in-one-pass --- catalog/includes/functions/general.php | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/catalog/includes/functions/general.php b/catalog/includes/functions/general.php index 7a87c5167..6ec2cfc96 100644 --- a/catalog/includes/functions/general.php +++ b/catalog/includes/functions/general.php @@ -561,6 +561,36 @@ function tep_get_manufacturers($manufacturers_array = '') { return $manufacturers_array; } +//// +// Returns the address_format_id for the given country +// TABLES: categories,categories_description; + function tep_path_tree() { + + $categories_query = tep_db_query("select c.categories_id,c.parent_id,cd.categories_name,c.categories_image from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = cd.categories_id and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'"); + + $refs = array(); + $tree = array(); + + while ($categories = tep_db_fetch_array($categories_query)) { + $arr_ref = & $refs[$categories['categories_id']]; + + $arr_ref['id'] = $categories['categories_id']; + $arr_ref['parent_id'] = $categories['parent_id']; + $arr_ref['name'] = $categories['categories_name']; + $arr_ref['image'] = $categories['categories_image']; + + if ($categories['parent_id'] == 0) { + //Root node + $tree[$categories['categories_id']] = & $arr_ref; + } else { + //Children node + $refs[$categories['parent_id']]['children'][$categories['categories_id']] = & $arr_ref; + } + } + unset($arr_ref); + return $tree; + } + //// // Return all subcategory IDs // TABLES: categories From f3dde064625addef5009cae3d6410c9ed85cd8e0 Mon Sep 17 00:00:00 2001 From: acidvertigo Date: Tue, 26 Aug 2014 12:31:53 +0200 Subject: [PATCH 2/3] fix comment --- catalog/includes/functions/general.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/catalog/includes/functions/general.php b/catalog/includes/functions/general.php index 6ec2cfc96..cd087ae45 100644 --- a/catalog/includes/functions/general.php +++ b/catalog/includes/functions/general.php @@ -562,7 +562,7 @@ function tep_get_manufacturers($manufacturers_array = '') { } //// -// Returns the address_format_id for the given country +// Returns the categories tree as array // TABLES: categories,categories_description; function tep_path_tree() { From afe466c428393f6dc0afe5ecf01f70d43b7b6d2a Mon Sep 17 00:00:00 2001 From: acidvertigo Date: Fri, 19 Sep 2014 10:47:36 +0200 Subject: [PATCH 3/3] remove extra spaces --- catalog/includes/functions/general.php | 40 +++++++++++++------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/catalog/includes/functions/general.php b/catalog/includes/functions/general.php index cd087ae45..9821dc06e 100644 --- a/catalog/includes/functions/general.php +++ b/catalog/includes/functions/general.php @@ -566,30 +566,30 @@ function tep_get_manufacturers($manufacturers_array = '') { // TABLES: categories,categories_description; function tep_path_tree() { - $categories_query = tep_db_query("select c.categories_id,c.parent_id,cd.categories_name,c.categories_image from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = cd.categories_id and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'"); + $categories_query = tep_db_query("select c.categories_id,c.parent_id,cd.categories_name,c.categories_image from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = cd.categories_id and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'"); - $refs = array(); - $tree = array(); + $refs = array(); + $tree = array(); - while ($categories = tep_db_fetch_array($categories_query)) { - $arr_ref = & $refs[$categories['categories_id']]; + while ($categories = tep_db_fetch_array($categories_query)) { + $arr_ref = & $refs[$categories['categories_id']]; - $arr_ref['id'] = $categories['categories_id']; - $arr_ref['parent_id'] = $categories['parent_id']; - $arr_ref['name'] = $categories['categories_name']; - $arr_ref['image'] = $categories['categories_image']; + $arr_ref['id'] = $categories['categories_id']; + $arr_ref['parent_id'] = $categories['parent_id']; + $arr_ref['name'] = $categories['categories_name']; + $arr_ref['image'] = $categories['categories_image']; - if ($categories['parent_id'] == 0) { - //Root node - $tree[$categories['categories_id']] = & $arr_ref; - } else { - //Children node - $refs[$categories['parent_id']]['children'][$categories['categories_id']] = & $arr_ref; - } - } - unset($arr_ref); - return $tree; - } + if ($categories['parent_id'] == 0) { + //Root node + $tree[$categories['categories_id']] = & $arr_ref; + } else { + //Children node + $refs[$categories['parent_id']]['children'][$categories['categories_id']] = & $arr_ref; + } + } + unset($arr_ref); + return $tree; + } //// // Return all subcategory IDs