Yii Framework Forum: Array Returns Null - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Array Returns Null

#1 User is offline   Aashis 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 18-March 12

Posted 24 April 2013 - 09:31 PM

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

?>

0

#2 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 24 April 2013 - 11:47 PM

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

#3 User is offline   Aashis 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 18-March 12

Posted 25 April 2013 - 01:10 AM

View Postwaterloomatt, on 24 April 2013 - 11:47 PM, said:

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
0

#4 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 25 April 2013 - 06:01 AM

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.

	$this->render('displayQuestion',array(
		'questions'=> $questions,
		'course'=> $course,
		'subject'=>$subject
	));
}



Matt
0

#5 User is offline   Aashis 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 18-March 12

Posted 25 April 2013 - 08:32 AM

View Postwaterloomatt, on 25 April 2013 - 06:01 AM, said:

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.



Matt

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.... :-)
0

#6 User is offline   Aashis 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 18-March 12

Posted 25 April 2013 - 08:38 AM

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....
0

#7 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 25 April 2013 - 12:09 PM

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.
0

#8 User is offline   Aashis 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 18-March 12

Posted 25 April 2013 - 07:30 PM

View Postwaterloomatt, on 25 April 2013 - 12:09 PM, said:

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users