I am using an array as global variable and after searching some data from a model, i am putting it in that array. And when i var_dump the array inside another function it returns null, even in view also it says null…
Note: by using the word global variable i mean that it is usable inside a particular controller only. Should be accessible by all its functions
public $question=array(); // my global variable for this controller only
public function actionCreate()
{
$db=new ExamMaster();
if(isset($_POST['ExamMaster'])){
//selecting em_id from exam_master table
$db->attributes=$_POST['ExamMaster'];
$crs=$_POST['ExamMaster']['course_id'];
$sub=$_POST['ExamMaster']['subject_id'];
$model = QuestionBankMaster::model()->findAll(array(
'select'=>array('question_id,question_entered','RAND()as rand'), //RAND() is used for random selection
'condition'=>'course_id = :cid AND subject_id = :sid',
'params'=>array(':cid'=> $crs, ':sid'=> $sub),
'order'=>'rand', // we have to use order by RAND()
'limit'=>3,
)) ;
$i=0;
foreach($model as $m)
{
$object= new ExamQuestionMaster();
$object->em_id=$examID;
$object->question_id=$m['question_id'];
$object->save();
$this->question[$i]=$m['question_entered'];
$i++;
}
// return $this->question;
$this->redirect(array('displayquestion','crs'=>$crs,'sub'=>$sub));
}//if closed
$this->render('create',array('db'=>$db,'courseList'=>$courseList,'subjectList'=>$subjectList,'examList'=> $examList));
}
I am redirecting to the following function:—
public function actionDisplayquestion($crs,$sub)
{
//selecting the subject name from subject table and course name from course table
$sname= Subject::model()->find(array(
'select'=>'subject_name',
'condition'=>'subject_id = :sid',
'params'=>array( ':sid' => $sub),
));
$cname= Course::model()->find(array(
'select'=>'course_name',
'condition'=>'course_id = :cid',
'params'=>array( ':cid' => $crs),
));
echo var_dump($this->$question); // It returns null???
$this->render('displayquestion',array('course'=>$cname,'subject'=>$sname));
And here is the view of Displayquestion
<?php
echo CHtml::link('Home',array('Site/Index')) . '<br/>';
echo 'Subject: '. $subject['subject_name'];
echo '</br>';
echo 'Course: '. $course['course_name'];
echo '</br>';
echo '<table class="myqustion">';
$limit=count($this->question);
//echo $limit;
for($i=0; $i<$limit; $i++)
{
echo '<tr><td>';
echo '<b>Question.'. $i+1 .'</b>' .$this->question[$i];
echo '</td></tr>';
echo '<tr><td>';
echo '</br>';
echo '<hr>';
echo '</br>';
$i++;
}
echo '</table>';
?>