XF 2.2 Select row with array()

mcatze

Well-known member
Hey everybody,

i have a template with a selectrow. With foreach i got the data from the array(). So far so good.
In the select row are all data from the array shown, but how can i get all data saved to the db?

I tried with name="data()" value="$data()" but it doesn't work.

I hope i describe my problem clear enough.
 

Attachments

  • Bildschirmfoto 2021-03-28 um 12.26.21.png
    Bildschirmfoto 2021-03-28 um 12.26.21.png
    70.1 KB · Views: 7
  • Bildschirmfoto 2021-03-28 um 12.26.56.png
    Bildschirmfoto 2021-03-28 um 12.26.56.png
    52.8 KB · Views: 7
Last edited:

Lukas W.

Well-known member
Unless you were passing an anonymous function in $leagues, $leagues() is not valid php syntax. () is a function call with zero arguments, and you can only call functions, hence the name.

Furthermore, your name attribute is the html input name, and thus follows normal conventions for that. name for a single value input and name[] for a multi selection.

Lastly, your value attribute on the selectrow must either be a single option value for single selects, or an array of option values for a multi select.
 

Lawrence

Well-known member
Hi, :) is this a multiple selection input row? If so, you need to add multiple="true" size="xxx" to the select row, and its name needs to be an array: name="leagues[]", and the value="{$leagues[]}. If it is not multiple selection then the name can just be "league", and the value "{$league}".

The value for your select row should not be the same variable used to generate the options, it should be the one where the members choice(s) were fetched from, ie: $profile.leagues, or $profile.league (depending).
 

mcatze

Well-known member
Okay, that makes sense. Actually i am at the coding-level "Try & Fail".. ;)

I got from the $viewParams an array called $leagues[] with the following values.
  • $league.league_id
  • $league.name
  • $league.code
  • $league.logo
  • $league.country_code

In the admin options i try to show a selectable entry labeled with the name and after choice and save, all values should be stored in the db.

What could be the finest/cleanest way to do this? When i understand correctly, the selectrow isn't it.
 

Lawrence

Well-known member
This is a single selection, then? All you need is to send the league_id to the controller. In the controller all the other data associated with that league_id will be selected when you verify the id is legitimate: ie:

PHP:
$leagueId = $this->filter('league_id', 'uint');
$league = $this->em()->find('YourAddonId:League', $leagueId);
if (!$league)
{
    throw $this->exception($this->notFound(\XF::phrase('your_phrase_prefix_requested_league_not_found')));
}
Now that you have all the data associated with the selected league_id, you can save it, :)

Edit: fixed the addonId portion.
 
Last edited:

mcatze

Well-known member
That could be tricky 'cause all data are served by an API. I will try it and maybe sometimes not fail. ;)

Thanks for that snippet.
 
Top