skip to Main Content

In Kanboard, why do I get undefined index for this code whenever I click the button?

[php7:notice] PHP Notice:  Trying to access array offset on value of type null in /var/www/.../public_html/app/Helper/LayoutHelper.php on line 90
[php7:notice] PHP Notice:  Undefined index: project in /var/www/.../public_html/app/Helper/LayoutHelper.php on line 91
<?= $this->url->link(t('Delete'), 'ContactsItemsController', 'remove', array('item_id' => $item['id'], 'plugin' => 'AddressBook'), true, 'btn btn-red') ?>

I read that isset is a solution but if using isset is the solution, where do I place it? I can’t figure it out.

I tried:

<?= $this->url->link(t('Delete'), 'ContactsItemsController', 'remove', array('item_id' => isset($item['id']), 'plugin' => 'AddressBook'), true, 'btn btn-red') ?>

2

Answers


  1. Chosen as BEST ANSWER

    The explanation was great and an answer for isset for other issues I was having so thanks for explaining that.

    The fix for this particular problem was because I was using the project layout helper instead of the config one in the controller.

    /**
         * Contacts items config page
         *
         * @access public
         */
        public function config()
        {
            $this->response->html($this->helper->layout->config('addressBook:config/index', array(
                'title' => t('Address Book Settings'),
                'items' => $this->contactsItemsModel->getAllItems(),
            )));
        }
    

  2. To use isset you basically wrap around section where you use that array (or even simple variable), like:

    <?php
    if(isset($array['key'])){
        //... section where we can safely use the value of $array['key'] ...
        echo $this->url->link(...);
    }
    

    But you use the <?= which is shorthand for <?php echo and echo command must have an argument, even if its empty string or null, so you have to pass some value there, which limits your options how to wrap the section with isset.

    You could use ternary operator, like:

    <?= isset($array['key']) ? /*... safe use of $array['key'] ...*/ $this->url->link(...) : '' ?>
    

    Or anonymous function executed right away, like:

    <?= (function(){ if(isset($array['key'])){ /*... safe use of $array['key'] ...*/ return $this->url->link(...); } })() ?>
    

    Note that when you do not explicitly call return command, php functions implicitly return null. Also you should use this method for complex cases which you cannot solve by ternary operator, because of the readability, there could be multiple branches which are harder to get when you or someone else return to the code after while.

    In case you care only about single value and not entire function call or even block, there is shorthand in form of null coalescing operator, like:

    array('item_id' => $item['id'] ?? 0)
    

    Which kinda does the isset check internally.

    It looks way better then ternary operator, don’t you think ?

    array('item_id' => isset($item['id']) ? $item['id'] : 0)
    

    To the warnings you posted: Trying to access array offset on value of type null, this means that the variable $item is set to null, therefore it is not an array. Code like this produce the same warning:

    $item = null;
    $item['id'];
    

    Or the variable $item could be undefined, in which case there would be another warning undefined variable $item.

    Most likely you are just setting the $item variable from database, but you are missing the check after that if you actually get the record.

    Second warning is irrelevant to the code you posted Undefined index: project, this would happen in case like this:

    $var = [];
    $var['project'];
    

    So you have variable which is an array, but there is no index/key named as project present in that array.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search