0

First of all, I'm really disappointed after all googling and getting nothing!

I have an Arraydataprovider called

$data = [
    400 => [
        'name' => 'x',
        'lesson_1' => '10',
        'lesson_2' => '9',
        ...
    ],
    389 => ...
]

It's generated in a for loop.

I want the values to be shown in a text box :

foreach($lessons as $lid => $name) {
        $attrs[] = [
                'attribute' => 'lesson_' . $lid,
                'label' => $name['name'], 
                'format' => 'raw',
                'value' => function($model, $key, $index) use($lid, &$data) {
                        return '<input class="txt" data-lid="'.$lid.'" type="text" value="'.$data[$key]['lesson_'.$lid].'"/>';
                }

        ];
}

It gives me error : Undefined index: lesson_49 But I'm sure that $data provider, has the key lesson_49 (proved by var_dump);

what is the problem? :-(

2
  • maybe try to use var_dump($data) in your value function when !isset($data[$key]['lesson_'.$lid])? Commented Feb 3, 2016 at 12:30
  • @PatrykRadziszewski oh I thought it's a great problem !!! it worked thanks. please reply to the question and let me choose it as answer Commented Feb 3, 2016 at 12:41

2 Answers 2

2

You should check for value is empty or not using isset() or empty():

Use isset() inside value function to check for empty value.

For example,

(isset($data[$key]['lesson_'.$lid]) ? $data[$key]['lesson_'.$lid] : '-';

You can also use var_dump($data).

Sign up to request clarification or add additional context in comments.

Comments

1

Try to use var_dump($data) in your value function when !isset($data[$key]['lesson_'.$lid]) and check what's wrong with your data

2 Comments

I should say the better answer is to use (isset($x) ? $x : '-')
@pooria sure, I agree that's it's how it should look in the end... But in certain circumstances we'd like to know what exactly is wrong, especially while developing.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.