create a statistics


i am having question form and an answer form …

in the question table i am having qid,question fields…

in the answer table i am having aid,qid,answer fields…

i want to generate a statistics for the answers in the answer table using the qid…

can anyone help me how to do it… or say is this possible…


That sure is possible.

What you need is a function in the question-model to get you all answers given to that question like:

public static function getAnswers($questionID)


	//Gives you an array of Answer-Objects that were given to the specified questionIDs

	$answers = Answer::model->findall("answer = 'Question::model->findByPk($questionID)->question'");

	//Now you can do whatever you like with this - i.e. return it as a string:

	$return = "";

	foreach($answers AS $elem)


		$return .= $elem->answer."<br>";


	return $return;


Now this needs to be displayed in a view. Something like:

$this->widget('zii.widgets.CDetailView', array(




		array('label'=>'Answers','type'=>'raw', 'value'=>Question::getAnswers($model->questionID)),




This should display every given answer to the specific question. Maybe you should count the ones that are equal to display every answer only once but with an attatched counter, but that’s all to be done in the function.


thanks for ur reply…

but i m not getting it clearly…

this is my model code…2490


this is my controller code…2491


this is my view code…2493


can u please help me how to display the no of answers entered for the particular qid …


You can let the database do that counting for you:

(Not entirely sure about the code, it’s quite quick and dirty. There are better ways to get to this using the yii query buider but this should work as well)

public static function getAnswerCount($questionID)


	$connection = Yii::app()->db;

	$command = $connection->createCommand("SELECT DISTINCT que, (SELECT COUNT(que) FROM Y_question b WHERE a.que = b.que AND qid = questionID) FROM Y_question a WHERE qid = questionID");

	$rows = $command->queryAll();

	$return = "<table><tr><td>Answer</td><td>Count</td></tr>";

	foreach ($rows AS $elem)


		$return .= "<tr><td>".$elem['name']."</td><td>".$elem['computed']."</td></td>";


	$return .= "</table>";

It’s not that elegant to to this with hard-coded html-table-tags, but you should get the direction how to solve this problem.

The view - something like this:


array('label'=>'Answers and count','type'=>'raw', 'value'=>Y_question::getAnswerCount($model->qid)),


Good luck with that


thanks for replying but its not working…