Dropdown select list in CakePHP

前端 未结 10 951
一个人的身影
一个人的身影 2020-12-15 11:59

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

相关标签:
10条回答
  • 2020-12-15 12:22

    please use

        //In controller
        $data=$this->Model->find('list');
        $this->set('data',$data);
    

    In View :

        $this->Form->input('list',array("options"=>$data));
    
    0 讨论(0)
  • 2020-12-15 12:24

    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>')?>
    
    0 讨论(0)
  • 2020-12-15 12:26

    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.

    0 讨论(0)
  • 2020-12-15 12:28

    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

    0 讨论(0)
  • 2020-12-15 12:32

    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)); ?>
    
    0 讨论(0)
  • 2020-12-15 12:32

    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 
    
    0 讨论(0)
提交回复
热议问题