Array Returns Null

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();




             //selecting em_id from exam_master table







               $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()


                 )) ; 




           foreach($model as $m)


                  $object= new ExamQuestionMaster();








            // return $this->question;







            }//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(


                'condition'=>'subject_id = :sid',

                'params'=>array( ':sid' => $sub),


             $cname= Course::model()->find(array(


                'condition'=>'course_id = :cid',

                'params'=>array( ':cid' => $crs), 



        echo var_dump($this->$question);  // It returns null???


And here is the view of Displayquestion


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">';


//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>';



echo '</table>';


Because you are redirecting and the $question variable is being reset when the new request (from the redirect) comes in.

How to pass it then? i hav to display it in the view. please tell me

Can you explain what you’re trying to do? It looks like you’re trying to display a random set of questions for a given course and subject. If examId is unique, you can pass that to the display function and return the list of questions from that.


$this->redirect(array('displayQuestion', 'examId' => $examId, 'courseId' => $crs, 'subjectId' => $sub));


public function actionDisplayQuestion($examId, $courseId, $subjectId)


	$course= Course::model()->findByPk($courseId);

	$subject = Subject::model()->findByPk($subjectId);


	$examMaster = ExamQuestionMaster::model()->findByPk($examId);

	$questions = $examMaster->questions; // You need relations setup correctly to do this.


		'questions'=> $questions,

		'course'=> $course,





U gave me an idea, after i get data from the form, I will redirect directly to the displayquestion function and also pass course and subject value. And all the operations will be done inside actionDisplayquestion and rendered directly… :slight_smile:

where do i declare the variable, so that it does not get deleted/renewed/null after i take the value inside it and use redirect/render????

and Many many thanks for giving me idea…

You’ll have access to it if you render the view. The redirect causes a new request and it’s hitting a different action so the array is never populated.

Ok, now i get it… Thanks a lot