Yii Framework Forum: How To Filter & Set Dropdown Values From Array? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Filter & Set Dropdown Values From Array? Rate Topic: -----

#1 User is offline   GPM 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 27-August 13

Posted 17 January 2014 - 03:22 PM

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
0

#2 User is offline   Johnny Gan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 197
  • Joined: 30-May 13

Posted 17 January 2014 - 05:29 PM

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.
1

#3 User is offline   Johnny Gan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 197
  • Joined: 30-May 13

Posted 17 January 2014 - 05:35 PM

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',
                ));

1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users