How to set default value in dropdown - yii

i have a dropdown here in yii that looks like this:
<?php echo $form->dropDownList($model, 'is_enabled',
array('0'=>'No', '1'=>'Yes'), array('id'=>'new-sys-user-is_enabled',));?>
the problem with this is that it shows the 'No' because it has the 0 key. I would like to show the 'Yes' by default. What is the work-around for this?

Check out if this works for you: http://www.yiiframework.com/forum/index.php/topic/11748-dropdownlist-selected-option/
EDIT: Copying the relevant part:
$form->dropDownList($model,'sex',array('1'=>'men','2'=>'women'), array('options' => array('2'=>array('selected'=>true))));

I think you should try like this ...
<?php echo $form->dropDownList($model, 'is_enabled', array('1'=>'Yes', '0'=>'No'), array('id'=>'new-sys-user-is_enabled',));?>
The value you want to keep default value, then keep first in array too.
Thanks

Related

Set title Cakephp 2.x

I am trying to change the title of a page.
The default.ctp view has the following code:
<title>
<?php echo $this->fetch('title'); ?>
- Welcome
</title>
I'm trying to use the following code in the controller of the page:
$title = 'Overview';
$this->set('title');
But unfortunately I do not see see 'Overview - Welcome', but only the name of the function of the controller followed by ' - Welcome'.
Can anyone help me to find the problem why it is not working?
Don't know if $this->set('title') will work
I usually use the 'compact' function to set the variables as they're named.
Like this: $this->set(compact('title')); or just simply this $this->set('title', $title);
You can define what $this->fetch('title') returns using View::assign() function that sets block's value this way:
$this->assign('title', $title);
See more in the documentation about view blocks.
I think instead of:
$this->set('title');
you must use:
$this->set('title', $title);

yii cgridview tooltip php variable

This is my one of the Cgridview Column
array( 'name'=>'furnished',
'type'=>'raw',
'value'=>'$data->furnished=="FF" ? CHtml::link(CHtml::encode($data->furnished),
"tooltip",["title"=>"**Here I Want to show <?php echo $data->anystring ?>**", "data-toggle"=>"tooltip",
"style"=>"text-decoration: none; cursor:pointer;"]) : $data->furnished',
),
How can I achieve this . Please help
normally typed data is displayed here. but how to show PHP variable
Thanks In advance
Everything in value will be executed via PHP's eval() function. So you must change your code to this:
'value'=>'$data->furnished=="FF" ? CHtml::link(CHtml::encode($data->furnished),
"tooltip",["title"=>"**Here I Want to show ".$data->anystring."**", "data-toggle"=>"tooltip",
"style"=>"text-decoration: none; cursor:pointer;"]) : $data->furnished'
Actually, you don't need <?php and all you need is just ..

Creating dropdown list from related table

I have a two tables:
tbl_day:id_day,mon,tues,wed,thurs,fri,sat,sun
tbl_shft:shft_id,start,end,name,status
I want to have a dropdown table in tbl_day where mon is dependent in the tbl_shft name.
I come up with a dropdown that is displaying data from shift. what I did is.
<?php echo $form->labelEx($model,'sun'); ?>
<?php echo CHtml::activeDropDownList($model,'sun',$model -> getCategories(),array('prompt'=>'Select a Shift'))
?>
and for my model
public function getCategories(){
//this function returns the list of categories to use in a dropdown
return CHtml::listData(Shift::model()->findAll(), 'shft_id', 'name');}
My problem is that it is not submitting. I dont know where my problem here is. Im a beginner in Yii. Hope someone helps. Thanks in advance.
In here your code will work. I think the submitted output was not handled correctly. #sam dark says correctly. Edit your question .just place the controller action of your process . Else you just place this code on your controller action. And give the result of controller on this question itself.
Public function actionYouraction(){
......
if(isset($_POST))
{
echo "<pre>";
print_r($_POST);
exit(); } }

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