To Create a List Box :
<?php echo CHtml::listBox('stat','stat', array(
'pending'=>'pending',' completed'=>'completed','other dept'=>'other dept','none'=>'none'),
array('size'=>'5', 'multiple'=>'true', 'style'=>'width:333px', 'options'=>array ('pending'=> array('selected'=> 'selected'))));
?>
Above is the CHtml listbox which is not included in the form to access this in controller we can use the $_POST method the above control has name as 'stat'. So $_POST['stat'] in controller access this listbox.
Syntax is <?php CHtml::listBox('name','name',$ data,$htmlOptions); ?>
here the htmlOptions are :
$htmlOptions = array('size'=>'5', 'multiple'=>'true', 'style'=>'width:333px', 'options'=>array ('pending'=> array('selected'=> 'selected'));
by writing the above line we can modify our control declaration as
<?php echo CHtml::listBox('stat','stat', array(
'pending'=>'pending',' completed'=>'completed','other dept'=>'other dept','none'=>'none'),
$htmlOptions));
?>
here the above example is a static declaration of listBox if we want to get from DB use the following method
<?php
$models = Raisedby::model()->findAll( array('order'=>'t.desig ASC'));
$rb='';
$list = CHtml::listData($models,' desig','desig');
echo CHtml::listBox('rb','rb',$ list,
array('size'=>'5','multiple'=> 'true','style'=>'width:333px')
);
?> Use Ctrl Key to select more than one
Here Raisedby is the model name and desig is the column name. in this contest we are creating a listData and assigning the list to the listBox.
All about the Controller :
I am taking the second example i.e., using DB
We have to save the selected items from listbox in the string field with comma separated values.
for this we use implode method
public function actionCreate()
{
$model=new Ddrc;
if(isset($_POST['Ddrc']))
{
$model->attributes=$_POST[' Ddrc'];
foreach($_POST['rb'] as $value)
$model->raised_by = implode(",",$_POST['rb']); // here rb is the control name
/* we are storing in a single string value format for all the selected list items with comma separated */
if($model->save())
$this->redirect(array('view',' id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
The method is same for the static list also we need to change the name of control that's enough
Where as Update action is not at the same we have to define the control in view differently and we need to write the code in controller also:
View for update action (remember the above example is for create action for update we need to write the below code) :
<?php
// First we need to get the listitems from model
$models = Raisedby::model()->findAll( array('order'=>'t.desig ASC'));
$rb='';
// Prepare the listdata for the selected column in the model (model : Raisedby , column : desig)
$data = CHtml::listData($models,' desig','desig');
$raise = explode(",",$model->raised_by) ;
/* explode makes as substrings for the seperated character ex : explode(".","Saik.Ahmed") gives array of two values with array[1] = "Saik" and array[2]="Ahmed" */
foreach($raise as $r) // reading each value in the array
{
$rr[$r] = array('selected'=>'selected'); // marking the listitems as selected if the value presented in
// the raised_by column of the model
}
/* Displaying the List */
echo CHtml::listBox('rb','rb',$ data,
array('size'=>'5','multiple'= >'true','style'=>'width:333px' ,'options'=>$rr));
?>
So the control shows the selected items along with complete list, now if user modified the selection the new selection has to be updated in controller so the update action in controller is defined as :
public function actionUpdate($id)
{
$model=$this->loadModel($id);
if(isset($_POST['Ddrc']))
{
$model->attributes=$_POST[' Ddrc']; // Same as create action we are updating the new selected
// values using the method implode.
foreach($_POST['rb'] as $value)
$model->raised_by = implode(",",$_POST['rb']);
if($model->save())
$this->redirect(array('view',' id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
<?php echo CHtml::listBox('stat','stat',
'pending'=>'pending','
array('size'=>'5', 'multiple'=>'true', 'style'=>'width:333px', 'options'=>array ('pending'=> array('selected'=> 'selected'))));
?>
Above is the CHtml listbox which is not included in the form to access this in controller we can use the $_POST method the above control has name as 'stat'. So $_POST['stat'] in controller access this listbox.
Syntax is <?php CHtml::listBox('name','name',$
here the htmlOptions are :
$htmlOptions = array('size'=>'5', 'multiple'=>'true', 'style'=>'width:333px', 'options'=>array ('pending'=> array('selected'=> 'selected'));
by writing the above line we can modify our control declaration as
<?php echo CHtml::listBox('stat','stat',
'pending'=>'pending','
$htmlOptions));
?>
here the above example is a static declaration of listBox if we want to get from DB use the following method
<?php
$models = Raisedby::model()->findAll(
$rb='';
$list = CHtml::listData($models,'
echo CHtml::listBox('rb','rb',$
array('size'=>'5','multiple'=>
);
?> Use Ctrl Key to select more than one
Here Raisedby is the model name and desig is the column name. in this contest we are creating a listData and assigning the list to the listBox.
All about the Controller :
I am taking the second example i.e., using DB
We have to save the selected items from listbox in the string field with comma separated values.
for this we use implode method
public function actionCreate()
{
$model=new Ddrc;
if(isset($_POST['Ddrc']))
{
$model->attributes=$_POST['
foreach($_POST['rb'] as $value)
$model->raised_by = implode(",",$_POST['rb']); // here rb is the control name
/* we are storing in a single string value format for all the selected list items with comma separated */
if($model->save())
$this->redirect(array('view','
}
$this->render('create',array(
'model'=>$model,
));
}
The method is same for the static list also we need to change the name of control that's enough
Where as Update action is not at the same we have to define the control in view differently and we need to write the code in controller also:
View for update action (remember the above example is for create action for update we need to write the below code) :
<?php
// First we need to get the listitems from model
$models = Raisedby::model()->findAll(
$rb='';
// Prepare the listdata for the selected column in the model (model : Raisedby , column : desig)
$data = CHtml::listData($models,'
$raise = explode(",",$model->raised_by)
/* explode makes as substrings for the seperated character ex : explode(".","Saik.Ahmed") gives array of two values with array[1] = "Saik" and array[2]="Ahmed" */
foreach($raise as $r) // reading each value in the array
{
$rr[$r] = array('selected'=>'selected');
}
/* Displaying the List */
echo CHtml::listBox('rb','rb',$
array('size'=>'5','multiple'=
?>
So the control shows the selected items along with complete list, now if user modified the selection the new selection has to be updated in controller so the update action in controller is defined as :
public function actionUpdate($id)
{
$model=$this->loadModel($id);
if(isset($_POST['Ddrc']))
{
$model->attributes=$_POST['
foreach($_POST['rb'] as $value)
$model->raised_by = implode(",",$_POST['rb']);
if($model->save())
$this->redirect(array('view','
}
$this->render('update',array(
'model'=>$model,
));
}
No comments:
Post a Comment