仅允许特定用户使用代码点火器编辑/删除帖子


Only allow specific user to edit/delete post using codeigniter

我正在尝试编辑和删除特定用户的记录。我有想法,但不知道如何实现它?。

当管理员登录会话时,也开始使用会话库。现在我将这个会话数据发送到MODEL,那里正在进行添加操作。

使用新的学生数据,我还存储admin_id。

现在的重点是,当我要编辑和删除时,我只想显示数据。记录是adminid的管理员与学生数据一起存储。通过这个,我可以编辑和删除特定用户的记录。super_admin可以编辑/删除所有记录。

当用户登录时,我只发送admin_id和会话。

listing.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Listing extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->model('student');
    $this->load->helper('url');
    $this->load->helper('form');
    $s = $this->session->userdata('admin_id');
    log_message('error', 'Some variable did not contain a value.');
}
public function index()
{
    $s = $this->session->userdata('admin_id');
    $this->load->model('student',$s);
   //$data['result'] = $this->student->listing();
    $students = $this->student->listing();/////new line delete [resulet]time 5:42 29/03/16
     //$this->load->view('list_view',$data); //// change here time 5:52 29/03/16
    $this->load->view('list_view',array('students'=>$students)); /////listing->list_view name change
}   
public function delete($id)
{
    $result = $this->student->delete_operation($id);
    $s = $this->session->userdata('admin_id');// session data call.
    //$data['result'] = $this->student->listing();
    $students = $this->student->listing();///new line 30/03 1230pm// change for list_view
    $this->load->view('list_view',array('students'=>$students));///same as above//change for list_view
    //$this->load->view('list_view',$data); ////////////////////////listing->list_view name change
} 
public function edit($id)
{               
    if($this->input->post('edit') && $this->input->post('edit_id')!='')
    {       

        $id = $this->input->post('edit_id');
        $data = array(
        'student_name' => $this->input->post('txt_name'),
        'student_email' => $this->input->post('txt_email'),          
        'student_address' => $this->input->post('txt_address'),
        'subject' => $this->input->post('subject'),
        'marks' => $this->input->post('marks'),
        );
        $result = $this->student->update_record($id,$data);
        header('location:'.base_url().'index.php/listing');       
    }
    if($id)
    {
        $result = $this->student->edit_record($id);   
        $data['action'] = 'edit';
        $data['student_id'] = $result[0]->student_id;
        $data['student_name'] = $result[0]->student_name;
        $data['student_email'] = $result[0]->student_email;
        $data['student_address'] = $result[0]->student_address;
        $data['subject'] = $result[0]->subject;
        $data['marks'] = $result[0]->marks;
    }
    $this->load->view('edit_student',$data);   
}   
public function add_student()
{       
    //$s['user'] = $this->session->userdata('admin_id');//get session data // new line30/03/16
    $data['student_id'] = '';
    $data['student_name'] = '';
    $data['student_email'] = '';
    $data['student_address'] ='';
    $data['subject'] = '';
    $data['marks'] = '';
    //$data['admin_id']=''; //new line 12:39 30/03/16
    $this->load->view('edit_student',$data);           
}
public function add()
{
    $data = array(
    'student_name' => $this->input->post('txt_name'),
    'student_email' => $this->input->post('txt_email'),          
    'student_address' => $this->input->post('txt_address'),
    'subject' => $this->input->post('subject'),
    'marks' => $this->input->post('marks'),
    'admin_id' => $this->input->post('admin_id')//new line 12:39 31/03
    );
    $result = $this->student->add_record($id,$data);
    header('location:'.base_url().'index.php/listing');
}
}

我的模型文件student.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Listing extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->model('student');
    $this->load->helper('url');
    $this->load->helper('form');
    $s = $this->session->userdata('admin_id');
    log_message('error', 'Some variable did not contain a value.');
}
public function index()
{
    $s = $this->session->userdata('admin_id');
    $this->load->model('student',$s);
   //$data['result'] = $this->student->listing();
    $students = $this->student->listing();/////new line delete [resulet]time 5:42 29/03/16
     //$this->load->view('list_view',$data); //// change here time 5:52 29/03/16
    $this->load->view('list_view',array('students'=>$students)); /////listing->list_view name change
}   
public function delete($id)
{
    $result = $this->student->delete_operation($id);
    $s = $this->session->userdata('admin_id');// session data call.
    //$data['result'] = $this->student->listing();
    $students = $this->student->listing();///new line 30/03 1230pm// change for list_view
    $this->load->view('list_view',array('students'=>$students));///same as above//change for list_view
    //$this->load->view('list_view',$data); ////////////////////////listing->list_view name change
} 
public function edit($id)
{               
    if($this->input->post('edit') && $this->input->post('edit_id')!='')
    {       

        $id = $this->input->post('edit_id');
        $data = array(
        'student_name' => $this->input->post('txt_name'),
        'student_email' => $this->input->post('txt_email'),          
        'student_address' => $this->input->post('txt_address'),
        'subject' => $this->input->post('subject'),
        'marks' => $this->input->post('marks'),
        );
        $result = $this->student->update_record($id,$data);
        header('location:'.base_url().'index.php/listing');       
    }
    if($id)
    {
        $result = $this->student->edit_record($id);   
        $data['action'] = 'edit';
        $data['student_id'] = $result[0]->student_id;
        $data['student_name'] = $result[0]->student_name;
        $data['student_email'] = $result[0]->student_email;
        $data['student_address'] = $result[0]->student_address;
        $data['subject'] = $result[0]->subject;
        $data['marks'] = $result[0]->marks;
    }
    $this->load->view('edit_student',$data);   
}   
public function add_student()
{       
    //$s['user'] = $this->session->userdata('admin_id');//get session data // new line30/03/16
    $data['student_id'] = '';
    $data['student_name'] = '';
    $data['student_email'] = '';
    $data['student_address'] ='';
    $data['subject'] = '';
    $data['marks'] = '';
    //$data['admin_id']=''; //new line 12:39 30/03/16
    $this->load->view('edit_student',$data);           
}
public function add()
{
    $data = array(
    'student_name' => $this->input->post('txt_name'),
    'student_email' => $this->input->post('txt_email'),          
    'student_address' => $this->input->post('txt_address'),
    'subject' => $this->input->post('subject'),
    'marks' => $this->input->post('marks'),
    'admin_id' => $this->input->post('admin_id')//new line 12:39 31/03
    );
    $result = $this->student->add_record($id,$data);
    header('location:'.base_url().'index.php/listing');
}
}

我的视图文件

listing.php//控制器和视图文件具有相同的名称。

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
 <title>Login Form</title>  
</head>
<body>
<section class="container">
<div class="listing">
<a class="btn btn-default" href="<?php echo base_url(); ?>index.php/listing/add_student">Add</a>
<h1>student List </h1>
    <table style="width:100%" border="1">
    <tr>
            <th>Id</th>
            <th>student Name</th>
            <th>student Email</th>      
            <th>student Address</th>        
            <th>subject</th>
            <th>marks</th>
            <th>Action</th>
        </tr>
        <?php foreach($result as $r) { ?>
        <tr>
            <td><?php echo $r->student_id; ?></td>
            <td><?php echo $r->student_name; ?></td>
            <td><?php echo $r->student_email; ?></td>       
            <td><?php echo $r->student_address; ?></td>
            <td><?php echo $r->subject; ?></td>
            <td><?php echo $r->marks; ?></td>
            <td><a class="btn btn-default" href="<?php echo base_url(); ?>index.php/listing/edit/<?php echo $r->student_id; ?>" > Edit</a><a class="btn btn-default" href="<?php echo base_url(); ?>index.php/listing/delete/<?php echo $r->student_id; ?>" > Delete</a></td>
        </tr>
        <?php } ?>

    </table>
<a class="btn btn-primary" href="<?php echo base_url(); ?>index.php/admin_login/logout" role="button">Logout</a>
</section>   
</body>
</html>

有很多方法可以做到这一点,简单的方法就像下面的代码(编辑自己)

    if(!$this->session->userdata('User_id')==// specific user id ex: 1 or 0 ){
   // it will be blank so its shows nothing 
    }else{
    <td><a class="btn btn-default" href="<?php echo base_url(); ?>index.php/listing/edit/<?php echo $r->student_id; ?>" > Edit</a><a class="btn btn-default" href="<?php echo base_url(); ?>index.php/listing/delete/<?php echo $r->student_id; ?>" > Delete</a></td>
    }