[ACCEPTED]-Dropdown select list in CakePHP-cakephp
Assuming your model is User and the field 3 you want to use is a list of US states (for 2 example)...
In your controller:
$this->set('states',$this->State->find('list'));
and in your 1 view:
<?php echo $form->input('User.state',array('type'=>'select','options'=>$states)); ?>
Here is the code to display a select dropdown.
<?php echo $form->input('inputname', array('type'=>'select', 'options'=>$cate, 'label'=>false, 'empty'=>'Category')); ?>
where 3 $cate is loaded with an array from a find('list') in 2 the format
array(0 => 'option1', 1=>'option2', etc 1 etc etc
<?php
$arrCategory=array(1=>"Car",2=>"Boat",3=>"Bike");
echo $form->input('inputname', array('options'=>$arrCategory, 'label'=>false,
'empty'=>'Category','selected'=>'Your Value'));
?>
0
please use
//In controller
$data=$this->Model->find('list');
$this->set('data',$data);
In View :
$this->Form->input('list',array("options"=>$data));
0
You can use like this in your controller 3 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 2 select value in dropdown then you can pass 1 value where use null in above line.
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));
0
If it's something like a "US States" dropdown 4 list that is going to be repeated from page 3 to page, consider using an Element, which you can 2 just pass data to and you won't have to 1 repeat all the UI code again.
This is the right solution in my opinion:
of 1 choices from column SET
or ENUM
type
*
* @param string $sColumn - col name
* @param string $sTable - if use other table as Model
* @return array
*/
function fGetArrayFromSQLSet($sColumn, $sTable=null) {
# Pokud neni urcena tabulka, pouzij tabulku modelu
if( !$sTable ) {
$sTable= $this->useTable;
}
# Nacti nastaveni daneho pole dane tabulky
$tmpHlaseno=$this->query("SELECT COLUMN_TYPE
FROM information_schema.columns
WHERE TABLE_NAME = '$sTable'
AND COLUMN_NAME = '$sColumn'
"); //db($tmpHlaseno);
$tmpList= $tmpHlaseno[0]['columns']['COLUMN_TYPE'];
# Ziskej hodnoty z nastaveni a uloz je do pole
$sTmp= str_replace(')', '', str_replace('set(', '', str_replace('enum(', '', $tmpList)));
$aTmp= explode(',', str_replace("'", '', $sTmp));
foreach( $aTmp as $value ) {
$aSetList[$value]= $value;
} //db($aSetList);
return $aSetList;
} // END of fGetArrayFromSQLSet
Using CakePHP 3.6
$fruits = ['1'=>'orange','2'=>'melon','3'=>'lemon','4'=>'apple'];
echo $this->Form->control('Fruit', ['options'=>$fruits, 'label'=>"select your fruit", 'value'=>'lemon']);
Your dropdownlist will 3 come with 'lemon' selected by default.
This 2 code will produce the following html:
<div class="input select">
<label for="Fruit">select your fruit</label>
<select name="Fruit" id="Fruit">
<option value="1">orange</option>
<option value="2">melon</option>
<option value="3">lemon</option>
<option value="4">apple</option>
</select>
</div>
You 1 can find more info here:
https://book.cakephp.org/3.0/en/views/helpers/form.html#options-for-select-checkbox-and-radio-controls
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.