zoukankan      html  css  js  c++  java
  • CI框架入门中的简单MVC例子

    最简单的CI模型:

    注意:模型需要用到数据库

    配置文件在appcation/config.php

    这里我们要用到数据库,需要将databases.php中的

    相关参数填写一下,具体不再赘述。

    直接进入主题:

    MVC:

    1、首先谈“M” 模型

    CI中的模型存放在application/models文件夹里

    命名规则是:类名_model.php

    文件中只包含一个类:

    如:

    class Nb_model extends CI_Model {
    
      public function __construct()
      {
        //连接数据库
        $this->load->database();
      }
    
      public function get(){
         //查询数据库
         $query=$this->db->get('users');
         //以数组形式返回查询结果
       return $query->result_array();
      }
    }

    2、其次谈“C”

    有了数据库模型及其方法,那么我们就该提取数据了

    CI中的控制器存放在application/controllers文件夹中

    命名规则:类名.php

    如:

    //防止非法访问
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Nb extends CI_Controller {
        public function __construct()
        {
        parent::__construct();
      //加载数据模型
        $this->load->model('nb_model');
      }
      public function index()
      {
              //根据数据模型获取数据
        $data['nb']=$this->nb_model->get();
        //加载视图文件
        $this->load->view('nb',$data);
      }
    }
    //文件末尾注释
    /* End of file nb.php */
    /* Location: ./application/controllers/nb.php */
    

    3、最后谈“V”

    有了数据库模型及其方法,那么我们就该提取数据了

    CI中的控制器存放在application/controllers文件夹中

    命名规则:类名.php(当然也可以不是类名,只要是跟控制器

    中的view传参的名字一致即可)

    如:

    <html>
      <head>
        <title>CI heiilo world</title>
      </head>
      <body>
        <!--循环输出数据-->
        <?php foreach($nb as $v):?> 
          <h1><?=$v['email']?></h1>
        <?php endforeach?>
      </body>
    </html>
    
  • 相关阅读:
    Spring cloud父项目的建立
    Spring cloud简介
    ssm整合
    springboot-注解讲解
    springboot-helloworld实现
    Java线程池七个参数
    JVM性能调优
    SpringBoot的特性
    Spring与Spring Boot核心注解
    mybatis中#{} 和 ${}的区别
  • 原文地址:https://www.cnblogs.com/out8/p/4364894.html
Copyright © 2011-2022 走看看