2009-07-23 12 views
0

私のCakePHPアプリケーションに問題があります。私はそれを正しく取得している選択肢のテーブルから値を取得したい。値はコントローラー部で正しく入力されています。上記CakePHPの表示部分

function view($formid = null,$userid=null)//viewPage 
     { 
     $this->set('Forms',$this->Form->find('all',array('conditions'=>array('Form.id'=>$formid),'fields'=>array('Form.name')))); 
     $this->data['Form']['id']=$formid; 
     $viewfields=$this->Form->viewForms($this->data); 
     $this->set('viewfields',$viewfields);//retreives all the Attributes from the Form (like attribute_id,,label) 

      foreach($viewfields as $attributeid)://For each attribute id , i am checking if there is any choices in the Table Choices 
        $choices=$this->Choice->find('all',array('conditions'=>array('Choice.attribute_id'=>$attributeid['Attribute']['id'],'Choice.label'=>$attributeid['Attribute']['label']),'fields'=>array('Choice.choice','Choice.label'))); 

       if(!empty($choices)){ 
       $this->set('options',$choices); 

          foreach($choices as $c): 
            echo $c['Choice']['label']; 
            echo $c['Choice']['choice']; 

           endforeach; 
         } 

      endforeach; 
     } 

コントローラの部分でうまく動作しますが、私が使用している場合:しかし、私のビューの一部で、それは、検索だけ最後の値を示しています...

私のコードがある

      foreach($options as $c): 
           echo $c['Choice']['label']; 
       echo $c['Choice']['choice']; 

          endforeach; 

最後の値のみが表示されます...これはなぜですか? 例私の選択]テーブルで

 id form_id label type sequence_no 
     1 1  Name text 1 
     2 1  age number 2 
     3 1  gender dropdown 3 
     4 1  email-id email 4 
     5 1  qualification dropdown 5 

 id attribute_id label choice sequence 
     1 3    gender male 1 
     2 3    gender female 2 
     3 5   qualification BE 1 
     4 5    qualification ME 2 
     5 5    qualification MBA 3 

view.ctpで、私は資格のためのエントリのみを取得しています、私の属性テーブルには、のようなエントリが含まれています。なぜこれはそうですか?

編集:

マイビューのページはのようなものです:私はオプションとして12345を使用しているサンプルのため

<?php foreach ($viewfields as $r): ?> 
    if($r['Attribute']['type']=='text'||$r['Attribute']['type']=="email"){ 
echo $form->input($r['Attribute']['label'], array('id'=>$r['Attribute']['id'],'name'=>$r['Attribute']['label'],'type'=>'text','style' => 'width:' . $r['Attribute']['size'] . 'px')); 
?><br> 
} 

    else if($r['Attribute']['type']=='dropdown') 
           { 
//here i want the Male and female for the label gender and for the label Qualification as BE ME MBA 


echo $form->input($r['Attribute']['label'], array('id'=>$r['Attribute']['id'],'name'=>$r['Attribute']['label'],'options' => array(1,2,3,4,5))); 

     } 
<?php endforeach; ?> 

..

あなたが言ったように私はオプションを試してみましたそのelseif(dropdown)ループ内like

 foreach($options as $c): 

                echo $c['Choice']['label']; 
               echo $c['Choice']['choice']; 
                echo $c[1]['Choice']['label']; 
                echo $c[1]['Choice']['choice']; 
                endforeach; 

エラーも表示されますが、配列全体が表示されますが、ラベルのジェンダーオプションと資格の認定オプションのジェンダーオプションのみが必要です。

答えて

0
$this->set('options', $choices); 

これは$choicesが含まれているビューに$optionsと呼ばれる変数を設定します。この変数を複数回設定することはできません。ビューには$optionsが1つしかないことがあります。同じ変数を何度も上書きしているので、最後に貼り付けるだけです。あなたが望むのは、次のようなものです。

$options = array(); 
foreach (...) { 
    ... 
    $options[] = $results; 
} 
$this->set('options', $options); 

しかし、あなたのコードはもっと多くの改良を加えることができると思います。 foreachループ内のデータベースから何度も結果を取得することはお勧めできません。

1

あなたは、$選択肢のキーとしてATTRIBUTE_ID追加しよう

foreach($viewfields as $attributeid)://For each attribute id , i am checking if there is any choices in the Table Choices 
      $choices[$attributeid['Attribute']['id']]=$this->Choice->find('all',array('conditions'=>array('Choice.attribute_id'=>$attributeid['Attribute']['id'],'Choice.label'=>$attributeid['Attribute']['label']),'fields'=>array('Choice.choice','Choice.label'))); 
      $this->set('options',$choices); 
endforeach; 

とビューにあなたは...........によってこの配列を操作することができなければならない[あなたのview.ctpコードを確認array(1,2,3,4,5)の代わりにこれを置きます。

echo $form->input($r['Attribute']['label'], array('id'=>$r['Attribute']['id'],'name'=>$r['Attribute']['label'],'options' => $options[$r['Attribute']['id']])); 
関連する問題