Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Cannot redeclare tep_make_cat_ullist()


beachkitty85

Recommended Posts

I am using a contribution for my category box that uses an unordered list. The problem is, each time I clear my browser's cache, I get this error:

 

Fatal error: Cannot redeclare tep_make_cat_ullist() (previously declared in /xxx/includes/boxes/categories.php:70) in /xxx/includes/boxes/categories.php on line 70

 

I checked and tep_make_cat_ullist() is only being declared once.

 

If I refresh the page, the error disappears until the cache is cleared again.

 

Here's the page in question.

 

Any suggestions?

Link to comment
Share on other sites

I guess it is only happening when the cache is cleared because that is when the function is called. So if you are sure it is only declared once, maybe the function is declared inside a loop. Something along the lines of

 

	$rows = 0;
while ($categories = tep_db_fetch_array($categories_query)) {
	   tep_make_cat_ullist($categories) {
		   build the function;
			  more function code;
}
  $rows++;

 

you know what I mean? If that is happening, put the function into general.php and just call it from where it is now.

Link to comment
Share on other sites

I guess it is only happening when the cache is cleared because that is when the function is called. So if you are sure it is only declared once, maybe the function is declared inside a loop. Something along the lines of

 

	$rows = 0;
while ($categories = tep_db_fetch_array($categories_query)) {
	   tep_make_cat_ullist($categories) {
		   build the function;
			  more function code;
}
  $rows++;

 

you know what I mean? If that is happening, put the function into general.php and just call it from where it is now.

 

Thank you so much for your help. I know very little PHP, so I may have never figured this out! So, if you don't mind answering a question: Why did I get the error when the function was in boxes/categories.php and not when it was moved to functions/general.php? Here's the categories.php code:

 

<!-- categories //-->
<?php
// BEGIN Configuration options

 // Set to false to display the unordered list only. Set to true to display in
// a regular box. The former is useful for better integrating the menu with your layout.
$show_ulcats_as_box = true;

// Indicates whether or not to render your entire category list or just the root categories
// and the currently selected submenu tree. Rendering the full list is useful for dynamic menu
// generation where you want the user to have instant access to all categories. The other option
// is the default oSC behaviour, when the subcats aren't available until the parent is clicked. 
$show_full_tree = false;	

// This is the CSS *ID* you want to assign to the UL (unordered list) containing
// your category menu. Used in conjuction with the CSS list you create for the menu.
// This value cannot be blank.
$idname_for_menu = 'nav';

// This is the *CLASSNAME* you want to tag a LI to indicate the selected category.
// The currently selected category (and its parents, if any) will be tagged with
// this class. Modify your stylesheet as appropriate. Leave blank or set to false to not assign a class. 
$classname_for_selected = 'selected';

// This is the *CLASSNAME* you want to tag a LI to indicate a category has subcategores.
// Modify your stylesheet to draw an indicator to show the users that subcategories are
// available. Leave blank or set to false to not assign a class. 	
$classname_for_parent = 'daddy';

// This is the HTML that you would like to appear before your categories menu if *not*
// displaying in a standard "box". This is useful for reconciling tables or clearing
// floats, depending on your layout needs.	
$before_nobox_html = '';

// This is the HTML that you would like to appear after your categories menu if *not*
// displaying in a standard "box". This is useful for reconciling tables or clearing
// floats, depending on your layout needs.	
 $after_nobox_html = '<div style="clear: both;"></div>';	


// END Configuration options

// Global Variables
$GLOBALS['this_level'] = 0;

// Initialize HTML and info_box class if displaying inside a box
//if ($show_ulcats_as_box) {
//echo '<tr><td>';
//$info_box_contents = array();
//$info_box_contents[] = array('text' => BOX_HEADING_CATEGORIES);
  //new infoBoxAltHeading($info_box_contents, false, false);					
//}

// Generate a bulleted list (uses configuration options above)
$categories_string = tep_make_cat_ullist();

// Output list inside a box if specified, otherwise just output unordered list
if ($show_ulcats_as_box) {
$info_box_contents = array();
$info_box_contents[] = array('text' => $categories_string);
new infoBoxAlt($info_box_contents);
} else {
	echo $before_nobox_html;	
echo $categories_string;
	echo $after_nobox_html;
}

?>
<!-- categories_eof //-->

 

And here's the function in general.php:

 

// Create the root unordered list
function tep_make_cat_ullist($rootcatid = 0, $maxlevel = 0){

global $idname_for_menu, $cPath_array, $show_full_tree, $languages_id;

// Modify category query if not fetching all categories (limit to root cats and selected subcat tree)
	if (!$show_full_tree) {
	$parent_query	= 'AND (c.parent_id = "0"';	

			if (isset($cPath_array)) {

				$cPath_array_temp = $cPath_array;

				foreach($cPath_array_temp AS $key => $value) {
						$parent_query	.= ' OR c.parent_id = "'.$value.'"';
					}

					unset($cPath_array_temp);
			}	

	$parent_query .= ')';				
	} else {
	$parent_query	= '';	
	}

	$result = tep_db_query('select c.categories_id, cd.categories_name, c.parent_id from ' . TABLE_CATEGORIES . ' c, ' . TABLE_CATEGORIES_DESCRIPTION . ' cd where c.categories_id = cd.categories_id and cd.language_id="' . (int)$languages_id .'" '.$parent_query.' order by sort_order, cd.categories_name');

	while ($row = tep_db_fetch_array($result)) {				
	$table[$row['parent_id']][$row['categories_id']] = $row['categories_name'];
}
$output .= '<h2>' . BOX_HEADING_CATEGORIES . '</h2>';
$output .= '<ul>';
$output .= tep_make_cat_ulbranch($rootcatid, $table, 0, $maxlevel);

	// Close off nested lists
for ($nest = 0; $nest <= $GLOBALS['this_level']; $nest++) {
	$output .= '</ul>';	

	}

return $output;
}

// Create the branches of the unordered list
function tep_make_cat_ulbranch($parcat, $table, $level, $maxlevel) {

global $cPath_array, $classname_for_selected, $classname_for_parent;

$list = $table[$parcat];

while(list($key,$val) = each($list)){

	if ($GLOBALS['this_level'] != $level) {

			if ($GLOBALS['this_level'] < $level) {
					$output .= "\n".'<ul>';
				} else {
			for ($nest = 1; $nest <= ($GLOBALS['this_level'] - $level); $nest++) {
				$output .= '</ul></li>'."\n";	
				}
/*							
							if ($GLOBALS['this_level'] -1 == $level)
$output .= '</ul></li>'."\n";
elseif ($GLOBALS['this_level'] -2 == $level)
$output .= '</ul></li></ul></li>'."\n";
elseif ($GLOBALS['this_level'] -3 == $level)
$output .= '</ul></li></ul></li></ul></li>'."\n";
elseif ($GLOBALS['this_level'] -4 == $level)
$output .= '</ul></li></ul></li></ul></li></ul></li>'."\n"; 
*/							
					}			

			$GLOBALS['this_level'] = $level;
	}

	if (isset($cPath_array) && in_array($key, $cPath_array) && $classname_for_selected) {
		$this_cat_class = ' class="'.$classname_for_selected.'"';
	} else {
		$this_cat_class = '';		
		}	

	$output .= '<li'.$this_cat_class.'><a href="';

	if (!$level) {
				unset($GLOBALS['cPath_set']);
					$GLOBALS['cPath_set'][0] = $key;
		$cPath_new = 'cPath=' . $key;

	} else {
					$GLOBALS['cPath_set'][$level] = $key;		
		$cPath_new = 'cPath=' . implode("_", array_slice($GLOBALS['cPath_set'], 0, ($level+1)));
	}

	if (tep_has_category_subcategories($key) && $classname_for_parent) {
		$this_parent_class = ' class="'.$classname_for_parent.'"';
	} else {
		$this_parent_class = '';		
		}				

	$output .= tep_href_link(FILENAME_DEFAULT, $cPath_new) . '"'.$this_parent_class.'>'.$val;		

	if (SHOW_COUNTS == 'true') {
		$products_in_category = tep_count_products_in_category($key);
		if ($products_in_category > 0) {
			$output .= ' (' . $products_in_category . ')';
		}
	}

	$output .= '</a>';	

	if (!tep_has_category_subcategories($key)) {
		$output .= '</li>'."\n";	
	}						 

	if ((isset($table[$key])) AND (($maxlevel > $level + 1) OR ($maxlevel == '0'))) {
		$output .= tep_make_cat_ulbranch($key,$table,$level + 1,$maxlevel);
	}

	} // End while loop

return $output;
}

Link to comment
Share on other sites

There are others on here that could explain this more accurately than me but I will try, with a simple example.

 

Your new code and the categories function in general, creates a loop so it can count every category and print them out. Your code will output a new <li> for every category so it will loop until it has printed enough.

 

As you are aware, you can only declare a function once so if you declare it within a loop, it will declare the same function for every cycle of the loop. You can call the function in the loop, no problem, as the loop will output the result of the function for every cycle.

 

The simple example. In time honoured tradition, using Hello World!

 

We declare a function to output Hello World! 5 times and then call the function at the end.

 

hello_world.php

<?php

function hello_world() {

	for($i = 0; $i < 5; $i++)
	 {
	  echo ('Hello World!<br>');
 }
}

hello_world();

?>

 

This works as expected, looping through 5 times and printing Hello World! for every loop.

 

Now we build another function with a loop and try to build the hello_world() function within that loop.

 

screw_up.php

<?php

function screw_up() {

for($i = 0; $i < 5; $i++)
   {
	function hello_world() {

		for($i = 0; $i < 5; $i++)
		 {
		  echo ('Hello World!<br>');
		 }
	}
}
}

screw_up();

?>

 

This fails because for every time the function screw_up() loops, it attempts to declare function hello_world() and gives the error

 

Fatal error: Cannot redeclare hello_world() (previously declared in...........)

 

 

So we remove function hello_world() from function screw_up() and just call it for every loop. This is what you did when putting your functions into general.php.

 

no_screw_up.php

<?php

function hello_world() {

	for($i = 0; $i < 5; $i++)
	 {
	  echo ('Hello World!<br>');
 }
}

function no_screw_up() {

for($i = 0; $i < 5; $i++)
   {
	hello_world();
}
}

no_screw_up();

?>

 

When we call no_screw_up() it correctly prints Hello World! 25 times.

 

Obviously this isn't exactly what happened with your code but the principle is the same.

 

Hope this helped, rather than gave you a headache.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...