tags:

views:

106

answers:

1

Hi, i am having a doubt in my application(CakePHP).i want to retrieve the values from the Table named choices i am retrieving it correctly.The values are coming correctly in the controller part But in my view part its showing only the last values retrieved ..

My code is,

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

The above works good in the Controller part , But if i used

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

                          endforeach;

Only the last values are shown .. Why so?? Eg. my atrbiutes table contains entries like

      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

In my choices table

     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

in the view.ctp i am getting only the entries for qualification y so???????

Edit:

My view page is like

    <?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')); ?>
}

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

   }

for a sample i have used 12345 as options ..

within that elseif(dropdown)loop i have tried the options as u said like

     foreach($options as $c):

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

But getting errors and also the whole array is displayed but i want only the gender options for the label gender and qualification options for the qualifications

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

This will set a variable called $options in your view that contains $choices. You can not set this variable several times, there can only be one $options in your view. You're overwriting the same variable several times, so only the last time it sticks. What you want is something more akin to:

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

But I think your code could use a lot more improvements. Fetching results several times from the database in a foreach loop is not a good idea.

deceze