後盾網-CI框架實例教程-馬振宇 - 學習筆記(5)

第五節視頻:php

  實例操做文章與欄目相關表單驗證:css

    表單驗證操做:application/controllers/admin/article.phphtml

<?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="width: 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="width: 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>

==========================app

如何設置編輯和發表共用這一套規則:函數

  一、在application/config/下新建文件名爲:form_validation.php(必須是這個名字)post

  二、寫入:ui

<?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]'
        )
    ),
);

三、在application/controllers/admin/article.php 中修改代碼,把表單驗證規則設置爲公共規則:this

<?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');
        }


    }
}

====================================================================================url

在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="width: 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="width: 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>

 

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

一、在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]'
        ),
    ),
);
相關文章
相關標籤/搜索