zoukankan      html  css  js  c++  java
  • 博客系统开发_管理员管理(三)

    管理员管理的功能包含

    1、显示所有的管理员Table

    2、添加管理员

    3、删除管理员

    4、修改管理员

    界面上包含

    1、一个通栏菜单,

    2、显示当前登录用户

    3、用户有个退出的功能

    一、管理员管理功能开发

    1.1、添加管理员

    1.1.1 添加新的用户控制器AuserController.class.php

    在用户控制器中AuserController.class.php中的index方法中调用自己的模板

    $this->display();  //调用自己的模板
    View Code

    1.1.2、模板的编写

             到View下面创建Auser文件夹,在Auser下创建index.php

             1、通栏菜单_nav.php  在view的根目录中

    <nav class="navbar navbar-default">
      <div class="container-fluid">   
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Admin</a>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav">
            <li><a href=" <?php echo U('/Admin/Blog/index'); ?> ">博客管理<span class="sr-only">(current)</span></a></li>
            <li><a href=" <?php echo U('/Admin/Auser/index'); ?>">管理员管理</a></li>     
            <li><a href=" <?php echo U('/Admin/Setting/index'); ?> ">系统设置</a></li>      
          </ul>     
          <ul class="nav navbar-nav navbar-right">       
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">欢迎 <span class="caret"></span></a>
              <ul class="dropdown-menu">
                <li><a href="#">退出</a></li>          
              </ul>
            </li>
          </ul>
        </div><!-- /.navbar-collapse -->
      </div><!-- /.container-fluid -->
    </nav>
    View Code

            在index.php中包含菜单HTML

    <?php  include(THEME_PATH.'/nav.php');?>
    View Code

    注:(1)THEME_PATH 表示当前主题的目录。此处指View文件夹的目录

             2、导航及表格代码

    <div class="page-header">
                    <h1>管理员列表 <small class="pull-right"><a href="<?php echo U("/Admin/Auser/add"); ?>" class="btn btn-default ">添加管理员</a></small></h1>
                </div>
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>用户</th>
                            <th>密码</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach($data['users'] as $user): ?>
                        <tr>
                            <th scope="row"><?php echo $user['aid'] ;?></th>
                            <td><?php echo $user['auser'] ;?></td>
                            <td><?php echo $user['apass'] ;?></td>
                            <td>
                                <a  href="<?php echo U("/Admin/Auser/add"); ?>?aid=<?php echo $user['aid']; ?>">编辑</a>
                                <a  href="<?php echo U("/Admin/Auser/del"); ?>?aid=<?php echo $user['aid'] ;?>">删除</a>
                            </td>
                        </tr> 
                        <?php  endforeach; ?>
                    </tbody>
                </table>            
    View Code

            3、表格中的数据显示_到控制器中查数据

    index方法中,(1)取出全部数据 (2)赋值到数组中 (3)将数据传递给模板

                    //实例化数据模型
            $admin = D("admin");
            //取出所有数据
            $users = $admin->select();
            //定义数组
            $data=array();
            $data['users'] = $users;   //给数据添加键值对
            //对模板变量赋值 ==$this->data =  $data;
            $this->assign("data",$data);  
    View Code

    注:(1)使用D函数实例化,还有模板中需要使用数据,需要将数据传递给模板,使用assign方法

           4、模板中循环使用

    1.1.3 实现添加管理员功能

          1、在AuserController控制器中实现一个add方法,在add方法中调用自己的模板

          2、编写add模板add.php

    <!DOCTYPE HTML>
    <html>
        <head>
            <title>添加管理员</title>
            <script src="/Public/js/jquery-3.2.1.js"></script>
            <link rel="stylesheet" href="/Public/bootstrap-3.3.7/css/bootstrap.css" />     
            <script src="/Public/bootstrap-3.3.7/js/bootstrap.js"></script>        
        </head>
        <body>
            <div class="container">
                <?php  include(THEME_PATH.'/nav.php');?>
                <div class="page-header">
                    <h1>添加管理员 <small class="pull-right"><a href="<?php echo U("/Admin/Auser/index"); ?>" class="btn btn-default ">返回</a></small></h1>
                </div>                
                <form class="form-horizontal" action=" <?php echo U("/Admin/Auser/save") ;?> ?aid=<?php echo $user['aid']; ?>" method="post">
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">用户</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control" name= "auser" id="auser" placeholder="请输入用户名" value="<?php echo $user['auser']; ?>">
                    </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">密码</label>
                        <div class="col-sm-8">
                            <input type="password" class="form-control" name="apass" id="apass" placeholder="请输入密码"  value="<?php echo $user['apass']; ?>">
                        </div>
                    </div>                        
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-8">
                            <button type="submit" class="btn btn-primary">提交注册</button>
                        </div>
                    </div>
                </form>
            </div>
        </body>
    </html>
    View Code

    注:(1)add.php中表单提交的地址

    <?php echo U("/Admin/Auser/save") ;?>

         3、回到Auser控制器中编写save方法

                     //1、实例化数据模型
            $admin = D("admin");        
            if(IS_POST){
                //2、获取用户输入
                 $auser =  I ("post.auser") ;
                 $apass =  I ("post.apass");
                 
                 //验证输入的合法性,使用框架下的rules
                 $rule = array(
                    array('auser','require',"用户不能为空"),
                    array('apass','require',"密码不能为空"),                 
                 );
    
                 if( !$admin->validate($rule)->create()){             
                     return $this->error($admin->getError(),"/Admin/Auser/add");
                 }            
    
                 //3、验证用户唯一性
                 $where =array(
                     'auser' => $auser,
                     );
                 $row = $admin->where($where)->find();
                 if($row){
                     return $this->error("用户名已存在","/Admin/Auser/add");
                 }
    
                 //4、插入数据
                 $insert =array(
                     'auser'=>$auser,
                     'apass'=>$apass,
                 );
                 $is = $admin->add($insert);
    
                 if($is){
                    return $this->success("操作成功","/Admin/Auser/index"); //跳转到列表页
                 }
                 else{
                    return $this->error("操作失败","/Admin/Auser/add");  //跳转回添加页
                 }
            }
    
       
    View Code

    注:(1)使用TP框架下的数据验证规则

           (2)组织插入数组$insert

           (3)使用TP框架下的数据插入方法

      1.2 删除管理员  

     1.2.1 HTML中链接提交页面

    <a  href="<?php echo U("/Admin/Auser/del"); ?>?aid=<?php echo $user['aid'] ;?>">删除</a>

     1.2.2  Auser控制器中编写del方法

    $aid = I('get.aid');        
    D("admin")->where( array('aid'=>$aid))->delete();
    return $this->redirect("/Admin/Auser/index");        
    View Code

    1.3 修改管理员

    1.3.1 HTML中链接提交页面

    <a  href="<?php echo U("/Admin/Auser/add"); ?>?aid=<?php echo $user['aid']; ?>">编辑</a>

    1.3.2 Auser控制器中编写add方法

    1、获取到选中用户,将相关数据显示到模板中

                            //获取到aid
                $aid = I('get.aid');
                $admin = D("admin");
                $user=array(
                    'auser'=>"",
                    'apass'=>"",            
                );
                //找到aid对应的数据
                if( $aid>0 ){
                    $user = $admin ->where( array('aid'=>$aid) )->find();                        
                }
                $this->assign("user",$user);  //将数组传递给模板
                //在模板中显示
                $this->display();  //调用自己的模板                
    View Code

    2、模板中显示数据

    <input type="text" class="form-control" name= "auser" id="auser" placeholder="请输入用户名" value="<?php echo $user['auser']; ?>">
    
    <input type="password" class="form-control" name="apass" id="apass" placeholder="请输入密码"  value="<?php echo $user['apass']; ?>">
    View Code

    3.点击提交后进入save方法

    前面模型实例化、获取aid,数据的验证和添加管理员时共用,判断如果传入aid 则为修改

                              if( $aid>0 ){    //修改管理员                
                     //验证
                     $where = array(
                         'auser'=>$auser,
                         'aid'=>array('NEQ',$aid),
                      );
                    $isArr = $admin->where ($where)->find();
                    var_dump($isArr);    
                    if($isArr){
                     return $this->error("存在相同用户名","/Admin/Auser/add?aid=".$aid);
                    }
                    else{                    
                         $update =array(
                         'auser'=>$auser,
                         'apass'=>$apass,
                     );
                     $admin->where( array('aid'=>$aid) )->save($update);
                     return $this->success("操作成功","/Admin/Auser/index");                                
                    }                
                 }
    View Code

    二、管理员管理代码

    1.nav.php   通栏菜单页

    <nav class="navbar navbar-default">
      <div class="container-fluid">   
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Admin</a>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav">
            <li><a href=" <?php echo U('/Admin/Blog/index'); ?> ">博客管理<span class="sr-only">(current)</span></a></li>
            <li><a href=" <?php echo U('/Admin/Auser/index'); ?>">管理员管理</a></li>     
            <li><a href=" <?php echo U('/Admin/Setting/index'); ?> ">系统设置</a></li>      
          </ul>     
          <ul class="nav navbar-nav navbar-right">       
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">欢迎 <span class="caret"></span></a>
              <ul class="dropdown-menu">
                <li><a href="#">退出</a></li>          
              </ul>
            </li>
          </ul>
        </div><!-- /.navbar-collapse -->
      </div><!-- /.container-fluid -->
    </nav>
    View Code

    2.index.php  列表页界面

    <!DOCTYPE HTML>
    <html>
        <head>
            <title>管理员管理</title>
            <script src="/Public/js/jquery-3.2.1.js"></script>
            <link rel="stylesheet" href="/Public/bootstrap-3.3.7/css/bootstrap.css" />     
            <script src="/Public/bootstrap-3.3.7/js/bootstrap.js"></script>        
        </head>
        <body>
    
            <div class="container">
                <?php  include(THEME_PATH.'/nav.php');?>
                <div class="page-header">
                    <h1>管理员列表 <small class="pull-right"><a href="<?php echo U("/Admin/Auser/add"); ?>" class="btn btn-default ">添加管理员</a></small></h1>
                </div>
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>用户</th>
                            <th>密码</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach($data['users'] as $user): ?>
                        <tr>
                            <th scope="row"><?php echo $user['aid'] ;?></th>
                            <td><?php echo $user['auser'] ;?></td>
                            <td><?php echo $user['apass'] ;?></td>
                            <td>
                                <a  href="<?php echo U("/Admin/Auser/add"); ?>?aid=<?php echo $user['aid']; ?>">编辑</a>
                                <a  href="<?php echo U("/Admin/Auser/del"); ?>?aid=<?php echo $user['aid'] ;?>">删除</a>
                            </td>
                        </tr> 
                        <?php  endforeach; ?>
                    </tbody>
                </table>            
            </div>
        </body>
    </html>
    View Code

    3.add.php  添加页界面

    <!DOCTYPE HTML>
    <html>
        <head>
            <title>添加管理员</title>
            <script src="/Public/js/jquery-3.2.1.js"></script>
            <link rel="stylesheet" href="/Public/bootstrap-3.3.7/css/bootstrap.css" />     
            <script src="/Public/bootstrap-3.3.7/js/bootstrap.js"></script>        
        </head>
        <body>
            <div class="container">
                <?php  include(THEME_PATH.'/nav.php');?>
                <div class="page-header">
                    <h1>添加管理员 <small class="pull-right"><a href="<?php echo U("/Admin/Auser/index"); ?>" class="btn btn-default ">返回</a></small></h1>
                </div>                
                <form class="form-horizontal" action=" <?php echo U("/Admin/Auser/save") ;?> ?aid=<?php echo $user['aid']; ?>" method="post">
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">用户</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control" name= "auser" id="auser" placeholder="请输入用户名" value="<?php echo $user['auser']; ?>">
                    </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">密码</label>
                        <div class="col-sm-8">
                            <input type="password" class="form-control" name="apass" id="apass" placeholder="请输入密码"  value="<?php echo $user['apass']; ?>">
                        </div>
                    </div>                        
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-8">
                            <button type="submit" class="btn btn-primary">提交注册</button>
                        </div>
                    </div>
                </form>
            </div>
        </body>
    </html>
    View Code

    4、AuserController.class.php  业务编写(CURD)

      1 <?php
      2 namespace AdminController;
      3 class AuserController extends AdmController {
      4 
      5      /*
      6       *列表显示
      7       */
      8     public function index(){ 
      9 
     10         //实例化数据模型
     11         $admin = D("admin");
     12         //取出所有数据
     13         $users = $admin->select();
     14         //定义数组
     15         $data=array();
     16         $data['users'] = $users;   //给数据添加键值对
     17         //对模板变量赋值 ==$this->data =  $data;
     18         $this->assign("data",$data);  
     19         $this->display();  //调用自己的模板
     20     }
     21 
     22      /*
     23       *修改管理员
     24       */
     25       public function add(){     
     26             //获取到aid
     27             $aid = I('get.aid');
     28             $admin = D("admin");
     29             $user=array(
     30                 'auser'=>"",
     31                 'apass'=>"",            
     32             );
     33             //找到aid对应的数据
     34             if( $aid>0 ){
     35                 $user = $admin ->where( array('aid'=>$aid) )->find();                        
     36             }
     37             $this->assign("user",$user);  //将数组传递给模板
     38             //在模板中显示
     39             $this->display();  //调用自己的模板
     40     }
     41 
     42     /*
     43      *删除管理员
     44      */
     45     public function del(){     
     46         $aid = I('get.aid');        
     47         D("admin")->where( array('aid'=>$aid))->delete();
     48         return $this->redirect("/Admin/Auser/index");        
     49     }
     50 
     51      /*
     52       *保存数据
     53       */
     54     public function save(){ 
     55         $aid = I('get.aid');
     56         //1、实例化数据模型
     57         $admin = D("admin");        
     58         if(IS_POST){
     59             //获取用户输入
     60              $auser =  I ("post.auser") ;
     61              $apass =  I ("post.apass");
     62              
     63              //验证输入的合法性,使用框架下的rules
     64              $rule = array(
     65                 array('auser','require',"用户不能为空"),
     66                 array('apass','require',"密码不能为空"),                 
     67              );
     68 
     69              if( !$admin->validate($rule)->create()){             
     70                  return $this->error($admin->getError(),"/Admin/Auser/add");
     71              }        
     72             
     73              if( $aid>0 ){    //修改管理员                
     74                  //验证
     75                  $where = array(
     76                      'auser'=>$auser,
     77                      'aid'=>array('NEQ',$aid),
     78                   );
     79                 $isArr = $admin->where ($where)->find();
     80                 var_dump($isArr);    
     81                 if($isArr){
     82                  return $this->error("存在相同用户名","/Admin/Auser/add?aid=".$aid);
     83                 }
     84                 else{                    
     85                      $update =array(
     86                      'auser'=>$auser,
     87                      'apass'=>$apass,
     88                  );
     89                  $admin->where( array('aid'=>$aid) )->save($update);
     90                  return $this->success("操作成功","/Admin/Auser/index");                                
     91                 }                
     92              }
     93              else{       //添加管理员                
     94                  //验证用户唯一性
     95                  $where =array(
     96                      'auser' => $auser,
     97                      );
     98                  $row = $admin->where($where)->find();
     99                  if($row){
    100                      return $this->error("用户名已存在","/Admin/Auser/add");
    101                  }
    102 
    103                  //插入数据
    104                  $insert =array(
    105                      'auser'=>$auser,
    106                      'apass'=>$apass,
    107                  );
    108                  $is = $admin->add($insert);
    109                  if($is){
    110                     return $this->success("操作成功","/Admin/Auser/index");
    111                  }
    112                  else{
    113                     return $this->error("操作失败","/Admin/Auser/add");
    114                  }
    115             }            
    116         }
    117     }        
    118 }
    View Code
  • 相关阅读:
    「四步接入」开启秀场直播,揭秘七牛云互动直播解决方案
    【直播预告】揭秘互动直播,技术咖携手美女主播解读直播背后那点事儿
    初识k8s(基础概念加历史了解)(一)
    Linux lsof命令的使用示例
    Linux netstat:查看网络状态
    Linux uname命令:查看系统和内核相关信息
    系统环境变量PATH的设置与查看,以Mac为例
    Shell echo命令
    Linux which命令
    【转译】每个Python开发者都应该掌握的8种数据结构
  • 原文地址:https://www.cnblogs.com/3309-whp/p/8023045.html
Copyright © 2011-2022 走看看