美文网首页
CI框架基础入门(二)

CI框架基础入门(二)

作者: peterz博客 | 来源:发表于2018-04-23 21:06 被阅读46次

    目录

    • 载入验证类
    • CI汉化
    • 创建模型
    • 载入模型
    • 配置数据库
    • 防跨站
    • 输入类
    • 使用AR类操作数据库(增删改查)
    • 分配数据给视图
    • 构造函数
    • 获取URL片段

    一、载入验证类

    • 载入验证类
      $this->load->library(form_validation);
    • 设置规则
      $this->form_validation->set_rules('name值','标签名称','规则');
    • 执行验证(返回bool值)
      $this->form_validation->run();

    表单验证辅助函数
    $this->load->helper('form');

    • set_value('name值') 重填数据
    • set_select('name值','value值','可选参数TRUE/FALSE' )
    • set_radio('name值','value值','可选参数TRUE/FALSE' )
    • set_checkbox('name值','value值','可选参数TRUE/FALSE' )
    • form_error('name值','<span>','</span>') 单独显示错误 参数('name值','开始标签','结束标签')

    注意:一定要在表单中加入<?php echo form_error('title', '<span>', '</span>') ?> 这样的代码 才会有表单验证提示

    <tr>
        <td>标题</td>
        <td><input type="text" name="title" value="<?php echo set_value('title') ?>"/>  
        <?php echo form_error('title', '<span>', '</span>') ?>      
        </td>
    </tr>
    

    简便写法,可设置多套规则
    application/config/ 文件夹中建立文件form_validation.php 写规则

    <?php 
    $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[200]'
            ),
            array(
                   'field'   =>  'content',
                   'label'  =>  '内容',
                   'rules'  =>  'required|max_length[2000]'
            )
    
        ),
    
        'cate'     =>   array(
            array(
                   'field'   =>  'cname',
                   'label'  =>  '栏目名称',
                   'rules'  =>  'required|max_length[20]'
            )
        )
    )
    ?>
    

    这样 只需写
    $this->load->library('form_validation');
    $status = $this->form_validation-run('article');
    就可以执行验证啦


    二、CI汉化

    将汉化包解压 命名为ch放入application/language文件夹下
    打开application/config/config.php 文件,
    找到 #confi['language'] = 'ch'; 将参数改为ch


    三、创建模型

    models目录下创建模型
    模型里类名的必须以_model结尾, 如category_model,且首字母大写,必须继承CI_Model类


    四、载入模型

    $this->load->model('catagory','cate');
    $this->cate->add();
    载入model,可以传递第二个参数 为模型取个别名


    五、配置数据库

    application/config/database.php文件中 找到

    • 配置
    'dsn'   => 'mysql:host=localhost;dbname=article',     //DSN 连接字符串(该字符串包含了所有的数据库配置信息)
        'hostname' => 'localhost',                        //主机名
        'username' => 'root',                             //用户名
        'password' => '123456',                           //密码
        'database' => 'article',                          //数据库名
    
    • 将数据库自动载入
      application/config/autoload.php文件中 找到
      $autoload['libraries'] = array('database');

    六、防跨站

    application/config/config.php文件中 找到
    $config['global_xss_filtering'] = TRUE; //对输入进行过滤,防止跨站、访问表单数据、类参考


    七、输入类

    引入输入类,能够预处理,会检查获取的数据是否存在,如果不存在则返回NULL
    $this->input->post('name值');

    $data = array(
            cname => $this->input->post('cname')
    );
    $this->cate->add($data);
    

    八、使用AR类操作数据库

    $active_record = TRUE; //默认开启 ,可通过 $this->db-> 操作里面的方法

    • 插入
    class AdminCategory_model extends CI_Model{
        public function addCategory($data){
            $this->db->insert('category',$data);    //插入数据库   参数(表名,参数) 
        }
    }
    
    • 查询
    public function queryAid($aid){     //select 选择字段, from 选择表, join 表关联,多表查询共同字段需要加上表名
            $data = $this->db->select('aid,title,info,content,type,category.cid')->from('article')->where(array('aid' => $aid ))->join('category','article.cid = category.cid')->order_by('aid','asc')->get()->result_array();
          //$data = $this->db->get_where('admin', array('username'=>$username))->result_array();
            return $data;
        }
    
    • 修改
    public function updateCate($cid,$data){
            $this->db->update('category',$data,array('cid' =>$cid ));   // 3个参数(' 表名','修改的值','条件')  修改的数据必须是数组格式
        }
    
    • 删除
    public function delCate($cid){
            $this->db->delete('category',array('cid' =>$cid ));     //2个参数(' 表名','条件')      修改的数据必须是数组格式    
        }
    

    九、数据分配给视图

    public function query(){
        $data['article'] = $this->db->get('article')->result_array();
        $this->load->view('home', $data); //$data通过第二参数传递到视图home
    }
    

    这里,把需要传递的数值加入至$data数组,CI在核心类中给自动使用extract()函数把数组“解压”出来,成为一个个变量。所以在view中可以直接这样使用变量:

    echo $article; 而不是 $data['article']


    十、构造函数

    public function __construct(){
            parent::__construct();          //CI中使用构造方法 必须先调用父类构造方法
            $this->load->model('adminCategory_model','cate');   
                    
        }
    

    十一、URI类-------用于从 URI 中获取指定段

    $cid = $this->uri->segment(3); //获取URI 第三片段

    通常用来修改或删除所选记录

    方法1:使用URI片段

    控制器代码
    public function delAttr(){
            $attr_id = $this->uri->segment(3);
            $this->adminAttribute_model->del($attr_id);
            success('adminAttribute/index','删除成功');
    }
    视图代码
    <a href="<?php echo site_url('adminAttribute/delAttr/'.$value['attr_id']); ?>删除</a>
    

    方法2:传递参数

    控制器代码
    public function delAttr($attr_id){
            $this->adminAttribute_model->del($attr_id);
            success('adminAttribute/index','删除成功');
    }
    视图代码
    <a href="<?php echo site_url('adminAttribute/delAttr/'.$value['attr_id']); ?>">删除</a>
    

    相关文章

      网友评论

          本文标题:CI框架基础入门(二)

          本文链接:https://www.haomeiwen.com/subject/klnllftx.html