Does anybody know how to select the contents of one take from a different view in CakePHP?
I have a take itemgroups
that has 2 fields ID
an
Here is the code to display a select dropdown.
<?php echo $form->input('inputname', array('type'=>'select', 'options'=>$cate, 'label'=>false, 'empty'=>'Category')); ?>
where $cate is loaded with an array from a find('list') in the format
array(0 => 'option1', 1=>'option2', etc etc etc
You can use like this in your controller and view...
//In Controller:
$data=$this->Model->find('list',array('conditions'=>array()));
$this->set('data',$data);
//In View:
echo $this->Form->select('field_name',$data,null,array("escape"=>false,"empty"=>"select"));
If you want to show initially select value in dropdown then you can pass value where use null in above line.
<?php
$arrCategory=array(1=>"Car",2=>"Boat",3=>"Bike");
echo $form->input('inputname', array('options'=>$arrCategory, 'label'=>false,
'empty'=>'Category','selected'=>'Your Value'));
?>
In controller:
$Itemgroup = $this->Itemgroup->find('list',
array(
'fields' => array('ID','Description')
)
);
$this->set('Itemgroup',$Itemgroup);
In View:
$form->input('itemgroup_id', array('type' => 'select','options'=> $Itemgroup));