• 后盾网-CI框架实例教程-马振宇


    第五节视频:

      实例操作文章与栏目相关表单验证:

        表单验证操作:application/controllers/admin/article.php

    <?php
    /**
     * Created by PhpStorm.
     * User: Administrator
     * Date: 2017/10/7
     * Time: 9:08
     */
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Article extends CI_Controller{
    
        //发表模板显示
        public function send_article(){
            //加载表单验证辅助函数
            $this ->load ->helper('form');
            $this ->load ->view('admin/article.html');
        }
    
        //发表文章动作
        public  function send(){
            //载入表单类
            $this ->load ->library('form_validation');
            //配置规则
            $this ->form_validation ->set_rules('title','文章标题','required|min_length[5]');//标题不能为空,至少为5个
            $this ->form_validation ->set_rules('type','类型','required|integer');//类型不能为空,保存在数据库里为整数;
            $this ->form_validation ->set_rules('cid','栏目','integer');//保存在数据库里为整数;
            $this ->form_validation ->set_rules('info','摘要','required|max_length[155]');
            $this ->form_validation ->set_rules('content','内容','reguired|max_length[2000]');
            //验证规则
            $status = $this ->form_validation ->run();
    
            if($status){
                echo '数据库操作';
            }else{
                $this ->load ->helper('form');
                $this ->load ->view('admin/article.html');
            }
    
    
        }
    }

    相对应的html页面:application/views/admin/article.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="<?php echo base_url().'style/admin/css/public.css'?>">
        <title>Title</title>
    </head>
    <body>
    <form action="<?php echo site_url('/admin/article/send')?>" method="post" enctype="multipart/form-data">
        <table class = "table">
            <tr>
                <td colspan="10" class="th">发表文章</td>
            </tr>
            <tr>
                <td>标题</td>
                <td><input type="text" name="title" value="<?php echo set_value('title') ?>">
                    <?php echo form_error('title','<span>','</td>') ?>
                </td>
            </tr>
            <tr>
                <td>类型</td>
                <td>
                    <input type="radio" name="type" value="0" <?php echo set_radio('type','0','TRUE') ?> > 普通
                    <input type="radio" name="type" value="1" <?php echo set_radio('type','1') ?>> 热门
                </td>
            </tr>
            <tr>
                <td>栏目</td>
                <td>
                    <select name="cid" id="">
                        <option value="1" <?php echo set_select('cid','1','TRUE') ?>>情感</option>
                        <option value="2" <?php echo set_select('cid','2') ?>>生活</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>缩略图</td>
                <td>
                    <input type="file">
                </td>
            </tr>
            <tr>
                <td>摘要</td>
                <td>
                    <textarea name="info" id="" style=" 550px;height: 160px;"><?php echo set_value('info') ?></textarea>
                    <?php echo form_error('info','<span>','</span>')?>
                </td>
            </tr>
            <tr>
                <td>内容</td>
                <td>
                    <textarea name="content" id="" style=" 550px;height: 160px;"><?php echo set_value('content')?></textarea>
                    <?php echo form_error('content','<span>','</span>')?>
                </td>
            </tr>
            <tr>
                <td colspan="10"><input type="submit" class="input_button" value="发布"></td>
            </tr>
    
        </table>
    </form>
    </body>
    </html>

    ==========================

    如何设置编辑和发表共用这一套规则:

      1、在application/config/下新建文件名为:form_validation.php(必须是这个名字)

      2、写入:

    <?php
    /**
     * Created by PhpStorm.
     * User: Administrator
     * Date: 2017/10/8
     * Time: 8:11
     */
    
    $config = array(
        'article' => array(
            array(
                'field' => 'title',
                'label' => '标题',
                'rules' => 'required|min_length[5]'
            ),
            array(
                'field' => 'type',
                'label' => '类型',
                'rules' => 'required|integer'
            ),
            array(
                'field' => 'cid',
                'label' => '栏目',
                'rules' => 'integer'
            ),
            array(
                'field' => 'info',
                'label' => '摘要',
                'rules' => 'required|max_length[155]'
            ),
            array(
                'field' => 'content',
                'label' => '内容',
                'rules' => 'required|max_length[2000]'
            )
        ),
    );

    3、在application/controllers/admin/article.php 中修改代码,把表单验证规则设置为公共规则:

    <?php
    /**
     * Created by PhpStorm.
     * User: Administrator
     * Date: 2017/10/7
     * Time: 9:08
     */
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Article extends CI_Controller{
    
        //发表模板显示
        public function send_article(){
            //加载表单验证辅助函数
            $this ->load ->helper('form');
            $this ->load ->view('admin/article.html');
        }
    
        //发表文章动作
        public  function send(){
            //载入表单类
            $this ->load ->library('form_validation');
    //        //配置规则
    //        $this ->form_validation ->set_rules('title','文章标题','required|min_length[5]');//标题不能为空,至少为5个
    //        $this ->form_validation ->set_rules('type','类型','required|integer');//类型不能为空,保存在数据库里为整数;
    //        $this ->form_validation ->set_rules('cid','栏目','integer');//保存在数据库里为整数;
    //        $this ->form_validation ->set_rules('info','摘要','required|max_length[155]');
    //        $this ->form_validation ->set_rules('content','内容','reguired|max_length[2000]');
            //验证规则
            $status = $this ->form_validation ->run('article');
    
            if($status){
                echo '数据库操作';
            }else{
                $this ->load ->helper('form');
                $this ->load ->view('admin/article.html');
            }
    
    
        }
    }

    ====================================================================================

    在application/controllers/admin/article.php控制器中添加:编辑文章方法、编辑动作方法

    <?php
    /**
     * Created by PhpStorm.
     * User: Administrator
     * Date: 2017/10/7
     * Time: 9:08
     */
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Article extends CI_Controller{
    
        //发表模板显示
        public function send_article(){
            //加载表单验证辅助函数
            $this ->load ->helper('form');
            $this ->load ->view('admin/article.html');
        }
    
        //发表文章动作
        public  function send(){
            //载入表单类
            $this ->load ->library('form_validation');
    //        //配置规则
    //        $this ->form_validation ->set_rules('title','文章标题','required|min_length[5]');//标题不能为空,至少为5个
    //        $this ->form_validation ->set_rules('type','类型','required|integer');//类型不能为空,保存在数据库里为整数;
    //        $this ->form_validation ->set_rules('cid','栏目','integer');//保存在数据库里为整数;
    //        $this ->form_validation ->set_rules('info','摘要','required|max_length[155]');
    //        $this ->form_validation ->set_rules('content','内容','reguired|max_length[2000]');
            //验证规则
            $status = $this ->form_validation ->run('article');
    
            if($status){
                echo '数据库操作';
            }else{
                $this ->load ->helper('form');
                $this ->load ->view('admin/article.html');
            }
    
    
        }
    
        //编辑文章
        public function editor_article(){
            $this ->load ->helper('form');
            $this ->load ->view('admin/editor_article.html');
        }
        //编辑动作
        public function editor(){
            $this ->load ->library('form_validation');
            $status = $this ->form_validation ->run('article');
            if($status){
                echo '数据库操作';
            }else{
                $this ->load ->helper('form');
                $this ->load ->view('admin/editor_article.html');
            }
        }
    
    }

    相对应的html页面:在application/views/admin下新建editor_article.html,代码如下:

      

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="<?php echo base_url().'style/admin/css/public.css'?>">
        <title>Title</title>
    </head>
    <body>
    <form action="<?php echo site_url('/admin/article/editor')?>" method="post" enctype="multipart/form-data">
        <table class = "table">
            <tr>
                <td colspan="10" class="th">编辑文章</td>
            </tr>
            <tr>
                <td>标题</td>
                <td><input type="text" name="title" value="<?php echo set_value('title') ?>">
                    <?php echo form_error('title','<span>','</td>') ?>
                </td>
            </tr>
            <tr>
                <td>类型</td>
                <td>
                    <input type="radio" name="type" value="0" <?php echo set_radio('type','0','TRUE') ?> > 普通
                    <input type="radio" name="type" value="1" <?php echo set_radio('type','1') ?>> 热门
                </td>
            </tr>
            <tr>
                <td>栏目</td>
                <td>
                    <select name="cid" id="">
                        <option value="1" <?php echo set_select('cid','1','TRUE') ?>>情感</option>
                        <option value="2" <?php echo set_select('cid','2') ?>>生活</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>缩略图</td>
                <td>
                    <input type="file">
                </td>
            </tr>
            <tr>
                <td>摘要</td>
                <td>
                    <textarea name="info" id="" style=" 550px;height: 160px;"><?php echo set_value('info') ?></textarea>
                    <?php echo form_error('info','<span>','</span>')?>
                </td>
            </tr>
            <tr>
                <td>内容</td>
                <td>
                    <textarea name="content" id="" style=" 550px;height: 160px;"><?php echo set_value('content')?></textarea>
                    <?php echo form_error('content','<span>','</span>')?>
                </td>
            </tr>
            <tr>
                <td colspan="10"><input type="submit" class="input_button" value="发布"></td>
            </tr>
    
        </table>
    </form>
    </body>
    </html>

    ===================================================================================

    1、在application/controllers/admin下新建栏目管理控制器category.php:

      在application/views/admin下新建html页面add_cate.html:

      需在后台首页中引入新建模板add_cate.html;<?php echo site_url('admin/category/add_cate')?>

           将验证写入到application/config/form_validation.php里面;

      编辑栏目:在application/views/admin下新建html页面editor_cate.html

      在category.php控制器中写入编辑栏目方法:

      附代码:application/controllers/admin/category.php栏目控制器:

    <?php
    /**
     * Created by PhpStorm.
     * User: Administrator
     * Date: 2017/10/8
     * Time: 12:03
     */
    
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Category extends CI_Controller{
    
        //添加栏目
        public function add_cate(){
            $this ->load ->helper('form');
            $this ->load ->view('admin/add_cate.html');
        }
    
        // 添加动作
        public function add(){
            //载入表单验证类库
            $this ->load ->library('form_validation');
            $status = $this ->form_validation ->run('cate');
    
            if($status){
                echo '数据库操作';
            }else{
                $this ->load ->helper('form');
                $this ->load ->view('admin/add_cate.html');
            }
    
        }
    
        //编辑栏目
        public function editor_cate(){
            $this ->load ->helper('form');
            $this ->load ->view('/admin/editor_cate.html');
    
        }
    
        //编辑栏目动作
        public function editor(){
            //载入表单验证类库
            $this ->load ->library('form_validation');
            $status = $this ->form_validation ->run('cate');
    
            if($status){
                echo '数据库操作';
            }else{
                $this ->load ->helper('form');
                $this ->load ->view('admin/editor_cate.html');
            }
    
        }
    
    
    }

    附code:application/views/admin/add_cate.html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="<?php echo base_url('style/admin/css/public.css')?>">
        <title>Title</title>
    </head>
    <body>
    <form action="<?php echo site_url('/admin/category/add')?>" method="post">
        <table class="table">
            <tr>
                <td class="th" colspan="10">添加栏目</td>
            </tr>
            <tr>
                <td>栏目名称</td>
                <td><input type="text" name="cname" value="<?php echo set_value('cname')?>">
                    <?php echo form_error('cname','<span>','</span>')?></td>
            </tr>
            <tr>
                <td colspan="10"><input type="submit" value="添加" class="input_button"></td>
            </tr>
        </table>
    </form>
    </body>
    </html>

    附code:application/views/admin/editor_cate.html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="<?php echo base_url('style/admin/css/public.css')?>">
        <title>Title</title>
    </head>
    <body>
    <form action="<?php echo site_url('/admin/category/editor')?>" method="post">
        <table class="table">
            <tr>
                <td class="th" colspan="10">编辑栏目</td>
            </tr>
            <tr>
                <td>栏目名称</td>
                <td><input type="text" name="cname" value="<?php echo set_value('cname')?>">
                    <?php echo form_error('cname','<span>','</span>')?></td>
            </tr>
            <tr>
                <td colspan="10"><input type="submit" value="添加" class="input_button"></td>
            </tr>
        </table>
    </form>
    </body>
    </html>

    附code:application/config/form_validation.php:公共表单验证规则:

    <?php
    /**
     * Created by PhpStorm.
     * User: Administrator
     * Date: 2017/10/8
     * Time: 8:11
     */
    
    $config = array(
        'article' => array(
            array(
                'field' => 'title',
                'label' => '标题',
                'rules' => 'required|min_length[5]'
            ),
            array(
                'field' => 'type',
                'label' => '类型',
                'rules' => 'required|integer'
            ),
            array(
                'field' => 'cid',
                'label' => '栏目',
                'rules' => 'integer'
            ),
            array(
                'field' => 'info',
                'label' => '摘要',
                'rules' => 'required|max_length[155]'
            ),
            array(
                'field' => 'content',
                'label' => '内容',
                'rules' => 'required|max_length[2000]'
            )
        ),
        'cate' => array(
            array(
                'field' => 'cname',
                'label' => '栏目名称',
                'rules' => 'required|max_length[20]'
            ),
        ),
    );
  • 相关阅读:
    熬夜到凌晨2点半

    浏览器F12,Network中各按钮的作用
    postman收藏 -大佬玩法。
    Windows日常快捷键
    认识jmeter(一)
    web测试:test过程中接口报错 "Object reference not set to an instance of an object."
    postman-error:SyntaxError: Invalid shorthand property initializer
    mysql 表结构及基本操作
    getinstance方法(转)
  • 原文地址:https://www.cnblogs.com/chengshun/p/7634505.html
Copyright © 2020-2023  润新知