CakePHP查看部分

时间:2009-07-23 04:26:07

标签: cakephp

我在CakePHP应用程序中遇到问题。我想从Table命名选项中检索值,我正在检索它。值在控制器部分正确显示。但在我看来,它只显示了检索到的最后一个值......

我的代码是:

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

上述内容适用于Controller部分,但如果我使用:

                         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;

但是我收到了错误,同时显示了整个数组,但我只想要标签性别和资格认证选项的性别选项。

2 个答案:

答案 0 :(得分:1)

你应该尝试添加attribute_id作为$ choices的键,

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

答案 1 :(得分:0)

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

这将在您的视图中设置一个名为$options的变量,其中包含$choices。您无法多次设置此变量,视图中只能有一个$options。你几次覆盖同一个变量,所以只有最后一次才能完成。你想要的是更类似于:

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

但我认为您的代码可以使用更多的改进。在foreach循环中从数据库中多次获取结果不是一个好主意。