Yii, set radio button value fetch from database - yii

In Yii, I listed my table which is fetch from database in grid view.
'value'=> 'CHtml::radioButton("set_default",false,array(
"value"=>"$data->id",
"set"=>"1",
"disable"=>"disable",
"uncheckValue"=>null
))',
the above code I entered in normal radio button view, how do I fetch from database? Anybody help me?

CGridView lets you write any value inside row's column instead of normal database values. For modify column values, you need to add a function for value attribute. Inside of this function, you can access each data's attributes. In your case, You need to echo a radio button like this:
//$data refres to each data row in the CGridView
'value' => function ($data, $row) {
echo
CHtml::radioButton("set_default",false,array(
"value"=>$data->id,
"set"=>"1",
"disable"=>"disable",
"uncheckValue"=>null
)),
},

Related

How i dont display empty and null rows in yii

How I do not display empty and null rows in yiiframework with listview.
Many rows in database are null.When I want to show data in view.php I see many null rows.I see a data on the other page.
can I see just rows with data.
The CListView widget uses a template to render each row of data.
In the skeleton generated by Gii, the index.php file has a CListView widget that uses the _view.php file to display row data.
You only have to check if a field is null and don't show it.
If you are aking for the autogenerated view.php file, the default is CDetailView
At this place, you can use the "visible" element of the attribute item:
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
array(
'name'=>'field1',
'visible'=>$model->field1!=null,
),
..............
See attributes property of CDetailView and CListView
in your model, put a condition to filter null/empty values before rendering the result to your view.
public function search() {
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('table_column', $this->tableColumn, false );
$criteria->condition .= "another_table_column IS NOT NULL"; //this will filter null values
Use the afterFind() method in your Model to change the value displayed in the view so that a null value is shown as "NA" or some other value like that.

Yii populate a dropdown on select of another

I have a dropdown that I want to populate when an item in another dropdown is selected. Both the dropdown are tied to data/model passed on from controller. and the first dropdown is populated from DB by calling a function in the model. Heres' the form,
echo $form->dropDownListRow($modelunit,
'superunit',
$model->getSunits(),
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('user/getunits'),
'update'=>'#unit_id',
)));
echo CHtml::dropDownList('unit_id','', array());
Here's the action user/getunits called by Ajax.
$data=Unit::model()->findAll('sid=:sid',
array(':sid'=>(int) $_POST['superunit']));
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
I keep getting an error "Undefined index: superunit" when first dropdown is selected. Also, you may notice I am using form->dropDownListRow for the first dropdown while using CHtml::dropDownList for the second. That's cause I am clueless on the syntax of how exactly to make sure the dropdown is populated correctly with ajax and at also properly bind to the model.
You use $form->dropDownListRow that's why you will get $_POST['MyModelName']['superunit'] on your server side
Change you code like
$data=Unit::model()->findAll('sid=:sid',
array(':sid'=>(int) $_POST['MyModelName']['superunit']));
Where MyModelName is a model that you use)
Or like
echo CHtml::dropDownList('superunit'.....
For others - this wiki may help.

Yii activeRadioButton does not get activerecord's real value

What might be the problem? I have a activerecord in $Row. The model has a property 'id'. Then I want to show radio buttons on interface like this:
echo CHtml::activeRadioButton($Row, 'id');
and when rendered on the browser, I can see that the radio button's value is '1'. But when I do like this:
echo $Row['id'];
that shows the correct value (128).
What might be the problem?
The 'id' argument specifies what property the radio button will write to when the form is submitted. However, you need to also specify what the radio button's value will be when selected. Do this through the function's third parameter:
echo CHtml::activeRadioButton($Row, 'id', array('value' => $Row['id']));

Updating the dropdown list field value which is created using drop down list using create

i got a problem while updating a record for a model in yii. i have 2 models. im working on one model now. I'm creating a record for one model using create controller. in the form i've 1 fields which is the name field(im getting this name from other model(table). im getting the names from this second table and showing them in dropdown list. and storing them.
when it comes to update its again coming up with the same dropdown what i've shown using the _form.php for creating a record. can anyone pls tell me how can i show the name instead of dropdown list again??
thanks in advance.
Add a simple check in your view to see if the value has already been added or not. Something like this:
<?php if ($model->attribute && $model->attribute != ''): ?>
// Code to display a normal textfield here
<?php else: ?>
// Code to display dropdown
<?php endif; ?>

Default select one item in the dropDownList Yii

I have a dynamic dropdown, data coming from the database.
<?php $sel_id = $selected_id_array[0]->UPR_RelationType;?>
My dropdowm looks like this
<?php echo CHtml::dropDownList('RelationType_'.$pat_id[0]->PAT_ID,'U2U_RelationType',CHtml::listData(MasterTypeItems::model()->findAllByAttributes(array('MSTT_MST_ID'=>$relationship_type_array[0]->MST_ID),array('order' => 'MSTT_Name')), 'MSTT_ID', 'MSTT_Name'),array('id'=>'select','class'=>'relation_type','style'=>'width:50px'));
In this dropdown i have to select defaultly $sel_id;
for example am getting $sel_id=5; In the drop down i have to select 5th option as selected in yii. please give me any suggestion what i have to write in dropDown to select $sel_id;
If I understand your question correctly, what you want to achieve is a dropdownlist which already has a pre-selected option. If so, then this piece of code should work.
echo dropDownList(string $name, string $select, array $data, array $htmlOptions=array ( ))
where $select would be your default selected item.
More information can be found here: http://www.yiiframework.com/doc/api/1.1/CHtml#dropDownList-detail