用户行未删除

时间:2014-10-05 08:12:06

标签: php codeigniter

我试图这样做,一旦用户点击了一个选中的复选框,然后删除按钮就会删除该行。

我无法删除已选择的用户行。

我对用户的删除功能无法使用该模型。

模型

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users_model extends CI_Model {

    public function deleteUser($user_id) {
        $this->db->where('user_id', $user_id);
        $this->db->delete('user');
    }

    public function getUsers() {
        $this->db->select('user_id');
        $this->db->select('username');
        $this->db->select('date_added');
        $this->db->select('status');
        $this->db->from('user');
        $this->db->limit(20);
        $this->db->order_by('username', 'asc');
        $query_user = $this->db->get();
        if ($query_user->num_rows) {
            return $query_user->result_array();
            return true;
        } else {
            return false;
        }
    }
}

控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends MX_Controller {
    public function __construct() {
        parent::__construct();
        $this->lang->load('admin/user/users', 'english');
        $this->load->model('admin/user/users_model');
        if ($this->session->userdata('isLogged')) {
            return true;
        } else {
            redirect('admin');
        }
    }

    public function index() {
        $this->document->setTitle($this->lang->line('heading_title'));
        $this->getList();
    }

    public function add() {

    }

    public function edit() {

    }

    public function delete() {

        if ($this->input->post('selected') == TRUE) {

            $this->users_model->deleteUser($user_id);

            redirect('users/users');

        } else {

            $this->getList();

        }
    }

    private function getList() {

        $data['heading_title'] = $this->lang->line('heading_title');

        // Get Bread Crumbs

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

        $data['breadcrumbs'][] = array(
            'text' => '<i class="fa fa-home"></i>' .' '.  $this->lang->line('text_home'),
            'href' => site_url('admin/dashboard')
        );

        $data['breadcrumbs'][] = array(
            'text'      => $this->lang->line('heading_title'),
            'href'      => site_url('admin/users')
        );

        // Get DB Results

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

        $results = $this->users_model->getUsers();

        foreach ($results as $result) {
            $data['users'][] = array(
                'user_id' => $result['user_id'],
                'username' => $result['username'],
                'date_added' => $result['date_added'],
                'status' => ($result['status'] ? $this->lang->line('text_enabled') : $this->lang->line('text_disabled')),
                'edit' => site_url('admin/users/edit/' . $result['user_id']),
                'delete' => site_url('admin/users/delete/' . $result['user_id'])
            );
        }

        // Set Data

        $data['text_confirm'] = $this->lang->line('text_confirm');

        $data['column_username'] = $this->lang->line('column_username');
        $data['column_status'] = $this->lang->line('column_status');
        $data['column_date_added'] = $this->lang->line('column_date_added');
        $data['column_action'] = $this->lang->line('column_action');

        if (trim($this->input->post('selected'))) {
            $data['selected'] = (array)$this->input->post('selected');
        } else {
            $data['selected'] = array();
        }

        return $this->load->view('user/users', $data);

    }

}

查看

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

<div id="content">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<h1><?php echo $heading_title;?></h1>
<ol class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
<?php } ?>
</ol>
</div>
</div>

<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title"><?php echo $heading_title; ?></h3></div>
<div class="panel-body">
<div class="table-responsive">
<table class="table table-bordered table-hover">
<thead>
    <tr>
        <td class="text-center"><input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" /></td>
        <td><b><a>User ID<a></b></td>
        <td><b><a><?php echo $column_username;?><a></b></td>
        <td><b><a><?php echo $column_status;?><a></b></td>
        <td><b><a><?php echo $column_date_added;?></a></b></td>
        <td class="text-right"><b><?php echo $column_action;?></b></td>
    </tr>
</thead>
<tbody>
<?php if ($users) { ?>
<?php foreach ($users as $user) { ?>
<tr>
<td class="text-center"><?php if (in_array($user['user_id'], $selected)) { ?>
<input type="checkbox" name="selected[]" value="<?php echo $user['user_id']; ?>" checked="checked" />
<?php } else { ?>
<input type="checkbox" name="selected[]" value="<?php echo $user['user_id']; ?>" /> 
<?php } ?></td>
<td class="text-left"><?php echo $user['user_id'];?></td>
<td class="text-left"><?php echo $user['username']; ?></td>
<td class="text-left"><?php echo $user['status']; ?></td>
<td class="text-left"><?php echo $user['date_added']; ?></td>
<td class="text-right">
<a href="<?php echo $user['edit']; ?>" class="btn btn-primary"><i class="fa fa-pencil"></i></a>
<a href="<?php echo $user['delete']; ?>" class="btn btn-danger" onclick="confirm('Are You Sure You Wish To Delete This User?') ? $('#form-user').submit() : false;"><i class="fa fa-trash-o"></i></a>
</td>
</tr>
<?php } ?>
<?php } ?>
</tbody>
</table>
</div><!-- . Table Responsive -->
</div><!-- . Panel Panel-Body -->
</div><!--  . Panel Panel-Default -->
</div><!-- . Columns -->
</div><!-- . Row -->
</div><!-- . Container-fluid-->
</div><!-- #Content -->

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

1 个答案:

答案 0 :(得分:0)

您没有发送$user_id

参数
public function delete() { // here you need to send a parameter $user_id ex. delete($user_id)

        if ($this->input->post('selected') == TRUE) {

            $this->users_model->deleteUser($user_id);

            redirect('users/users');

        } else {

            $this->getList();

        }
    }