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
and Description
. I need to make a down down list in the item add page but I can not find a good way to get all of the values from another table into an array to put into the page.
Below I have also listed my models for each.
<?php
class Item extends AppModel
{
var $name = 'Item';
var $belongsTo = 'Itemgroup';
}
?>
class Itemgroup extends AppModel
{
var $name = 'Itemgroup';
var $hasOne = array('Item');
var $validate = array(
'description' => array(
'rule' => 'notEmpty'
),
'description' => array(
'rule' => 'notEmpty'
)
);
}
?>
Upvotes: 8
Views: 63864
Reputation: 9425
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 come with 'lemon' selected by default.
This 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 can find more info here:
https://book.cakephp.org/3.0/en/views/helpers/form.html#options-for-select-checkbox-and-radio-controls
Upvotes: 0
Reputation: 29
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));
Upvotes: 1
Reputation: 1
This is the right solution in my opinion:
of 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
Upvotes: 0
Reputation: 203
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.
Upvotes: 1
Reputation: 313
<?php
$arrCategory=array(1=>"Car",2=>"Boat",3=>"Bike");
echo $form->input('inputname', array('options'=>$arrCategory, 'label'=>false,
'empty'=>'Category','selected'=>'Your Value'));
?>
Upvotes: 4
Reputation: 21
please use
//In controller
$data=$this->Model->find('list');
$this->set('data',$data);
In View :
$this->Form->input('list',array("options"=>$data));
Upvotes: 1
Reputation: 11
Or You can use this:
In Model:
/**
* Get list of choises from collumn 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
In Controller:
# TB ENUM fields
$aFields= array('order', '<TB_col_SET_type>', '<TB_col_SET_type>', .... );
foreach ($aFields as $sFieldName ) {
$this->set(Inflector::pluralize(Inflector::variable($sFieldName)), $this->PHlaseni->fGetArrayFromSQLSet($sFieldName) );
}
In View is enought:
<?=$form->input('order')?>
<?=$form->input('<TB_col_SET_type>')?>
Upvotes: -7
Reputation: 26169
If it's something like a "US States" dropdown list that is going to be repeated from page to page, consider using an Element, which you can just pass data to and you won't have to repeat all the UI code again.
Upvotes: 0
Reputation: 28245
Assuming your model is User and the field you want to use is a list of US states (for example)...
In your controller:
$this->set('states',$this->State->find('list'));
and in your view:
<?php echo $form->input('User.state',array('type'=>'select','options'=>$states)); ?>
Upvotes: 23
Reputation: 3219
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
Upvotes: 4