一、MVC

CodeIgniter 采用MVC架构即:控制层、模型层和视图层。

对应Application下面的文件夹   (图1):

所有新建文件以.php结尾

视图层 view 文件夹放入HTML模板

模型层 model 存放对数据库操作的代码

控制层 controllers 存放进行逻辑判断的代码,从模型层取得数据然后输入到视图层,发送给用户。

图1

功能

1. 模板增加输入表单

2. 控制器增加接收表单数据的代码,并对用户输入进行简单校验。

3. 在表单上方输出标题和正文,以及发布时间。

用到的知识点:CI helper类(url) 和 输入类(input),

以及CI ActiveRecord 和向模板传值。

二、初始配置

1. 链接数据库

修改数据库配置:/application/config/database.php

1
2
3
4
5
6
'hostname' => 'localhost',
  'username' => 'root',
  'password' => '',
  'database' => 'test',
  'dbdriver' => 'mysqli',
  'dbprefix' => 'ts_',

  

2. 修改默认路由

CI框架采用单文件入口的方式,默认必须通过index.php来访问控制层。比如controllers文件夹下有个名为test的class类,test有个叫home的function,

则访问URL为:http://www.example.com/index.php/test/home

三、输出页面

1. 直接输出HTML模板

新建两个文件分别位于controllers文件夹和views文件夹

Test.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
    public function home()
    {
        $this->load->view('home');
    }
}
home.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home</title>
</head>
<body>
    <h1>这是我们的主页</h1>
</body>
</html>

  

在浏览器打开类似如下地址:http://test.com/index.php/test/home

2. 插入数据库条目

创建数据库表ts_news

Test.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('news_model');
}
public function home()
{
$this->load->view('home');
}
public function add_news(){
$title $this->input->get('title',TRUE);
$content $this->input->get('content');
if ( (strlen($title) < 20 ) or (strlen($content) < 20 ) ){
echo '标题或正文内容过短';
return false;
}
$arr array(
'id' => '',
'title' => $title,
'content' => $content,
'update_time' => time(),
'create_time' => time()
);
$check $this->news_model->insert($arr,'news');
if ($check)
{
redirect('test/home');
}
else
{
echo '提交失败';
}
}
}
home.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<h1>这是我们的主页</h1>
<hr>
<form action="<?php echo site_url('test/add_news'); ?>">
<label for="title">标题</label>
<input type="text" name="title" value="">
<br>
<label for="content">正文</label>
<textarea name="content" id="" cols="30" rows="10"></textarea>
<br>
<input type="submit" value="提交" >
</form>
</body>
</html>
News_model.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class News_model extends CI_Model {
public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
public function insert($arr,$table)
{
$this->db->insert($table$arr);
        if ($this->db->affected_rows() > 0)
        {
            return $this->db->insert_id();
        }
        else
        {
            return FALSE;
        }
}
}
412ded80-4884-4a2f-ae37-6ba69cdc4278
493498ee-0f5c-4676-9cec-38e5a3f3e6fd
    3. 查询数据库并输出
    News_model.php增加
public function get_all($table)
    {
        $this->db->select('*');
        $query $this->db->get($table);
        $query $query->result_array();
        return $query;
    }
Test.php 的 home修改为:
public function home()
    {
        $news $this->news_model->get_all('news');
        $data['news'] = $news;
        $this->load->view('home',$data);
    }

  Home模板的body修改为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<body>
<h1>这是我们的主页</h1>
<?php
foreach ($news as $key => $value) {
echo '<div>
<h3>'.$value['title'].'</h3>
<span>发布时间:'.date('Y-m-d H:i:s',$value['create_time']).'</span>
<p>'.$value['content'].'</p>
</div>';
}
?>
<hr>
<form action="<?php echo site_url('test/add_news'); ?>">
<label for="title">标题</label>
<input type="text" name="title" value="">
<br>
<label for="content">正文</label>
<textarea name="content" id="" cols="30" rows="10"></textarea>
<br>
<input type="submit" value="提交" >
</form>
</body>

  

  

刷新查看效果:

CodeIgniter 入门教程第一篇:信息发布的更多相关文章

  1. Vue入门教程 第一篇 (概念及初始化)

    注:为了本教程的准确性,部分描述引用了官网及网络内容. 安装Vue 1.使用npm安装vue: npm install vue 2.下载使用js文件: https://vuejs.org/js/vue ...

  2. 《进击吧!Blazor!》系列入门教程 第一章 8.部署

    <进击吧!Blazor!>是本人与张善友老师合作的Blazor零基础入门教程视频,此教程能让一个从未接触过Blazor的程序员掌握开发Blazor应用的能力. 视频地址:https://s ...

  3. spring cloud系列教程第一篇-介绍

    spring cloud系列教程第一篇-介绍 前言: 现在Java招聘中最常见的是会微服务开发,微服务已经在国内火了几年了,而且也成了趋势了.那么,微服务只是指spring boot吗?当然不是了,微 ...

  4. (转) 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)

    一.spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运 ...

  5. EnjoyingSoft之Mule ESB开发教程第一篇:初识Mule ESB

    目录 1. Mule ESB基本介绍 2. Mule ESB社区版和企业版 3. Mule ESB常用场景 4. Mule ESB软件安装 客户端安装 服务端安装 5. 第一个Mule ESB应用- ...

  6. Entity Framework 6.0 入门系列 第一篇

    Entity Framework 6.0 入门系列 第一篇 好几年前接触过一些ef感觉不是很好用,废弃.但是 Entity Framework 6.0是经过几个版本优化过的产物,性能和功能不断完善,开 ...

  7. SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(转载)

    SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本) 转载请标明出处:http://blog.csdn.net/forezp/article/details ...

  8. CodeIgniter框架入门教程——第一课 Hello World!

    本文转载自:http://www.softeng.cn/?p=45 今天开始,我将在这里连载由我自己编写的<CodeIgniter框架入门教程>,首先,这篇教程的读着应该是有PHP基础的编 ...

  9. Swing:LookAndFeel 教程第一篇——手把手教你写出自己的 LookAndFeel

    本文是 LookAndFeel 系列教程的第一篇. 是我在对 Swing 学习摸索中的一些微薄经验. 我相信,细致看全然系列之后.你就能写出自己的 LookAndFeel. 你会发现 Swing 原来 ...

随机推荐

  1. 在windows下部署laravel项目的步骤

    laravel版本:5.4 php版本:7.1以上,我用的php7.2.7 1.代码库下载laravel源码,放在你自己的运行目录下 2.配置hosts域名及 apache域名配置 3.安装compo ...

  2. 蓝鲸安装Agent

    1. APPO 所在机器(在 app 运行所在机器) 必须能通过 ssh 登陆到 Agent 机器2. Agent 所在机器可以访问到 zk 的端口3. 支持 Linux/Windows/AIX 操作 ...

  3. C# 关键字 Visual Studio 2012

    C# 关键字 Visual Studio 2012 其他版本 关键字是对编译器具有特殊意义的预定义保留标识符. 它们不能在程序中用作标识符,除非它们有一个 @ 前缀. 例如,@if 是有效的标识符,但 ...

  4. nmap小技巧[1] 探测大网络空间中的存活主机

    url: nmap是所有安全爱好者应该熟练掌握的扫描工具,本篇介绍其在扫描大网络空间时的用法. 为什么要扫描大网络空间呢? 有这样的情形: 内网渗透   攻击者单点突破,进入内网后,需进一步扩大成果, ...

  5. html checkbox 实现全选/取消全选

    html checkbox  实现全选/取消全选 <html> <body> <table border="1"> <tr> < ...

  6. Spring AOP 面向切面编程相关注解

    Aspect Oriented Programming 面向切面编程   在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业.   ...

  7. Spring 配置中的 ${}

    Spring 配置中的 ${}     <!-- ============ GENERAL DEFINITIONS========== --> <!-- Configurer tha ...

  8. Oracle 官网 jdk1.6 下载地址

    在oracle官方网站下载地址 http://www.oracle.com/technetwork/java/archive-139210.html http://www.oracle.com/tec ...

  9. ros下基于百度语音的,语音识别和语音合成

    代码地址如下:http://www.demodashi.com/demo/13153.html 概述: 本demo是ros下基于百度语音的,语音识别和语音合成,能够实现文字转语音,语音转文字的功能. ...

  10. How to learn a new technology

    是什么?为什么会出现? 这一阶段主要是对该技术有一个整体了解,他所解决的是什么问题,他的整体结构等. 怎么做? 最简单的是找一个上手视频,因为视频是非常直观的展示了技术的使用.先学会用是最根本的,对于 ...