Tutorial :CakePHP views part



Question:

I am having a problem in my CakePHP application. 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 it's 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 well in the Controller part, but if I use:

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

Only the last values are shown... Why is this so? Eg. my attributes 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. Why is this 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'));  ?><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; ?>  

for a sample I have used 12345 as options ..

within that elseif(dropdown) loop I have tried the options as you 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 I'm 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.


Solution:1

You should try to add attribute_id as key of $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;  

And in view you can manipulate this array by........... [Check your view.ctp code given here.. place this instead of 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']]));  


Solution:2

$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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »