如何在codeigniter中推送数组?

时间:2015-01-08 13:40:47

标签: php arrays codeigniter

我将我的数组声明为公共

class Doctor extends CI_Controller {

    public $prescription_drug = array();

.....

}

我需要像下面这样的数组

Array
(
        [0] => Array
        (
            [drugdoze] => 8
            [drugname] => 80
            [drugsize] => 5
            [drugtype] => 1
            [duration] => 
            [rx_duration] => 2
            [rx_instruction] => 3
            [rx_special_note] => “test 1”
        )
    [1] => Array
        (
            [drugdoze] => 4
            [drugname] => 10
            [drugsize] => 5
            [drugtype] => 3
            [duration] => 1
            [rx_duration] => 2
            [rx_instruction] => 3
            [rx_special_note] => “test 2”
        )
    [2] => Array
        (
            [drugdoze] => 1
            [drugname] => 13
            [drugsize] => 5
            [drugtype] => 3
            [duration] => 1
            [rx_duration] => 2
            [rx_instruction] => 3
            [rx_special_note] => “test 3”
        )

)


How can I append following array with
[3] => Array
        (
            [drugdoze] => 1
            [drugname] => 13
            [drugsize] => 5
            [drugtype] => 3
            [duration] => 1
            [rx_duration] => 2
            [rx_instruction] => 3
            [rx_special_note] => “test 3”
        )

我有一个公共职能

public function prescription_selected_drug_session()
    {

    $prescription_data= array(
                'drugdoze' => $this->input->post("drugdoze"),
                'drugname'=> $this->input->post("drugname"),
                'drugsize' => $this->input->post("drugsize"),
                'drugtype' =>$this->input->post("drugtype"),
                'duration'=> $this->input->post("duration"),
                'rx_duration' => $this->input->post("rx_duration"),
                'rx_instruction'=>$this->input->post("rx_instruction"),
                'rx_special_note' => $this->input->post("rx_special_note")
                );


    array_push($this->prescription_drug,$prescription_data);

}

我使用ajax调用该函数。但$ this-> prescription_drug的值每次都不会被覆盖。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

Php是无国籍的。每个ajax请求都会创建一个Doctor的新实例。

您需要在会话或数据库中保留数据,或者聚合客户端