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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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