Yii Framework Forum: Radiolist shuffle methodology - Yii Framework Forum

Jump to content

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

Radiolist shuffle methodology

#1 User is offline   callmesid 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 26-November 16

Posted 13 January 2018 - 02:31 PM

Hello Experts,

I have a situation in a project, where four radiobutton appeared as a radiolist input. I want to shuffle the position of the correct value.

Can somebody help me how to do that??

Regards
0

#2 User is offline   Roberto Braga 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 209
  • Joined: 18-February 13
  • Location:Roma, Italy

Posted 16 January 2018 - 03:54 AM

Can you better explain what you want to do?
0

#3 User is offline   callmesid 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 26-November 16

Posted 16 January 2018 - 06:49 AM

View PostRoberto Braga, on 16 January 2018 - 03:54 AM, said:

Can you better explain what you want to do?


Hello Roberto,

Please see the below code, I have put in the view file.

<?= $form->field($model, 'user_selection')->radioList(['Option1'=>$opt1,'Option2'=>$opt2,'Option3'=>$opt3], ['onclick'=>'document.form-id.submit()'])->label(false); ?>


In this case page is get refreshed on submit. It is giving output as I wanted. Its working perfectly.

But every time the position of the options are fixed.

I want to shuffle the position of the options to be shuffled.

Like on first submit positions lets say "option1,option2,option3,option4. On next submit it will be like "option3,option2,option4, option1" etc.
Can you help me on this??

Regards
Sid
0

#4 User is offline   skworden 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 525
  • Joined: 14-June 12
  • Location:Virginia, USA

Posted 17 January 2018 - 12:16 AM

Something like this should help
<?php
function shuffle_arr($arr){
 $keys = array_keys($arr);
 shuffle($keys);
 foreach($keys as $key) {
  $shuffeled[$key] = $arr[$key];
 }
 return $shuffeled;
}

$form->field($model, 'user_selection')->radioList(shuffle_arr(['Option1'=>$opt1,'Option2'=>$opt2,'Option3'=>$opt3]), ['onclick'=>'document.form-id.submit()'])->label(false); ?>

0

#5 User is offline   Roberto Braga 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 209
  • Joined: 18-February 13
  • Location:Roma, Italy

Posted 17 January 2018 - 05:57 AM

View Postskworden, on 17 January 2018 - 12:16 AM, said:

Something like this should help...

no need to write shuffle function for array there is a native one in php
http://php.net/manua...ion.shuffle.php
0

#6 User is offline   skworden 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 525
  • Joined: 14-June 12
  • Location:Virginia, USA

Posted 17 January 2018 - 09:40 AM

View PostRoberto Braga, on 17 January 2018 - 05:57 AM, said:

no need to write shuffle function for array there is a native one in php
http://php.net/manua...ion.shuffle.php


shuffle doesn't keep the keys intact and you also can't pass the vars to it like 'opt'=>'1' (how yii needs it). I use shuffle in the function with array_keys to keep them intact. There could be a better way but that was just the first thing i thought to use. I'd put the function somewhere reusable so you don't have it on the view page.
0

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