CodeIgniter 入门教程第一篇:信息发布
来源:互联网 发布:群智能算法应用 编辑:程序博客网 时间:2024/06/06 04:40
一、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
'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
<?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
<?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修改为:
<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 入门教程第一篇:信息发布
- CodeIgniter框架源码分析第一篇comnon.php
- CodeIgniter经典入门教程
- CodeIgniter框架入门教程-HelloWorld
- 我要发布第一篇程序文档
- 发布第一篇博客之心路历程
- 信息安全策略第一篇:Acceptable Use Policy
- 信息安全第一篇(加密算法介绍)
- 第一篇:JAVA获得设备信息
- Beej的网络编程入门教程 第一篇 版权与目录
- DZ插件制作简易入门教程(自学手记)第一篇
- 第一篇使用live writer发布的文档
- 使用Windows Live Writer发布的第一篇日志
- 开局第一篇(删除之后重新发布的。。。)
- 信息安全数学基础 第一篇-数论基础-第一章 整除
- 开源php框架: codeigniter 1.6.0发布
- 第一篇。
- 第一篇
- Centos7安装并配置mysql5.6完美教程
- node与javascriptAES加密
- zabbix
- Linux tty驱动程序<一> 架构
- Deconvolutional network
- CodeIgniter 入门教程第一篇:信息发布
- Android 开发笔记——通过 Intent 传递类对象
- CENTOS7快速入门(一)
- JAVA中sort函数的简单使用方法 (JAVA新手向)
- 专业的Excel C ++ API Aspose.Cells for C++ 发布 | 附下载
- jquery checkbox 全选反选,解决第二次失败的问题
- 次方求模
- Ueditor图片上传成功后,返回json数据格式
- Git相关整理