Passing Array

I want to pass the value of array from one function to another in the same controller. I don’t know how to do it. please help me.

Suppose:




 class TestController extends Controller{

     

           public $abc=array();

        

        public function actionOne()

        {

           $xyz=array();

           if(isset($_POST['Student']))

             { 

                  //save and then call another action

                   $this->actionTwo($xyz);

                   return;

             }

            else

                  $this->render('create',array('db'=>$db));  

        }


       public function actionTwo($this->abc)

        {

             

                  //do something with the array and redirect


                  $this->render('create');  

        }

}




     Is the above example of passing array correct?





Secondly:- 



 class TestController extends Controller{

     

              

        public function actionOne()

        {

           $xyz=array();

           if(isset($_POST['Student']))

             { 

                  //save and then call another action

                   $this->actionTwo($xyz);

                   return;

             }

            else

                  $this->render('create',array('db'=>$db));  

        }


       public function actionTwo($abc=array())

        {

             

                  //do something with the array and redirect


                  $this->render('create',array('abc'=>$abc));  

        }

   }




I tried this also, but did not display anything in view? So am i correct this way… or not?

And one more question is:- how to pass the array using redirect function?

example:




class TestController extends Controller{


        

        public function actionOne()

        {

           $xyz=array();

           if(isset($_POST['Student']))

             { 

                  //save and then call another action

                   $this->redirect(array('Two','xyz'=>$xyz);

                   return;

             }

            else

                  $this->render('create',array('db'=>$db));  

        }


       public function actionTwo($xyz=array())

        {

             

                  //do something with the array and redirect


                  $this->render('create');  

        }

}



Please correct me…

your second solutions are almost correct… I tried with same code… it’s working for me…




<?php

/**** TestController.php**/

class TestController extends Controller

{

	public function actionIndex()

	{			

        $days=array('sunday','monday','tuesday');

		if(sizeof($days)>0)

		$this->actionTest($days);

		else

        $this->render('index');

    }

    public function actionTest($daysPassed=array())

    {

        $this->render('test',array('days'=>$daysPassed));   	

	}

}



If you want I will attach view files too…

i guess the view will be like this:


$limit=count($days);

for($i=0; $i<$limit; $i++)

{

  echo '<li><b>';

  echo $days[$i];

  echo '</b></li>';

}



thanks a lot, and do u know how pass array through redirect function?

Hi,

please have a look here… open the Source Code…by clicking show…

  1. http://www.yiiframew…redirect-detail

  2. http://www.yiiframew…redirect-detail

If you see here, these two method used in yii for redirection…

when you say redirect in yii or inside yii method, it actualy using the php function…




 header('Location: '.$url, true, $statusCode);



So, when this line executes… the control comes to the browser… so… if you passing PHP array … that will not work… it shows the ‘Array’ in url of browser…

still if you want pass the PHP arry to next action… you can append the data in URL… like this…




public function actionRedirect()

{

	$days=array('sunday','monday','tuesday');

	//$this->actionTest($days);

	$this->redirect(array('test/display/?day[]='.$days[0].'&day[]='.$days[1].'&day[]='.$days[2]));

}



you get the day in $_GET[‘day’] as a array…

i can pass a normal variable only… Thanks for explaining me