Reputation: 1754
I have a database table worksheet(qID varchar(5), answer varchar(50), wsheetid varchar(5)) with qID as the primary key. One worksheet has many questions.
I want to fetch all the questions where wsheetid =1 and display them as a form so that user can enter the answers. And i want to check the user entered ans with the answer column in the database.
How can i do that in Yii.
Tried google and the guide to yii, couldnt find any solution. Any suggestion would be helpful.
Update:
I have the below view where i am am needed to get the attributes into a array and display the form. Is there a better way to do this? and for activeTextArea since model has the data, the text area contains data from database but i need a blank text area.
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'wdetails-form',
'enableAjaxValidation'=>false,
)); ?>
<?php echo CHtml::beginForm(); ?>
<?php foreach($questions as $i=>$questions): ?>
<?php $array = $questions->getAttributes();
echo CHtml::activeLabel($questions,"[$i]question",array('label'=>"$array[question]"));
echo CHtml::activeTextArea($questions,"[$i]answer",array('id'=>"$array[question_ID]"));
endforeach;
?>
</br>
<?php echo CHtml::submitButton('Submit'); ?>
<?php echo CHtml::endForm(); ?>
<?php $this->endWidget(); ?>
</div>
and in the controller, i need to insert the data that i got from the above form and insert into a different table(worksheetResults). DO i need to get the data into an array and use Yii DAO or is there any better way to do this.
table worksheetResults (username, worksheetID,question_ID,submitted_ans)
Upvotes: 0
Views: 3899
Reputation: 9402
Did you use Gii to create the scaffolding? That would get you the model and CRUD files and you can then customize the look and functionality. If your data model is good, this will save you a lot of time, even if you end up redoing the form view, you can still use Yii to create and maintain the model and controller classes.
See: http://www.yiiframework.com/doc/guide/1.1/en/topics.gii
Upvotes: 0
Reputation: 2398
I think you should have a Worksheet model, a Worksheet controller and a Worksheet view.
Here is a skeleton for the controller part:
class WorksheetController extends CController
{
public function actionQuestions()
{
$criteria = new CDbCriteria;
$criteria->addCondition('(wsheetid = :id)');
$criteria->params[':id'] = '1';
$questions = Worksheet::model()->findAll($criteria);
$this->render('questions_form', array('questions'=>$questions));
}
public function actionAnswers()
{
//check the contents of $_POST['Worksheet']
}
}
Upvotes: 1