如何在codeigniter中单击按钮后删除一些会话数组段

时间:2016-10-14 09:44:49

标签: php arrays codeigniter session

我该怎么做?请帮帮我。

这是我的控制器功能

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

如果我点击视图页面$deleted_plan中的删除按钮,请填写jQuery。

这是我的jQuery代码

$(".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);
            }
        }
    });
});

我的问题是在单击“删除”按钮后删除会话数组中的数组段。

2 个答案:

答案 0 :(得分:3)

试试这个,希望它能帮助

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

答案 1 :(得分:1)

您可以参考以下示例:

take an array and set in session

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

现在取消设置特定元素,首先get session into a variable like here $arra

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

Unset specific array element您想要取消设置,如果您想remove 'plan1'

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

Set result array into session

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

OR

您可以使用

直接取消设置会话元素
$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