Magento - cannot redeclare categories

998 Views Asked by At

Hi I get an error in the following lines, but I have the following code on two different servers and on one server I don´t get this warning/error:

<?php
// gets the root cat without setting it plain
$root = Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCategories($root);
// create an array for the category navigation
function get_categories($categories) {

    $array= '<ul>';
    foreach($categories as $category) {

        $cat = Mage::getModel('catalog/category')->load($category->getId());
        $imageFile = Mage::getModel('catalog/category')->load($category->getId())->getThumbnail();
        $imageUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . "catalog/category/" . $imageFile;

        // check if a file exists
        if(isset($imageFile)) {
            $image = '<div class="b_imageWrap"><img src="'. $imageUrl .'" /></div>';
        }
        $array .= '<li class="b_catId'.$cat->getId().'">'.'<a href="' . Mage::getUrl($cat->getUrlPath()). '">' . $image . $category->getName() . '<div class="b_arrowFlyout"></div>' . '</a>';
        if($category->hasChildren()) {
            $children = Mage::getModel('catalog/category')->getCategories($category->getId());
            $array .= '<div class="b_flyout"><h1>'.$category->getName().'</h1><h3 class="b_line"><span>UNSERE PRODUKTE</span></h3>';
            $array .= '<ul>';
            foreach($children as $category) {
                $array .= '<li class="b_catId'.$category->getId().'">'.'<a href="' . Mage::helper('catalog/category')->getCategoryUrl($category) . '"><b>' . $category->getName() . '</b></a>';
                $array .= '</li>';
            }
            $array .= '</ul><a class="btn btn-default btn-full" href="' . Mage::getUrl($cat->getUrlPath()). '">ZUR PRODUKTÜBERSICHT</a></div>';
        }
        $array .= '</li>';
    }

    return  $array . '</ul>';
}
echo get_categories($categories); 
?>     

Fatal error: Cannot redeclare get_categories() (previously declared in /var/www/production/htdocs/app/design/frontend/default/default/template/page/html/header.phtml:134) in /var/www/production/htdocs/app/design/frontend/default/default/template/page/html/header.phtml on line 161

Line 134 is: function get_categories($categories) {

Line 161 is: } (before the echo get_categories)

I cannot find the mistake here.

Greetings

Daniel

2

There are 2 best solutions below

1
On BEST ANSWER

hi I can see by the code stacktrace that you were redeclaring the function multiple times with the same name. try to see the phtml files you stated and remove one of the function /var/www/production/htdocs/app/design/frontend/default/default/template/page/html/header.phtml:134) in /var/www/production/htdocs/app/design/frontend/default/default/template/page/html/header.phtml on line 161

It will solve your issue

0
On

This is because that file is being loaded multiple times. Both times it is creating the function. Your options are either to add the function to another file that is not being instantiated multiple times, or wrap the function in an if statement to check for the function's existence:

if( !function_exists('get_categories') ){ // If function doesn't exist, declare it...
    function get_categories($categories) {
        // The rest of the function...
    }
}