Yii Framework Forum: Passing Array - Yii Framework Forum

Jump to content

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

Passing Array

#1 User is offline   Aashis 

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

Posted 29 April 2013 - 11:40 AM

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

#2 User is offline   gaurish 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 05-March 12
  • Location:Pune, India

Posted 30 April 2013 - 06:44 AM

View PostAashis, on 29 April 2013 - 11:40 AM, said:

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...
Thanks,
Gaurish
0

#3 User is offline   Aashis 

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

Posted 30 April 2013 - 07:33 PM

View Postgaurish, on 30 April 2013 - 06:44 AM, said:

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

#4 User is offline   gaurish 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 05-March 12
  • Location:Pune, India

Posted 02 May 2013 - 06:32 AM

View PostAashis, on 30 April 2013 - 07:33 PM, said:

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....
Thanks,
Gaurish
0

#5 User is offline   Aashis 

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

Posted 02 May 2013 - 07:14 PM

View Postgaurish, on 02 May 2013 - 06:32 AM, said:

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