Magento 1.9 Error – Notice: Undefined Offset: 0

errormagento-1.9

on my admin panel appear :

Notice: Undefined offset: 0 in /var/www/main/app/code/local/Sm/Megamenu/Helper/Data.php on line 213

how to resolve?

public function getParentIdNode($id, $groupId, $nametable, $myLeft, $myRight='' ){
        $query = "
            SELECT * FROM {$nametable}
            WHERE (lft < '{$myLeft}' and rgt > '{$myRight}') AND group_id ='{$groupId}'
            ORDER BY lft DESC
            LIMIT 1
        ";
        // echo $query;die;
        try{
            $read= Mage::getSingleton('core/resource') ->getConnection('core_read');
            $data = $read->fetchAll($query);
            // Zend_Debug::dump($data);die;
            $item = new Varien_Object();
            $item->setData($data[0]);  <<====line 213
            // Zend_Debug::dump($item);die;
            return $item;
        }
        catch(Exception $e){
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            return;
        }

Best Answer

The array $data doesn't contain element with key 0. Try to use the following construction

try{
    $read= Mage::getSingleton('core/resource') ->getConnection('core_read');
    $data = $read->fetchAll($query);
    if(!count($data)){
        return new Varien_Object();
    }
    return new Varien_Object(reset($data));
}
catch(Exception $e){
    Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
    return;
}

The code above will check is array has elements and set the first $data array element to $item

Also you can disable strict error reporting to suppress warnings. http://php.net/manual/en/migrating5.errorrep.php

Related Topic