1

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;
        }
1
  • If this query has one data then $item->setData($data); it doesnot have[0] array Commented Sep 13, 2016 at 12:34

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.