$form->select example with array from db

Permalink
in the documentation it says

$form->select($name, $options, $value, $tagAttributes)

sample -
print $form->select('favoriteFruit', array('p' => 'Pears', 'a' => 'Apples', 'o' => 'Oranges'), 'a');

In this example, the select menu will be output, with the second option selected.


Okay I get this
It is the same as
<select name="favoriteFruit">
<option value="p">Pears</option>
<option value="a" selected>Apples</option>
<option value="o">Oranges</option>
</select>


but - I want to swap out the
array('p' => 'Pears', 'a' => 'Apples', 'o' => 'Oranges')

with an array from my db.
in my case it is a list of teams - of course I just need the id and team_name from my db Teams and replace where it says array() above

It's not clear to me where to put everything ( model, controller, or right in the single_page).
Any samples of course would be really be helpful. I have been searching through the forums for hours. Sorry if this has been answered before - I just can't seem to find it.

also the $tagAttributes is not quite clear in the docs with the only sample of which is array('style' => 'width: 100%'); can I add a class after that? i.e. array('style' => 'width: 100%', 'class' => 'MyClassName');

Thanks in advance.

INTcommunications
 
INTcommunications replied on at Permalink Reply
INTcommunications
still think is an easy question for someone who has been using Concrete 5 for a while.
Inspektor replied on at Permalink Reply
This is an answer to using a db table in the select helper on a single page that worked for me:

$fh = Loader::helper('form');
$db = Loader::db();
$id = 1;
$idarray = $db->GetAssoc("SELECT id,name FROM table ORDER BY id");
print $fh->Select("ids",$idarray,$id,"");

Now trying to find out how to make the select submit :-)
rockface replied on at Permalink Reply
rockface
GetAssoc() is no longer in concrete5 5.7 :-(
And fetchAssoc() only returns the last row of the query
Dose anyone have an idea how this can work in 5.7?
kloport replied on at Permalink Reply
kloport
For what it's worth, I have no idea about the way it is 'officially' done but there is always the plain ol' PHP approach.

$results = $db->GetAll('SELECT id,name FROM table');
$indexed_array = [];
foreach($results as $result)
    {
        $indexed_array[$result['id']] =  $result['name'];
    }
return $indexed_array;