2017-02-21 5 views
0

cakephpオプションの値を作成する際に問題があります。ドロップダウンリストのオプション値

add.ctpで私のコード

<h1>New Book</h1> 
<?php 
echo $this->Form->create('Book',array('enctype'=>'multipart/form-data')); 
echo $this->Form->input('title'); 
echo $this->Form->input('author'); 
echo $this->Form->input('summary',array('rows' => '3')); 
echo $this->Form->input('category',array('type'=>'select','options'=>$category)); 
echo $this->Form->input('price'); 
echo $this->Form->input('image',array('type'=>'file')); 
echo $this->Form->end('Save'); 
?> 

とコントローラ機能を追加

public function add(){ 
    $this->loadModel('Category'); 
    $category =$this->Category->find('all', array('fields'=>'name')); 
    debug($category); 
    $this->set('category',$category); 
    if($this->request->is('post')){ 

    $this->Book->create(); 
    $filePath="./img/images/".$this->request->data['Book']['image']['name']; 
    $filename=$this->request->data['Book']['image']['tmp_name']; 
    if(move_uploaded_file($filename, $filePath)){ 
     $this->request->data['Book']['cover']=$this->request->data['Book']['image']['name']; 
    } 
    if ($this->Book->save($this->request->data)) { 
     $this->Flash->success(__('Your book has been saved.')); 
    } 
    return $this->redirect(array('controller' =>'Books','action' => 'index')); 
    } 
} 

私は私のカテゴリ名での私のリストボックスが私のフォームでのforeachを行うことはできませんします。どうして?

+0

にhttp://stackoverflow.com/questions/42291925/how-to-display-an-array-of-fetched-data -from-database-to-dropdown-list-in-cake/42292168#42292168 – drmonkeyninja

答えて

0

変更

$category =$this->Category->find('all', array('fields'=>'name'));

$category =$this->Category->find('list');

関連する問題