We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to pass a variable set in Controller to Select Helper “value”?

I have something like this in my Controller:

$this->view->userTagsComma = ($skills[0] == 'No Skills Set')?'':'"' . implode('", "', $skills) . '"';

And this generates something like "Cooking", "Swimming" when I echo this in View.

And in my View I have:

<?php echo Phalcon\Tag::select(array('user-skills-input', 
                                      TagsStandard::find("status = 1"), 
                                      "using" => array("name", "name"),
                                      "class" => "select-chosen",
                                      "type" => "text",
                                      "data-role" => "tagsinput",
                                      "value" => [],
                                      "multiple" => "multiple"
                                                        )); ?>

I want to pass $userTagsComma into [] in my select helper so how can I do it? I tried doing "view" => [$userTagsComma] in the select helper but it does not work.

Update: I tried putting in "value" => ["Cooking", "Swimming"], and this works. So not sure why passing in a variable with everything looking the same wouldn't work.

hi,

I think you are putting a string, and not an array.

What about "value" => explode(", ", $userTagsComma) ?

edited Jul '15

Hi,

Thanks for the help in advance. I tried that and it didn't work. When I echo explode(", ", $userTagsComma); it gives me Notice: Array to string conversion

hi,

I think you are putting a string, and not an array.

What about "value" => explode(", ", $userTagsComma) ?



16.0k
Accepted
answer
edited Jul '15

you can't echo an array. Use print_r($userTagsComma) to see the contents of an object/array. As Max said, by using implode you're passing a string instead of an array. How about passing $skills directly?

$this->view->userTagsComma = ($skills[0] == "No Skills Set") ? [] : $skills;

Wow thanks alot. Works perfectly.

you can't echo an array. Use print_r($userTagsComma) to see the contents of an object/array. As Max said, by using implode you're passing a string instead of an array. How about passing $skills directly?

$this->view->userTagsComma = ($skills[0] == "No Skills Set") ? [] : $skills;