Totally Zenned
- Join Date:
- Jan 2005
- Posts:
- 1,285
- Plugin Contributions:
- 0
Non-recursive category_tree::zen_show_category
Not sure where or even if to post this, but figured it can't hurt and maybe it'll even help somebody.
After importing a few hundred categories (with my new import tool..), where about 100 are top-categories, I found that the recursive method category_tree::zen_show_category would crash my local apache server with PHP 4.4.4 with this message in the apache log:
[notice] Parent: child process exited with status 3221225725 -- Restarting.
There was nothing in the php log, but after googling it I found a post mentioning recursive functions and that was indeed the problem. PHP was crashing because category_tree::zen_show_category would make too many recursive calls to itself. When I tried with less categories there was no problem. PHP 5.2 does not have this problem.
It may be just a setting somewhere on my server, but instead of finding whether this is an apache/php/xampp bug I decided to make a non-recursive version of the responsible class method.
/**
* Non-recursive version of category_tree::zen_show_category
*/
function ShowCategory ($first_element)
{
global $cPath_array;
$catID = (int)$first_element;
$row = 0;
# checks that the item is not 0, false or not set
while (!empty($this->tree[$catID]))
{
# NOTE: $this->tree[$catID]['parent'] is a string, but would make more sense as integer - consider forcing (int) when setting the var
if ($this->tree[$catID]['parent'] == 0) {
$cPath_new = 'cPath=' . $catID;
$this->box_categories_array[$row]['top'] = 'true';
} else {
$this->box_categories_array[$row]['top'] = 'false';
$cPath_new = 'cPath=' . $this->tree[$catID]['path'];
}
$this->box_categories_array[$row]['path'] = $cPath_new;
if (isset($cPath_array) && in_array($catID, $cPath_array)) {
$this->box_categories_array[$row]['current'] = true;
} else {
$this->box_categories_array[$row]['current'] = false;
}
// display category name
# if parent is 0 don't add a prefix to the category name, otherwise repeat CATEGORIES_SUBCATEGORIES_INDENT (int)$this->tree[$catID]['level'] amount of times and add CATEGORIES_SEPARATOR_SUBS before the category name
$this->box_categories_array[$row]['name'] = ($this->tree[$catID]['parent'] == 0 ? '' : str_repeat(CATEGORIES_SUBCATEGORIES_INDENT, (int)$this->tree[$catID]['level']) . CATEGORIES_SEPARATOR_SUBS) . $this->tree[$catID]['name'];
// make category image available in case needed
$this->box_categories_array[$row]['image'] = $this->tree[$catID]['image'];
if (zen_has_category_subcategories($catID)) {
$this->box_categories_array[$row]['has_sub_cat'] = true;
} else {
$this->box_categories_array[$row]['has_sub_cat'] = false;
}
if (SHOW_COUNTS == 'true') {
$products_in_category = zen_count_products_in_category($catID);
if ($products_in_category > 0) {
$this->box_categories_array[$row]['count'] = $products_in_category;
} else {
$this->box_categories_array[$row]['count'] = 0;
}
}
# break loop if there's no next_id
if (empty($this->tree[$catID]['next_id']))
{
break;
}
# get next category ID
$catID = (int)$this->tree[$catID]['next_id'];
$row++;
}
return $this->box_categories_array;
}
It does the same thing with a while loop instead of recursive function calls.
Perhaps not very interesting if this only happens on my system, but I also made a small improvement in code readability to how the category name is found.
Current way of finding category names:
$this->categories_string = "";
for ($i=0; $i<$this->tree[$counter]['level']; $i++) {
if ($this->tree[$counter]['parent'] != 0) {
$this->categories_string .= CATEGORIES_SUBCATEGORIES_INDENT;
}
}
if ($this->tree[$counter]['parent'] == 0) {
...
} else {
...
$this->categories_string .= CATEGORIES_SEPARATOR_SUBS;
}
// display category name
$this->box_categories_array[$ii]['name'] = $this->categories_string . $this->tree[$counter]['name'];
My one-line version of the same thing:
// display category name
# if parent is 0 don't add a prefix to the category name, otherwise repeat CATEGORIES_SUBCATEGORIES_INDENT (int)$this->tree[$counter]['level'] amount of times and add CATEGORIES_SEPARATOR_SUBS before the category name
$this->box_categories_array[$ii]['name'] = ($this->tree[$counter]['parent'] == 0 ? '' : str_repeat(CATEGORIES_SUBCATEGORIES_INDENT, (int)$this->tree[$counter]['level']) . CATEGORIES_SEPARATOR_SUBS) . $this->tree[$counter]['name'];