2009-07-23 48 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我只獲得資格的條目。這是爲什麼?

編輯:

我的看法頁面就像是:

<?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; ?> 

,因爲我已經使用12345作爲選項的樣本..

elseif(dropdown)環路內

像你說的我都試過的選項像

 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。你幾次覆蓋同一個變量,所以只有最後一次粘貼。你想要的更類似於:

$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代碼這裏給出..放置此代替陣列(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']]));