If this is your first visit, check our documentation. To post, register or login.
Forums / Zen Cart Code Suggestions / Non-recursive category_tree::zen_show_category

Non-recursive category_tree::zen_show_category

Started by dwno on 14 Apr 2007, 4:42 PM · Views: 1,567

Results 1 to 5 of 5
14 Apr 2007, 4:42 PM
#1
dwno avatar

dwno

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'];
23 Jul 2016, 7:12 PM
#2
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,760
Plugin Contributions:
56

Re: Non-recursive category_tree::zen_show_category

If just experienced this issue for the first time (after many years of working with Zen Cart), and this fix works like a charm. :)

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.

23 Jul 2016, 8:04 PM
#3
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,760
Plugin Contributions:
56

Re: Non-recursive category_tree::zen_show_category

If you get this issue, the debug log you see will start with something roughly like this:

[23-Jul-2016 14:53:28 America/New_York] PHP Fatal error: Maximum function nesting level of '100' reached, aborting! in /includes/classes/query_cache.php on line 42

A temporary workaround could be to add this to an extra_configures file:

ini_set('xdebug.max_nesting_level','200');

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.

23 Jul 2016, 8:14 PM
#4
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Non-recursive category_tree::zen_show_category

What application causes this?

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

23 Jul 2016, 9:27 PM
#5
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,760
Plugin Contributions:
56

Re: Non-recursive category_tree::zen_show_category

categories_select sidebox.

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.