Tabular input

I want to create a form with several questions and answers for each question .Upon form submission I want to save each question and it’s answers into the database.

Is there a way to implement this, using CHtml::activeTextArea, (basically I want to be able to insert something like this: Question[0][answer][0][name] into the input field name using question and answer models)?

Surely it is possible. But this has nothing to with yii rather with plain php. Just think of a appropriate delimiter to separate your Question and Answer. The php function explode will than save the parts in an array. Each line of the textarea could be looped through with an foreach loop.

But don’t you think it is easier to use one textarea for the question and one for the answer?

I want to create a quiz add page. There I want to be able to insert the quiz name, and multiple questions each question having multiple answers . I could have done this with inserting simple html input fields:

<input type="text" name="Question[0][answer][0][name]" />

<input type="text" name="Question[0][answer][0][points]" />

<input type="text" name="Question[0][answer][1][name]" />

<input type="text" name="Question[0][answer][1][points]" />

but I wanted to be sure if this can’t be done programatically via CHtml.

Ah okay, now I understand your question. Just create a new variable in your model:




public $field = array();



Now you can use CActiveForm Textarea or Textfield to fill your array:




<?php echo $form->textField($model,'field[0]'); ?> 

<?php echo $form->textField($model,'field[1]'); ?>