CodeIgniter Forums

Full Version: unset session userdata array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
On my permissions form I can select and un select check boxes which when form is submitted it gets added to database and then after re login gets added to sessions.

What I would like to be able to try and do is if check box is empty when I submit my form then it will unset it from sessions when my form is submitted. permissions[access] && permissions[modify]

How can I do that?

PHP Code:
public function edit() {

$this->load->model('admin/user/model_user_group');

if (
$this->input->server('REQUEST_METHOD') == 'POST') {

$this->model_user_group->editUserGroup($this->uri->segment(4), $this->input->post());

redirect('admin/users_group');
            
}

$this->getForm();



PHP Code:
public function getForm() {
        
$data['title'] = "Users Group";

$this->load->model('admin/user/model_user_group');

$user_group_info $this->model_user_group->getUserGroup($this->uri->segment(4));

if (
$this->uri->segment(4) == FALSE) {
$data['name'] = $this->input->post('name');
} else {
$data['name'] = $user_group_info['name'];
}

if (
$this->uri->segment(4) == FALSE) {
$data['user_group_id'] = $this->input->post('user_group_id');
} else {
$data['user_group_id'] = $user_group_info['user_group_id'];
}
        
$ignore = array(
'admin',
'dashboard',
'filemanager',
'login',
'menu',
'register',
'online',
'customer_total',
'user_total',
'chart',
'activity',
'logout',
'footer',
'header',
'permission'
);

$data['permissions'] = array();

$files glob(FCPATH 'application/modules/admin/controllers/*/*.php');

foreach (
$files as $file) {
$permission ┬ábasename(strtolower($file), '.php');

if (!
in_array($permission$ignore)) {
$data['permissions'][] = $permission;
}
}

$permission_access $this->input->post('permission');
if (isset(
$permission_access)) {
if (isset(
$permission_access['access'])) {
$data['access'] = $permission_access['access'];
} elseif (!empty(
$user_group_info['permission']['access'])) {
$data['access'] = $user_group_info['permission']['access'];
} else {
$data['access'] = array();
}
}

$permission_modify $this->input->post('permission');
if (isset(
$permission_modify)) {
if (isset(
$permission_modify['modify'])) {
$data['modify'] = $permission_modify['modify'];
} elseif (!empty(
$user_group_info['permission']['modify'])) {
$data['modify'] = $user_group_info['permission']['modify'];
} else {
$data['modify'] = array();
}
}

$this->load->view('template/user/users_group_form.tpl'$data);


View


PHP Code:
<?php echo Modules::run('admin/common/header/index');?>

<div id="wrapper">
<?php echo Modules::run('admin/common/menu/index');?>
<div id="page-wrapper" >
<div id="page-inner">

<div class="panel panel-default">
<div class="panel-heading clearfix">
<div class="pull-left" style="padding-top: 7.5px"><h1 class="panel-title"><?php echo $title;?></h1></div>
<div class="pull-right">
<a href="<?php echo base_url('admin/users_group');?>" class="btn btn-primary">Cancel</a>
<button type="submit" form="users_group" class="btn btn-primary">Save</button>
</div>
</div>
<div class="panel-body">

<?php if ($this->uri->segment(4) == FALSE) { ?>
<?php $data 
= array('class' => 'form-horizontal''id' => 'users_group');?>
<?php 
echo form_open('admin/users_group/add'$data);?>
<?php 
} else { ?>
<?php $data 
= array('class' => 'form-horizontal''id' => 'users_group');?>
<?php 
echo form_open('admin/users_group/edit' .'/'$this->uri->segment(4), $data);?>
<?php 
?>

<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php 
echo form_label('User Group Name''name'$data);?>
<div class="col-sm-10">
<?php 
$data_user_name 
= array(
'id' => 'name'
'name' => 'name'
'class' => 'form-control'
'value' => $name
)
;
?>
<?php 
echo form_input($data_user_name);?>
</div>
</div>

<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php 
echo form_label('User Group Id''user_group_id'$data);?>
<div class="col-sm-10">
<?php 
$data_user_group_id 
= array(
'id' => 'user_group_id'
'name' => 'user_group_id'
'class' => 'form-control'
'value' => $user_group_id
)
;
?>
<?php 
echo form_input($data_user_group_id);?>
</div>
</div>

<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php 
echo form_label('Access Permission''permission_access'$data);?>
<div class="col-sm-10">
<div class="well well-sm" style="height: 200px; overflow: auto;">
<?php foreach ($permissions as $permission) { ?>
<div class="checkbox">
<label>
<?php if (in_array($permission$access)) { ?>
<?php 
$data_checked 
= array(
'name' => 'permission[access][]',
'id' => 'permission_access',
'value' => $permission,
'checked' => TRUE,
);
echo 
form_checkbox($data_checked);
?>
<?php 
echo $permission?>
<?php 
} else { ?>
<?php 
$data_not_checked 
= array(
'name' => 'permission[access][]',
'id' => 'permission_access',
'value' => $permission,
'checked' => FALSE,
);
echo 
form_checkbox($data_not_checked);
?>
<?php 
echo $permission?>
<?php 
?>
</label>
</div>
<?php ?>
</div>
<a onclick="$(this).parent().find(':checkbox').prop('checked', true);">Select All</a> / <a onclick="$(this).parent().find(':checkbox').prop('checked', false);">Unselect All</a></div>
</div>

<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php 
echo form_label('Modify Permission''permission_modify'$data);?>
<div class="col-sm-10">
<div class="well well-sm" style="height: 200px; overflow: auto;">
<?php foreach ($permissions as $permission) { ?>
<div class="checkbox">
<label>
<?php if (in_array($permission$modify)) { ?>
<?php 
$data_checked 
= array(
'name' => 'permission[modify][]',
'id' => 'permission_modify',
'value' => $permission,
'checked' => TRUE,
);
echo 
form_checkbox($data_checked);
?>
<?php 
echo $permission?>
<?php 
} else { ?>
<?php 
$data_not_checked 
= array(
'name' => 'permission[modify][]',
'id' => 'permission_modify',
'value' => $permission,
'checked' => FALSE,
);
echo 
form_checkbox($data_not_checked);
?>
<?php 
echo $permission?>
<?php 
?>
</label>
</div>
<?php ?>
</div>
<a onclick="$(this).parent().find(':checkbox').prop('checked', true);">Select All</a> / <a onclick="$(this).parent().find(':checkbox').prop('checked', false);">Unselect All</a></div>
</div>

<?php echo form_close();?>
</div>
</div>

</div><!-- # Page Inner End -->
</div><!-- # Page End -->

</div><!-- # Wrapper End -->

<?php echo Modules::run('admin/common/footer/index');?>
Are you talking about having it affect the current user, all logged in users, or a different user who is logged in?
(03-10-2015, 08:02 AM)CroNiX Wrote: [ -> ]Are you talking about having it affect the current user, all logged in users, or a different user who is logged in?

Hi, Thanks for reply I think I have found what best way is.


PHP Code:
public function edit() {

$this->load->model('admin/user/model_user_group');

if (
$this->input->server('REQUEST_METHOD') == 'POST') {

$this->model_user_group->editUserGroup($this->uri->segment(4), $this->input->post());

$user_group_query $this->db->query("SELECT permission FROM " $this->CI->db->dbprefix "user_group 
WHERE user_group_id = '" 
. (int)$this->session->userdata('user_group_id') . "'");

$permissions unserialize($user_group_query->row('permission'));

$this->session->set_userdata($permissions);

redirect('admin/users_group');
            
}

$this->getForm();