0

I am declare my array as public

class Doctor extends CI_Controller {

    public $prescription_drug = array();

.....

}

I need array like following

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

I have a public function

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

}

I call the function using ajax. But the value of $this->prescription_drug is overwriten every times not appended.

Can anyone help me?

1 Answer 1

1

Php is stateless. Every ajax request creates a new instance of Doctor.

You will need to persist the data in session or database, or aggregate client side

Sign up to request clarification or add additional context in comments.

2 Comments

if($this->session->userdata('prescription_selected_drug')) { $temp_array = $this->session->userdata('prescription_selected_drug'); array_push($temp_array, $prescription_data); $this->session->set_userdata('prescription_selected_drug', $temp_array); } else { $this->session->set_userdata("prescription_selected_drug",$prescription_data); }
but now array became Array ( [drugdoze] => 2 [drugname] => 155 [drugsize] => 25 [drugtype] => 1 [duration] => 1 [rx_duration] => 1 [rx_instruction] => 1 [rx_special_note] => this is test1 [0] => Array ( [drugdoze] => 2 [drugname] => 303 [drugsize] => 3 [drugtype] => 1 [duration] => 1 [rx_duration] => 2 [rx_instruction] => 1 [rx_special_note] => this is test2 ) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.