zoukankan      html  css  js  c++  java
  • CI框架学习笔记

    一、CI版本

         开发版本3.1.2   下载地址:https://github.com/bcit-ci/CodeIgniter/archive/3.1.2.zip

    二、开发步骤

        1、解压文件到www/ci 目录下

        2、创建数据库 myci  后建表 user

    CREATE TABLE `user` (
    `id` int(5) NOT NULL AUTO_INCREMENT,
    `uname` varchar(20) DEFAULT NULL,
    `age` int(2) DEFAULT NULL,
    `product` varchar(200) DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=17 DEFAULT CHARSET=gbk;
    
     
    

      3、修改数据库配置文件 ciapplicationconfigdatabase.php

      

    $db['default'] = array(
    	'dsn'	=> '',
    	'hostname' => 'localhost',
    	'username' => 'root',
    	'password' => 'root',
    	'database' => 'myci',
    	'dbdriver' => 'mysqli',
    

      4、修改routes.php 下的初始路径

      $route['default_controller'] = 'index1';

      5、model文件下创建 M_user.php 

        

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class M_user extends CI_Model {
    
    
    	public function insert(){
    
    		$data=array(
    		'uname'=>$this->input->post('username'),
    		'age'=>$this->input->post('age'),
    		'product'=>$this->input->post('product')
    		);
    		$this->db->insert('user', $data);
            return $this->db->insert_id();
    	}
    }
    

      6、controllers 下有两个文件  Index1.php  和 Add.php

    Index1.php内容如下

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Index1 extends CI_Controller {
    
    	/**
    	 * Index Page for this controller.
    	 *
    	 * Maps to the following URL
    	 * 		http://example.com/index.php/welcome
    	 *	- or -
    	 * 		http://example.com/index.php/welcome/index
    	 *	- or -
    	 * Since this controller is set as the default controller in
    	 * config/routes.php, it's displayed at http://example.com/
    	 *
    	 * So any other public methods not prefixed with an underscore will
    	 * map to /index.php/welcome/<method_name>
    	 * @see https://codeigniter.com/user_guide/general/urls.html
    	 */
    	public function index()
    	{
    		$data = array(
    			'title'=>'我的标题',
    			'message'=>'这里是我的内容'
    			
    		);
    		$this->load->view('index',$data);
    	}
    }
    

      Add.php内容如下

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Add extends CI_Controller {
    	
        function __construct() {
            parent::__construct();
    		$this->load->model('M_user');
    		//$this->load->database();
        }
    	
    	public function insert(){
    		
    		if ($this->M_user->insert() > 0)
    				{
    					echo "<script>alert('增加成功');window.history.back(-1);</script>";
    
    				}
    				else
    				{
    					echo "no";
    				}
    		
    	}
    }
    

      7、view 文件夹下创建文件 index.php 

      

    <html>
    <body>
    
    <div id="container">
    	<?php 
    	echo $message;
    	?>
    	<form name="myform" action="index.php/Add/insert" method="post">
    姓名:<input type="text" name="username" width="200px"><p>
    年龄:<input type="text" name="age" width="200px"><p>
    备注:<input type="text" name="product" width="200px"><p>
    <input type="submit" value="提交">
    </form>
    </body>
    </html>
    

      到此,全部完成,可将数据写入数据库,一个简单的增功能就完成了。

  • 相关阅读:
    Global 文件中挂接HttpModule事件的方法列表参考
    百行代码打造一个DI容器(支持瞬时生命周期、单利生命周期、构造函数自动注入、属性自动注入、字段自动注入)
    松耦合服务调用利器服务分发器
    架构视角面面观之: WebPage能支持DI注入那该多好
    架构视角面面观之: WebPage能像MVC的ViewPage那样支持泛型节约不少代码量的?
    安装 Nuget 插件过程以及注意事项
    给Web Api 再加把柴让它更火起来
    Mini 容器泛型类型的使用
    JAVA虚拟机08垃圾回收HotSpot的算法实现细节
    Web UI 设计(网页设计)命名规范
  • 原文地址:https://www.cnblogs.com/jeib/p/6158925.html
Copyright © 2011-2022 走看看