How To Filter & Set Dropdown Values From Array?

Hi

My dropdown is working but it is displaying all of the attributes returned in the array.

I would like to only set the [id] as the dropdown value & display [player_surname] in the dropdown list but I am unsure how to only get and pass these attributes.

Any help would be most appreciated

Model




// Return a player array for primary use in dropdowns

public function getPlayerlist()

{

$id=(Yii::app()->user->venue_user_id); 		// set the ID 


// Return all the players associated with the users venue

$sqlPlayers='SELECT * FROM player LEFT JOIN player_venues ON player.id = player_venues.player_id WHERE player_venues.venue_id=:id AND player_venues.active = "yes"';

$result = Yii::app()->db->createCommand($sqlPlayers)->bindValue('id',$id)->queryAll();


$playerList = array();			


foreach($result as $row)		

	{

		$playerList[ ] = $row;

  	}

return $playerList;				// return the array values into the dropdown


}



Returned Array Values From Model




Array

(

    [0] => Array

        (

            [id] => 1

            [player_salutation] => Mr.

            [player_forename] => Martin

            [player_surname] => Blackshaw

        )


    [1] => Array

        (

            [id] => 2

            [player_salutation] => Mr.

            [player_forename] => David

            [player_surname] => Ward

        )


    [2] => Array

        (

            [id] => 14

            [player_salutation] => Mr.

            [player_forename] => Simon

            [player_surname] => Walker

        )


    [3] => Array

        (

            [id] => 15

            [player_salutation] => Mrs.

            [player_forename] => Susan

            [player_surname] => Potts

        )


)



Dropdown In The View File




<?php

echo $form->dropDownList($model, 'player_total_selector', $model->getPlayerlist(),

	array(

		'class'=>'styled-select',

		'prompt'=>'Select Your Player',

		));

?>



Many thanks

GPM

Looks you are looking for active player. Because your function getPlayerlist is return an array, not active records, so I hope you can apply below code, of course you should save your code before you do the changes.

in the player view file:




echo $form->dropDownList($model, 'player_total_selector', 

CHtml::listData($model->findAllBySql('SELECT * FROM player LEFT JOIN

 player_venues ON player.id = player_venues.player_id WHERE 

player_venues.venue_id=:id AND player_venues.active = "yes"'), 

'id', 'player_surname'),

        array(

                'class'=>'styled-select',

                'prompt'=>'Select Your Player',

                ));



This code will create all your player list as the options depends on the SQL query, then put them in your drop down list as a pair of (id, player_surname).

Good luck.

I mean replace below code in your view file by mine. Make sure it’s in player view file, so the $model is Player::model(). If it’s not in player view file, you can replace $model->findAllBySql by Player::model()->findAllBySql.




echo $form->dropDownList($model, 'player_total_selector', $model->getPlayerlist(),

        array(

                'class'=>'styled-select',

                'prompt'=>'Select Your Player',

                ));