• phpcms v9二次开发之模型类的应用(2)


    二、模型操作方法select()--查询语句
             //查询级别管理列表信息
        public function levellists() {


                $lelists = $this->level->select();//查询语句方法


                 include $this->admin_tpl('level_list');//加载后台级别管理列表的模板
              }
             模型select()方法,默认情况下查询fb_level表所有字段的信息,上面的levellists()方法如果转化为mysql语句为:
             
             public function levellists() {


                    $sql="select * from fb_level ";//sql查询语句方法


                    $lelists=$this->level->query($sql);


                    include $this->admin_tpl('level_list');//加载后台级别管理列表的模板
                  }
      查询获取的数据列表如图:
             


    三、模型操作方法update()--更新语句
    //修改级别信息

      public function editlevel() {


      if(isset($_get['id'])){//获取要修改的级别id


        $id = intval($_get['id']);


        if(!$id) showmessage("操作失败");//如果获取的id为空,则返回操作提示信息


          if(isset($_post['dosubmit'])) {


           $this->level->update($_post['level'], array('id' => $id));


          } else {


         $where = array('id' => $_get['id']);//查询条件,是一个数组


         $level_info = $this->level->get_one($where);//get_one()模型方法,获取一条记录


         include $this->admin_tpl('level_edit');//加载后台级别修改的模板

         }

      }

       
     }
     写成等效的mysql语句
      public function editlevel() {


      if(isset($_get['id'])){//获取要修改的级别id


        $id = intval($_get['id']);


        if(!$id) showmessage("操作失败");//如果获取的id为空,则返回操作提示信息


          if(isset($_post['dosubmit'])) {


            $sql="update fb_level set level = '".$_post['level']."' where id = '".$id."' ";


            $lelists=$this->level->query($sql);  


          } else {


         $where = array('id' => intval( $_get['id']));//查询条件,是一个数组


         $level_info = $this->level->get_one($where);//get_one()模型方法,获取一条记录


         include $this->admin_tpl('level_edit');//加载后台级别修改的模板

         }

      }

       
     }

     修改别级效果图:
     

     

    四、模型操作方法delete()--删除语句
    //删除级别信息 
    public function dellevel() {
      
      if(isset($_get['id'])){

        $levelid = intval($_get['id']);


        $result = $this->level->delete(array('id'=>$levelid));//模型删除语句


        if($result){//返回删除结果提示信息


         showmessage(l("操作成功"),'?m=football&c=level&a=levellists');


        }else {


         showmessage(l("操作失败"),'?m=football&c=level&a=levellists');


        }

        } 
     }
    同样道理,写成等效的mysql的删除语句如
      
    public function dellevel() {
      
      if(isset($_get['id'])){

        $id = intval($_get['id']);


        $result=$this->level->query("delete from `fb_level` where `id` in ($id )");


        if($result){//返回删除结果提示信息


         showmessage(l("操作成功"),'?m=football&c=level&a=levellists');


        }else {


         showmessage(l("操作失败"),'?m=football&c=level&a=levellists');


        }

        } 
     } 
     

    至此,明白模型类model.class.php的查询、添加、修改、删除数据的运用,我们就可以在phpcms v9二次开发中根据需求随心所欲地开发自己的模块。 

  • 相关阅读:
    二部图(二分图判定--dfs)
    chd校内选拔赛题目+题解
    敌兵布阵 线段树单点更新
    Employment Planning DP
    Tickets 基础DP
    Super Jumping! Jumping! Jumping! 基础DP
    【高精度加法】
    【最短路径之dijkstra(迪杰斯特拉)算法】
    各类最短路算法基本模板-C++
    【最小生成树之Prim算法】-C++
  • 原文地址:https://www.cnblogs.com/semcoding/p/3358795.html
Copyright © 2020-2023  润新知