Codeigniter会话flashdata没有显示正确的消息

时间:2015-05-22 07:27:54

标签: codeigniter

当我提交表单时,它不会显示正确的flashdata消息。

如果我的用户组已被删除,则应显示信息消息但由于某种原因,即使已成功从数据库表中删除,也会显示delete_error_message。

问题:设置flashdata消息的最佳方式是什么,如果成功则显示$this->session->set_flashdata('info', 'You have modified user groups!');,如果不成功则显示$this->session->set_flashdata('delete_error_message', 'Could not delete User Group');

<?php

class User_group_delete extends MX_Controller {

    public function __construct() {
        parent::__construct();

        $admin_userdata = $this->session->userdata('admin');

        if ($admin_userdata['token'] == FALSE) {

            $this->session->set_flashdata('token_message', 'Invalid Session Token Please Login Again');
            redirect('admin');

        } elseif ($admin_userdata['token'] == TRUE) {

            if (!$this->user->hasPermission('access', 'user_group/user_group_delete')) {
                redirect('admin/error/permission');
            }
        }

    }

    public function index() {

        $data['user_group_id'] = $this->uri->segment(4);

        $this->form_validation->set_rules('name', 'User Group Name', 'required|callback_validate');

        if ($this->form_validation->run($this) == FALSE) {

            $this->load->view('template/user_group/user_group_delete_view', $data);

        } else {


            if ($this->delete_user_group()) {

                $this->session->set_flashdata('info', 'You have modified user groups!');

                redirect('admin/user_group/user_group_list');

            } else {

                $this->session->set_flashdata('delete_error_message', 'Could not delete User Group');

                redirect('admin/user_group/user_group_list');

            }

        }
    }

    public function delete_user_group() {
        $this->db->where('name', $this->input->post('name'));
        $this->db->where('user_group_id', $this->uri->segment(4));
        $this->db->delete($this->db->dbprefix .'user_group');
    }

    public function validate() {
        if ($this->user->hasPermission('modify', "user_group/user_group_delete")) {

            return TRUE;

        } else {

            $this->form_validation->set_message('validate', 'Warning: You do not have permission to modify user groups');

            return FALSE;

        }
    }
}

查看消息

<?php if ($this->session->flashdata('success')) { ?>
<div class="alert alert-info text-center">
<?php echo $this->session->flashdata('success'); ?>
</div>
<?php } ?>

<?php if ($this->session->flashdata('info')) { ?>
<div class="alert alert-info text-center">
<?php echo $this->session->flashdata('info'); ?>
</div>
<?php } ?>

<?php if ($this->session->flashdata('delete_error_message')) { ?>
<div class="alert alert-danger">
<?php echo $this->session->flashdata('delete_error_message'); ?>
</div>
<?php } ?>

2 个答案:

答案 0 :(得分:2)

您忘记了return函数中的delete_user_group()类型,如下所示

public function delete_user_group() {
 $this->db->where('name', $this->input->post('name'));
 $this->db->where('user_group_id', $this->uri->segment(4));
 $this->db->delete($this->db->dbprefix .'user_group');
 $row=$this->db->affected_rows();
 if($row>0){
  return TRUE;
 }else{
  return FALSE;
 }
}

答案 1 :(得分:1)

我认为delete_user_group()没有返回任何值,因此它不起作用。您可以尝试以下代码:

public function delete_user_group() {
    $this->db->where('name', $this->input->post('name'));
    $this->db->where('user_group_id', $this->uri->segment(4));
    if($this->db->delete($this->db->dbprefix .'user_group'))
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}