0

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)

2 Answers 2

1

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']
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. Can you also please look at my update question. Thanks
0

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

1 Comment

yes i have used gii but the problem is i have to get around ten records and display as form and save the submitted form into another table. In the above updated section of my question i have my form view where i am getting the data after converting to an array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.