Yii Framework Forum: How To Get Checkboxlist On Action Update? - Yii Framework Forum

Jump to content

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

How To Get Checkboxlist On Action Update? Rate Topic: -----

#1 User is offline   Quentinb 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 16-October 13

Posted 19 March 2014 - 04:58 AM

Hi all.

I have the following CheckBoxList.

		<?php
		echo $form->checkBoxList($model,'sessionsids',ConferenceProgram::model()->getConfListData(),
			array(
				'template'=>'{input}{label}',
				'separator'=>'<br>',
				'labelOptions'=>array(
				'style'=> '
				width: 690px;
				float: right;
				'),
			) );
		?>


I got the example for somewhere on the net. As you can see I hook it to my current view model called $model, but the actual list comes from ConferenceProgram::model()->getConfListData()

The list works and my checkboxes are shown, but on submit of the form, it is not getting any data back. I have tried:
$model->attributes=$_POST['ConfConferenceAttendee'];

//Both lines below return nothing
$sessionsarr=$model->sessionsids;
$sessionsarr=$_POST['ConferenceAttendee']['sessionsids'];


And a few other things, but I just don't get the selected checkbox values back to the controller.

Thanks for any help..
0

#2 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,327
  • Joined: 21-February 11
  • Location:Greece

Posted 19 March 2014 - 05:17 AM

Hi

did you set the sessionsids in rules of the model (and as a specific variable) ?

Also trace your code using var_dump($_POST). What do you see ?
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#3 User is offline   Quentinb 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 16-October 13

Posted 19 March 2014 - 05:32 AM

This is what I have created in ConferenceAttendee model:
public $sessionsids = array();


public function rules()
{
	return array(
		array('sessionsids', 'required','message'=>'You must select at least 1 Conference Session.'),
	);
}


public function getSessionsIds()//Custom field
{
	return $this->sessionsids;
}
	
public function setSessionsIds($sessionsids)//Custom field
{
	$this->sessionsids = $sessionsids;
}





This is the var_dump output:
array (size=2)
  'ConfConferenceAttendee' => 
    array (size=10)
      'person_firstname' => string 'Katrin' (length=6)
      'person_lastname' => string 'ouioiu' (length=6)
      'person_contact_email' => string 'katrin@uoiuio.com' (length=17)
      'person_contact_number' => string '8889996235' (length=10)
      'address_line_1' => string 'adr1' (length=4)
      'address_line_2' => string 'adr2' (length=4)
      'address_line_3' => string 'adr3' (length=4)
      'address_line_4' => string 'adr4' (length=4)
      'conf_diet_type_id' => string '2' (length=1)
      'sessionsids' => 
        array (size=3)
          0 => string '1' (length=1)
          1 => string '2' (length=1)
          2 => string '3' (length=1)
  'yt0' => string 'Save' (length=4)

0

#4 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,327
  • Joined: 21-February 11
  • Location:Greece

Posted 19 March 2014 - 05:57 AM

View PostQuentinb, on 19 March 2014 - 05:32 AM, said:

This is what I have created in ConferenceAttendee model:
public $sessionsids = array();


public function rules()
{
	return array(
		array('sessionsids', 'required','message'=>'You must select at least 1 Conference Session.'),
	);
}


public function getSessionsIds()//Custom field
{
	return $this->sessionsids;
}
	
public function setSessionsIds($sessionsids)//Custom field
{
	$this->sessionsids = $sessionsids;
}





This is the var_dump output:
array (size=2)
  'ConfConferenceAttendee' => 
    array (size=10)
      'person_firstname' => string 'Katrin' (length=6)
      'person_lastname' => string 'ouioiu' (length=6)
      'person_contact_email' => string 'katrin@uoiuio.com' (length=17)
      'person_contact_number' => string '8889996235' (length=10)
      'address_line_1' => string 'adr1' (length=4)
      'address_line_2' => string 'adr2' (length=4)
      'address_line_3' => string 'adr3' (length=4)
      'address_line_4' => string 'adr4' (length=4)
      'conf_diet_type_id' => string '2' (length=1)
      'sessionsids' => 
        array (size=3)
          0 => string '1' (length=1)
          1 => string '2' (length=1)
          2 => string '3' (length=1)
  'yt0' => string 'Save' (length=4)




So after of that
$model->attributes=$_POST['ConfConferenceAttendee'];
var_dump ($model->sessionsids) should display all selected items. right ?
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#5 User is offline   Quentinb 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 16-October 13

Posted 19 March 2014 - 06:13 AM

Yip, after doing:

var_dump ($model->sessionsids);
die;


I get:

array (size=2)
  0 => string '1' (length=1)
  1 => string '2' (length=1)


This is 100% correct, but how to access them so I can write them to the database?
0

#6 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,327
  • Joined: 21-February 11
  • Location:Greece

Posted 19 March 2014 - 06:16 AM

View PostQuentinb, on 19 March 2014 - 06:13 AM, said:

Yip, after doing:

var_dump ($model->sessionsids);
die;


I get:

array (size=2)
  0 => string '1' (length=1)
  1 => string '2' (length=1)


This is 100% correct, but how to access them so I can write them to the database?



You should override the beforeSave or afterSave model method and in foreach iterator save the another models with the specific sessionsid
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#7 User is offline   Quentinb 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 16-October 13

Posted 19 March 2014 - 07:12 AM

Thanks, but as I have mentioned, I cannot seem to get the ids back to my action using any of the below methods to access them. I can see via the DUMP that they are getting passed, but how do I get them stored in a variable so I can use them? Below is what I have tried:

None of these work.
$sessionsarr=$model->sessionsids; //sessionsids is empty
$sessionsarr=$_POST['ConferenceAttendee']['sessionsids']; //sessionsids is empty
$sessionsarr=$_POST['sessionsids']; //Compilation error



This is what I use to iterate through them:
for($i=0; $i<sizeof($sessionsarr); $i++)
{
	$sessStore = new ConfAttendeeLinkProgram; //Model
	$sessStore->attendee_id=$id; //$id is the primary key of selected row to update
	$sessStore->conference_program_id=$sessionsarr[$i];
	$sessStore->save();
}

0

#8 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,327
  • Joined: 21-February 11
  • Location:Greece

Posted 19 March 2014 - 07:26 AM

View PostQuentinb, on 19 March 2014 - 07:12 AM, said:

Thanks, but as I have mentioned, I cannot seem to get the ids back to my action using any of the below methods to access them. I can see via the DUMP that they are getting passed, but how do I get them stored in a variable so I can use them? Below is what I have tried:

None of these work.
$sessionsarr=$model->sessionsids; //sessionsids is empty
$sessionsarr=$_POST['ConferenceAttendee']['sessionsids']; //sessionsids is empty
$sessionsarr=$_POST['sessionsids']; //Compilation error



This is what I use to iterate through them:
for($i=0; $i<sizeof($sessionsarr); $i++)
{
	$sessStore = new ConfAttendeeLinkProgram; //Model
	$sessStore->attendee_id=$id; //$id is the primary key of selected row to update
	$sessStore->conference_program_id=$sessionsarr[$i];
	$sessStore->save();
}



You said that the var_dump $_POST['ConfConferenceAttendee']['sessionsids'] has values so,

$sessionsarr = $model->sessionsids; //if not works then something not works in massive assigment on
$model->attributes = $_POST['ConfConferenceAttendee']; (please inform us)

$sessionsarr=$_POST['ConferenceAttendee']['sessionsids']; //has values ?
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
1

#9 User is offline   Quentinb 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 16-October 13

Posted 19 March 2014 - 07:44 AM

Thank you very much, by adding more DUMPS I could trace it down to the loop and followed your instruction to use a foreach loop instead.

To finish and close this topic, this is the final piece of code:

if(isset($_POST['ConfConferenceAttendee']))
{
	$model->attributes=$_POST['ConfConferenceAttendee'];
	$sessionsarr = $model->sessionsids;
		
		if($model->save())
		{
			foreach($sessionsarr as $sessid)
			{
				$sessStore = new ConfAttendeeLinkProgram;
				$sessStore->attendee_id=$id;
				$sessStore->conference_program_id=$sessid;
				$sessStore->save();
			}

0

#10 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,327
  • Joined: 21-February 11
  • Location:Greece

Posted 19 March 2014 - 08:14 AM

View PostQuentinb, on 19 March 2014 - 07:44 AM, said:

Thank you very much, by adding more DUMPS I could trace it down to the loop and followed your instruction to use a foreach loop instead.

To finish and close this topic, this is the final piece of code:

if(isset($_POST['ConfConferenceAttendee']))
{
	$model->attributes=$_POST['ConfConferenceAttendee'];
	$sessionsarr = $model->sessionsids;
		
		if($model->save())
		{
			foreach($sessionsarr as $sessid)
			{
				$sessStore = new ConfAttendeeLinkProgram;
				$sessStore->attendee_id=$id;
				$sessStore->conference_program_id=$sessid;
				$sessStore->save();
			}



1) So, what was the problem ? Other Yii members maybe want to know about it.
2) Also your code after of successful save you could use overriding afterSave method of your model for reusable reason
3) Thanks for your voting :)
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#11 User is offline   Quentinb 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 16-October 13

Posted 19 March 2014 - 08:22 AM

It seems like it did not like the for loop, or the for loop did not understand the $sessionarr, or maybe $sessionsarr was/is not passed as an array?

This worked:
$sessionsarr = $model->sessionsids;
foreach($sessionsarr as $sessid)
{
	$sessStore = new ConfAttendeeLinkProgram;
	$sessStore->attendee_id=$id;
	$sessStore->conference_program_id=$sessid;
	$sessStore->save();
}



This did NOT work:
$sessionsarr = $model->sessionsids;
for($i=0; $i<sizeof($sessionsarr); $i++)
{
        $sessStore = new ConfAttendeeLinkProgram;
        $sessStore->attendee_id=$id;
        $sessStore->conference_program_id=$sessionsarr[$i];
        $sessStore->save();
}

0

#12 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,327
  • Joined: 21-February 11
  • Location:Greece

Posted 19 March 2014 - 09:20 AM

View PostQuentinb, on 19 March 2014 - 08:22 AM, said:

It seems like it did not like the for loop, or the for loop did not understand the $sessionarr, or maybe $sessionsarr was/is not passed as an array?

This worked:
$sessionsarr = $model->sessionsids;
foreach($sessionsarr as $sessid)
{
	$sessStore = new ConfAttendeeLinkProgram;
	$sessStore->attendee_id=$id;
	$sessStore->conference_program_id=$sessid;
	$sessStore->save();
}



This did NOT work:
$sessionsarr = $model->sessionsids;
for($i=0; $i<sizeof($sessionsarr); $i++)
{
        $sessStore = new ConfAttendeeLinkProgram;
        $sessStore->attendee_id=$id;
        $sessStore->conference_program_id=$sessionsarr[$i];
        $sessStore->save();
}



It should work!

$sessionsarr is an array with integer index (0=>'1',1=>'3',...)
check with var_dump ($sessionsarr); 'foreach' and 'for' should work same loops, add an echo ($i); what happens ?
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
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