title: Yii, Looping form fields for Tabular Input
body: <p>I am using the Yii Framework 1.1.17, and have generated three models:
  Question, AnswerOption and Response.</p>
  
  <p>The relationships:</p>
  
  <pre><code>Table: Question (list of questions)
    id
    text
  
  Table: AnswerOption (list of possible answers, associated with question)
    id
    question_id
    text
  
  Table: Response (question and selected answer collector)
    id
    question_id
    answer_option_id
    text
  </code></pre>
  
  <p>I am trying to create a form, and admittedly collect answers for all possible questions.</p>
  
  <p>File: ResponseController</p>
  
  <pre><code>public function actionCreate()
  {
      // load all questions and with it the possible answer Options
      $questions = Question::model()-&gt;findAll();
  
      // get number of questions
      $count = Question::Model()-&gt;count();
  
      $model = array();
      $i = 1;
  
      while ($i &lt;= $count) {
          $model[] = Response::model();
          $i++;
      }
  
      if (isset($_POST['Response'])) {
          // 
     }
  
      $this-&gt;render('create', array(
          'model' =&gt; $model,
          'questions' =&gt; $questions,
      ));
  }
  </code></pre>
  
  <p>This is the area that I am having trouble with:</p>
  
  <p>File: response/_form</p>
  
  <pre><code>&lt;?php foreach($questions as $i=&gt;$question): ?&gt;
  
      &lt;?php echo CHtml::activehiddenField($question,"[$i]id"); ?&gt; &lt;?php echo $question['text']; ?&gt;
  
          &lt;?php $options = CHtml::listData($question-&gt;answerOptions, 'id', 'text');?&gt;
  
          &lt;?php echo CHtml::activeDropDownList(AnswerOption::model(), "[$i]text", $options, array('empty' =&gt; 'Select answer...')); ?&gt;
  
  &lt;?php endforeach; ?&gt;
  </code></pre>
  
  <p>I may have populated my questions and possible answers, but I need to validate and save the results in $model.</p>
  
  <p>It seems like I cannot find a way to efficiently work this out.  Can someone please guide me?</p>