Dropdown select list in CakePHP

2020-02-09 04:16发布

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'
        )
    );
}

?>

标签: cakephp
10条回答
\"骚年 ilove
2楼-- · 2020-02-09 04:51

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

查看更多
Evening l夕情丶
3楼-- · 2020-02-09 04:57

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>')?>
查看更多
对你真心纯属浪费
4楼-- · 2020-02-09 04:59

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)); ?>
查看更多
对你真心纯属浪费
5楼-- · 2020-02-09 05:00

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.

查看更多
爷的心禁止访问
6楼-- · 2020-02-09 05:03

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));
查看更多
smile是对你的礼貌
7楼-- · 2020-02-09 05:04

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

查看更多
登录 后发表回答