Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove some session array segment after click button in codeigniter

How can I do this. Please help me.

This is my controller function

public function payment_plan() {

    $result = new stdClass();
    $result->status = FALSE;
    $post_data = $this->input->post();
    if ($post_data) {
        $payment_plan = $post_data['payment_plan'];
        $value = $post_data['value'];

        $deleted_plan = $this->input->post('key');

        $payment_plans = $payment_plan."/".$value.",";            

        $payment_session = $this->session->userdata('payment_plans');
        if($payment_session == NULL){
            $array_one = array();
            array_push($array_one, $payment_plans);
            $this->session->set_userdata('payment_plans', $array_one);
        }  else {
           $session_two=  $this->session->userdata('payment_plans');
           array_push($session_two, $payment_plans);               
           $this->session->set_userdata('payment_plans', $session_two);
        }

        $final_session = $this->session->userdata('payment_plans');

        $this->data['payment_plans'] = $final_session;
        //var_dump($final_session);
        $result->status = TRUE;
        $this->load->view('cms/payment_plan', $this->data);
    }
}

If I clicked delete button in view page $deleted_plan fill via jQuery.

This is my jQuery code

$(".key").click(function () {

    var key = $(this).attr('id');        

    jQuery.ajax({
        type: "POST",
        url: "<?php echo base_url(); ?>" + "property" + "/" + "payment_plan",
        dataType: 'html',
        data: {key: key,payment_plan: payment_plan, value: value},
        success: function (data) {
            if (data) {
                // $("#payment_plans").html(data);
            }
        }
    });
});

My problem is how I remove array segments in session array after I clicked Delete button.

like image 675
Menaka Kariyawasam Avatar asked Oct 14 '16 09:10

Menaka Kariyawasam


People also ask

How can delete session data in CodeIgniter?

Remove Session Data In PHP, we can remove data stored in session using the unset() function as shown below. unset($_SESSION['some_name']); Removing session data in CodeIgniter is very simple as shown below. The below version of unset_userdata() function will remove only one variable from session.

How do you destroy a session in ci4?

To clear the current session (for example, during a logout), you may simply use either PHP's session_destroy() function, or the library's destroy() method.

How do you empty a session variable in PHP?

$_SESSION['name']; Output: If you want to destroy all the session variables, then use the following PHP function. session_destroy();

What is session array?

Session arrays are like session variables which maintain a unique link between user's web page and the server. You can read more on session management here. Related Tutorial PHP Session PHP Cookies. Let us start with declaring a session array.


2 Answers

Try this, hope it'll help

if($deleted_plan)
{
    foreach($final_session as $k => $session)
    {
         if(isset($session[$deleted_plan]))
         {
            unset($final_session[$k][$deleted_plan]);
         }
    }
    $final_session = array_values($final_session);
    $this->session->set_userdata('payment_plans', $final_session);
}
like image 73
Pankaj Dadure Avatar answered Sep 22 '22 06:09

Pankaj Dadure


You can refer below example :

Lets take an array and set in session like

$array_one = array(0=>'plan1',1=>'plan2',2=>'plan3');
$this->session->set_userdata('payment_plans', $array_one);

Now to unset specific element, First get session into a variable like here $arra

$arra = $this->session->userdata('payment_plans');

Unset specific array element you want to unset like if you want to remove 'plan1'

unset($arra[0]); //0 is key of plan1

Set result array into session again

$this->session->set_userdata('payment_plans', $arra);
$arra = $this->session->userdata('payment_plans');                
print_r($arra);

OR

You can directly unset session element using

$array_one = array(0=>'plan1',1=>'plan2',2=>'plan3');
$this->session->set_userdata('payment_plans', $array_one); //set session
unset($_SESSION['payment_plans'][0]); //unset specific element. 0 is key of session array
$arra = $this->session->userdata('payment_plans');  // get session array              
print_r($arra); // display session array
like image 28
Rakesh Sojitra Avatar answered Sep 21 '22 06:09

Rakesh Sojitra