如何使用代码点火器闪光变量


How to use codeigniter flash variable?

在 CodeIgniter 中处理闪存数据。

我基本上想:

向数据库添加问题 将用户重定向回页面 显示成功弹出消息"您的问题已创建"

到目前为止,我可以成功地将类别添加到数据库中,并且用户输入已正确验证,唯一的问题是我不知道如何创建弹出成功消息。(我不想加载成功视图),只需重定向回它们的来源并在右上角显示小消息或其他内容。

闪存数据是正确的选择吗?

控制器:-

 $create_data =  $this->input->post();
    if(isset($create_data['question'])){
    $this->load->model('Test_model', 'test');
    $insert_status = $this->test->insertQuestions($create_data['question']);
    if($insert_status){
            echo "Record Inserted";
        }
        else{
            echo "Insertion Failed";
        }
    }
$this->layout->view('test/create');

闪存数据听起来像是要走的路。你可以做这样的事情:

if($insert_status){
    $notification = "Record Inserted";  
} else {
    $notification = "Insertion Failed";
}
$this->session->set_flashdata('notification', $notification);
redirect('controller/method','refresh');

然后使用

$this->session->flashdata('notification');

该手册是此类信息的宝贵来源。

function insert(){
    $create_data =  $this->input->post();
    if(isset($create_data['question'])){
        $this->load->model('Test_model', 'test');
        $insert_status = $this->test->insertQuestions($create_data['question']);
        if($insert_status){
            //echo "Record Inserted";
            $this->session->set_flashdata('msg', 'Record Inserted'); //set session flash
            redirect('controller_name/insert', 'refresh');
        }
        else{
            //echo "Insertion Failed";
            $this->session->set_flashdata('msg', 'Insertion Failed'); //set session flash
            redirect('controller_name/insert', 'refresh');
        }
    }else{
        $this->layout->view('test/create');    
    }
}
?>    

在"查看"页面中:

<p><?=$this->session->flashdata('msg')?></p>