开发者

Pass array within controller in cakePHP

In my controller i have 2 actions e.g

action1() {

//code

SomeArray=();

//code

}

How can i pass all the SomeArray data to action2?

  1. I have tried to create a public array variable in my class and pass it but with no luck.

  2. i have tried to pass as an argument to the action2...

e.g in action1, $this->action2(SomeArray) and then action2($param) with no luck again.

function doExam($id = null) {

if (!$id) {
    $this->Session->setFlash(__('Invalid exam开发者_StackOverflow中文版', true));
    $this->redirect(array('action' => 'index'));
}

$this->Exam->recursive=1;


$conditions_question = array('Question.exam_id' => $id);
$questions = $this->Exam->Question->find('all',array('conditions' => $conditions_question));

foreach ($questions as $question) {
    **$this->questionsByExam[]** = $question['Question']['qst'];
}
//OK PASSED
echo debug($this->questionsByExam);

//OK $exam_id  
$this->exam_id = $id;
}

i have another action validate_answer, and i want to pass the questionsByExam in here

any help?

Thanks in advance


I have tried to create a public array variable in my class and pass it but with no luck.

Can you show the code for this? It should work fine as class variable...

E.g:

class FooController extends AppController {

    var $someArray = array();

    function doExam() {

        // Populate the array here
        $this->someArray = array(1,2,3); 

    }

    function bar() {

        // Use it here, no need to pass it as an argument
        print_r($this->someArray);

    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜